Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JDBC properties file

Posted on 2003-12-09
7
Medium Priority
?
6,928 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
[X]
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
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 480 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 440 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 80 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

722 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