Results 1 to 4 of 4
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    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.

    :confused:

    Java 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("[B]database.properties[/B]");
           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

  2. #2
    susantasahu01 is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default

    thanks man..

    it is very easy to undrstand...
    it helped me.....

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default jdbc (java.util.)Properties file

    Thanks, however, you would need to document what I don't know and what you know so it becomes easy for me.

    :)

  4. #4
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Quote Originally Posted by jon80 View Post
    Thanks, however, you would need to document what I don't know and what you know so it becomes easy for me.

    :)
    My original question reads that I would like to know how to write the .properties files for example.

Similar Threads

  1. Reading a properties file
    By peiceonly in forum New To Java
    Replies: 7
    Last Post: 05-06-2010, 05:17 PM
  2. Not able to load a properties file
    By Happy9959 in forum New To Java
    Replies: 12
    Last Post: 07-29-2008, 07:15 AM
  3. How to getvalue from properties file into JSP
    By jaga in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-04-2008, 08:00 AM
  4. how to use java.util.Properties?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-03-2007, 05:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •