Home arrow Java arrow Page 5 - Reading a Properties File for a Java Application using NetBeans IDE
JAVA

Reading a Properties File for a Java Application using NetBeans IDE


This article introduces you to a step-by-step process for developing Java (or JFC) based applications with Microsoft SQL Server as the database, using NetBeans IDE. It is the second in a series, and focuses on showing you how to read the properties file.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 17
May 31, 2006
TABLE OF CONTENTS:
  1. · Reading a Properties File for a Java Application using NetBeans IDE
  2. · How to get the current working folder (or where the current class file exists) in Java
  3. · Starting to develop a Java application using NetBeans IDE
  4. · Adding a properties file
  5. · Reading the connection string from the properties file

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Reading a Properties File for a Java Application using NetBeans IDE - Reading the connection string from the properties file
(Page 5 of 5 )

Once you have completed all the steps in the previous section, you need to copy the following code in your button "actionperformed" event:

try {
            ResourceBundle bdl = new PropertyResourceBundle(new
FileInputStream(getLocalDirName() + "/dbconfig.properties"));
            String url = bdl.getString("url");
            Properties p = new Properties();
            Enumeration keys = bdl.getKeys();           

            while(  keys.hasMoreElements( ) ) {
                String prop = (String)keys.nextElement( );
                String val = bdl.getString(prop);  

                p.setProperty(prop, val);
            }
            Class.forName(bdl.getString("driver")).newInstance
( );
            Connection conn = DriverManager.getConnection(url,
p);
            Statement sql_stmt = conn.createStatement();
            ResultSet rset = sql_stmt.executeQuery("SELECT * FROM
Northwind..orders ");
            int i=0;
            while (rset.next()){
                i++;
            }
            //Close the ResultSet and Statement
            rset.close();
            sql_stmt.close();
            //Close the database connection
            conn.close();
            this.txtMsg.setText(Integer.toString(i) + " rows
found");
        }
        catch(Exception e) {
            this.txtMsg.setText("Failed to connect; Please view
Stack Trace");
            e.printStackTrace();
        }

You can press F5 to execute your application.

Remarks

You can develop the same application using several other methods.  I don't claim that this is the best method.  I just wanted to introduce a few of the issues that most need to be addressed through this article.

The entire code for this article is freely available in the form of a zip file, linked to at the beginning just after the introduction.  That downloadable solution was developed using NetBeans 4.1 IDE and tested with Microsoft SQL Server 2000 Database Enterprise Edition (with Service Pack 3).  I didn't really test it in any other version.  If you are unable to connect to the appropriate database, please post in the discussion section, so that I may guide you.

Another issue is that this entire article focuses on the Microsoft platform only.  The steps I discussed in this article would never help you for any other operating system (except the programming code).  I used Microsoft Windows 2003 Standard Edition to work this sample.  I suggest that you follow the steps according to the operating system installed on your computer. Most of them will be very similar to follow and very easy to understand.

The final issue is that none of my articles in this series are optimized for performance.  Tuning or improving the performance of a Java application is beyond the scope of this article.  I simply wanted to explain the concepts a bit more clearly.  

Any doubts, bugs, errors, suggestions, feedback etc. are highly appreciated at jag_chat@yahoo.com.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials