Link to home
Start Free TrialLog in
Avatar of dkim18
dkim18

asked on

JDBC properties file

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)

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hope thats not your real user/password :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
oh sorry... the exception is at JDBCBook.createConnection (). I thought it was at JDBCLibrary.createConnection (). Yeah, quite possible that DRIVER could be null.
Avatar of dkim18
dkim18

ASKER

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,
> System.out.println(props); prints this:
> {}

Thats means nothing was loaded.

http://www.objects.com.au