Solved

JDBC properties file

Posted on 2003-12-09
7
6,886 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Assisted Solution

by:mayankeagle
mayankeagle 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:mayankeagle
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

22 Experts available now in Live!

Get 1:1 Help Now