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