Solved

JDBC properties file

Posted on 2003-12-09
7
6,901 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 84
Cipher Configuration on Apache HTTPD 4 62
javap not working 8 44
String array comparison 4 33
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 …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

770 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