Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-28-2009, 04:54 PM
jon80's Avatar
Senior Member
 
Join Date: Feb 2008
Posts: 195
Rep Power: 3
jon80 is on a distinguished road
Default jdbc (java.util.)Properties file
This code snippet reads from a file named 'database.properties', however, I would like to know what format should the properties be written?

I'm trying to connect to a postgre database, hence, included file named postgresql-8.3-604.jdbc4.jar within the project.



Code:
import java.sql.*;
import java.io.*;
import java.util.*;

 /**
   This program tests that the database and the JDBC
    driver are correctly configured.
*/
class TestDB
{
    public static void main (String args[])
    {
       try
       {
          runTest();
       }
       catch (SQLException ex)
      {
          while (ex != null)
          {
             ex.printStackTrace();
             ex = ex.getNextException();
          }
       }
       catch (IOException ex)
       {
          ex.printStackTrace();
       }
    }

    /**
       Runs a test by creating a table, adding a value, showing the table contents, and
       removing the table.
    */
    public static void runTest()
       throws SQLException, IOException
    {
       Connection conn = getConnection();
       try
       {
          Statement stat = conn.createStatement();

          stat.execute("CREATE TABLE Greetings (Message CHAR(20))");
          stat.execute("INSERT INTO Greetings VALUES ('Hello, World!')");
          ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
          result.next();
          System.out.println(result.getString(1));
          stat.execute("DROP TABLE Greetings");
       }
       finally
       {
          conn.close();
       }
    }

    /**
       Gets a connection from the properties specified
       in the file database.properties
       @return the database connection
    */
    public static Connection getConnection()
       throws SQLException, IOException
    {
       Properties props = new Properties();
       FileInputStream in = new FileInputStream("database.properties");
       props.load(in);
       in.close();

       String drivers = props.getProperty("jdbc.drivers");
       if (drivers != null)
          System.setProperty("jdbc.drivers", drivers);
       String url = props.getProperty("jdbc.url");
       String username = props.getProperty("jdbc.username");
       String password = props.getProperty("jdbc.password");

       return DriverManager.getConnection(url, username, password);
    }
}

References
1. Core Java Vol II 7th Ed.
2. The API
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not able to load a properties file Happy9959 New To Java 12 07-29-2008 08:15 AM
Loading properties from XML file JavaForums Java Blogs 0 05-17-2008 11:31 PM
How to getvalue from properties file into JSP jaga JavaServer Pages (JSP) and JSTL 1 04-04-2008 09:00 AM
Reading a properties file peiceonly New To Java 5 08-09-2007 08:08 PM
how to use java.util.Properties? christina New To Java 2 08-03-2007 06:26 PM


All times are GMT +2. The time now is 08:52 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org