JDBC properties file

Posted on 2003-12-09
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://";
  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 =
    Statement statement = conn.createStatement() ;
    ResultSet results = statement.executeQuery(QUERY);
    System.out.println("Books: ");

      System.out.println("Book: "+results.getString(1) );
      System.out.println("Author: "+results.getString(2));

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();
       DRIVER = props.getProperty("jdbc.driver");
       USER = props.getProperty("jdbc.user");
       DB_URL = props.getProperty("jdbc.url");
       PASSWORD = props.getProperty("jdbc.password");
     }catch (IOException 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(
        at dkim18.jdbc.JDBCBook.createConnection(
        at dkim18.jdbc.JDBCBook.<init>(
        at dkim18.jdbc.JDBCBook.main(

Question by:dkim18
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 92

Accepted Solution

objects earned 120 total points
ID: 9910157
looks like either "" 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();
DRIVER = props.getProperty("jdbc.driver");

Assisted Solution

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

Expert Comment

ID: 9910188
Hope thats not your real user/password :)
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Author Comment

ID: 9910238
I figure it out. my file contains some errors
System.out.println(props); prints this:

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

LVL 92

Expert Comment

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

Thats means nothing was loaded.


Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

630 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