Java
  Home arrow Java arrow Page 5 - Reading a Properties File for a Java Appli...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Reading a Properties File for a Java Application using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 11
    2006-05-31

    Table of Contents:
  • Reading a Properties File for a Java Application using NetBeans IDE
  • How to get the current working folder (or where the current class file exists) in Java
  • Starting to develop a Java application using NetBeans IDE
  • Adding a properties file
  • Reading the connection string from the properties file

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    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.

       · Hello guys! You can read properties file in a simpler fashion using Java together...
       · ResourceBundle bundle =...
       · Your method getLocalDirName can be improved in three ways:1) you call...
     

    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...






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT