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.j hu.edu/lib rary";
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.getConnectio n(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(QUE RY);
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() .getResour ceAsStream ("/config/ jdbc.prope rties"));
DRIVER = props.getProperty("jdbc.dr iver");
USER = props.getProperty("jdbc.us er");
DB_URL = props.getProperty("jdbc.ur l");
PASSWORD = props.getProperty("jdbc.pa ssword");
}catch (IOException e) {
System.out.println(e);
}
Class.forName(DRIVER) ;
Connection conneciton = DriverManager.getConnectio n(DB_URL, USER, PASSWORD) ;
return conneciton;
}
-------------------------- --------
but, I am having these errors.
Exception in thread "main" java.lang.NullPointerExcep tion
at java.lang.Class.forName0(N ative Method)
at java.lang.Class.forName(Cl ass.java:1 40)
at dkim18.jdbc.JDBCBook.creat eConnectio n(JDBCBook .java:46)
at dkim18.jdbc.JDBCBook.<init >(JDBCBook .java:29)
at dkim18.jdbc.JDBCBook.main( JDBCBook.j ava:89)
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.j
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.getConnectio
return conneciton;
}
public synchronized void excuteQuery() throws SQLException{
final String QUERY =
"SELECT TITLE, AUTHOR FROM BOOKS";
Statement statement = conn.createStatement() ;
ResultSet results = statement.executeQuery(QUE
System.out.println("Books:
while(results.next()){
System.out.println("Book: "+results.getString(1) );
System.out.println("Author
}
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()
DRIVER = props.getProperty("jdbc.dr
USER = props.getProperty("jdbc.us
DB_URL = props.getProperty("jdbc.ur
PASSWORD = props.getProperty("jdbc.pa
}catch (IOException e) {
System.out.println(e);
}
Class.forName(DRIVER) ;
Connection conneciton = DriverManager.getConnectio
return conneciton;
}
--------------------------
but, I am having these errors.
Exception in thread "main" java.lang.NullPointerExcep
at java.lang.Class.forName0(N
at java.lang.Class.forName(Cl
at dkim18.jdbc.JDBCBook.creat
at dkim18.jdbc.JDBCBook.<init
at dkim18.jdbc.JDBCBook.main(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hope thats not your real user/password :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
oh sorry... the exception is at JDBCBook.createConnection (). I thought it was at JDBCLibrary.createConnecti on (). Yeah, quite possible that DRIVER could be null.
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,
BTW
System.out.println(props);
{}
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
> {}
Thats means nothing was loaded.
http://www.objects.com.au