Solved

JDBC properties file

Posted on 2003-12-09
7
6,891 Views
Last Modified: 2007-12-19
I am trying to configure JDBC property via a Projerties file.
This is original.

public class JDBCLibrary{

  private static final String DRIVER= "com.mysql.jdbc.Driver";
  private static final String DB_URL = "jdbc:mysql://webdev.apl.jhu.edu/library";
  private static final String USER = "lib_user";
  private static final String PASSWORD = "jhu481";

  private Connection conn;

  public JDBCLibrary() throws ClassNotFoundException, SQLException{
    conn = createConnection();
  }

  private Connection createConnection() throws ClassNotFoundException, SQLException{
    Class.forName(DRIVER) ;
    Connection conneciton = DriverManager.getConnection(DB_URL, USER, PASSWORD) ;

    return conneciton;
  }

  public synchronized void excuteQuery() throws SQLException{
    final String QUERY =
        "SELECT TITLE, AUTHOR FROM BOOKS";
    Statement statement = conn.createStatement() ;
    ResultSet results = statement.executeQuery(QUERY);
    System.out.println("Books: ");

    while(results.next()){
      System.out.println("Book: "+results.getString(1) );
      System.out.println("Author: "+results.getString(2));
    }
    results.close();
    statement.close();
  }


This is updated one.
--------------------------------------
 public class JDBCBook{

   private static String DRIVER;
   private static String DB_URL;
   private static String USER;
   private static String PASSWORD;
   
   public JDBCBook() throws ClassNotFoundException, SQLException{
     conn = createConnection();

     bookList = new ArrayList();
   }

   private Connection createConnection() throws ClassNotFoundException, SQLException{
     try {
       Properties props = new Properties();
       props.load(this.getClass().getResourceAsStream("/config/jdbc.properties"));
       DRIVER = props.getProperty("jdbc.driver");
       USER = props.getProperty("jdbc.user");
       DB_URL = props.getProperty("jdbc.url");
       PASSWORD = props.getProperty("jdbc.password");
     }catch (IOException e) {
       System.out.println(e);
     }

     Class.forName(DRIVER) ;
     Connection conneciton = DriverManager.getConnection(DB_URL, USER, PASSWORD) ;

     return conneciton;
   }
----------------------------------
but, I am having these errors.

Exception in thread "main" java.lang.NullPointerException
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:140)
        at dkim18.jdbc.JDBCBook.createConnection(JDBCBook.java:46)
        at dkim18.jdbc.JDBCBook.<init>(JDBCBook.java:29)
        at dkim18.jdbc.JDBCBook.main(JDBCBook.java:89)

0
Comment
Question by:dkim18
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 120 total points
ID: 9910157
looks like either "jdbc.properties" is not defined in your properties file, or the file is not being found.
Print out the value of props before using it to check:

Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/config/jdbc.properties"));
System.out.println(props);
DRIVER = props.getProperty("jdbc.driver");
...
0
 
LVL 9

Assisted Solution

by:yongsing
yongsing earned 110 total points
ID: 9910159
Make sure that DRIVER is not null.

System.out.println(DRIVER == null); // Check!
Class.forName(DRIVER) ;
Connection conneciton = DriverManager.getConnection(DB_URL, USER, PASSWORD) ;

If printed "true", then DRIVER is null, which means that the property is not correctly read.
0
 
LVL 92

Expert Comment

by:objects
ID: 9910188
Hope thats not your real user/password :)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 20 total points
ID: 9910203
But the DRIVER string is being initialized.... don't think that would be null. Maybe that the com.mysql.jdbc.Driver driver is not found.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 9910211
oh sorry... the exception is at JDBCBook.createConnection (). I thought it was at JDBCLibrary.createConnection (). Yeah, quite possible that DRIVER could be null.
0
 

Author Comment

by:dkim18
ID: 9910238
I figure it out. my jdbc.properties file contains some errors
BTW
System.out.println(props); prints this:
{}

System.out.println(DRIVER == null); prints this:
false

thanks,
0
 
LVL 92

Expert Comment

by:objects
ID: 9910261
> System.out.println(props); prints this:
> {}

Thats means nothing was loaded.

http://www.objects.com.au

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now