| Author |
question on JDBC connection to MySQL
|
rick collette
Ranch Hand
Joined: Mar 22, 2002
Posts: 208
|
|
Hi, I try to connect to my database, but it keeps giving me NullPointerException on "stmt". I am wondering what's wrong with my code? Thanks.
/*== database constants ==*/ String dbhost = "localhost"; String dbname = "mysql"; String dbuser = "***"; String dbpass = "***"; Connection db = null; Statement stmt = null; ResultSet rs = null; String dbdriver = "org.gjt.mm.mysql.Driver"; /*== setup database driver and connect ==*/ try{ Class.forName(dbdriver).newInstance(); }catch (Exception ex){} String conurl = "jdbc:mysql://localhost:3336  atabase01"; try{ db = DriverManager.getConnection(conurl, dbname, dbpass); }catch ( SQLException ex){} /*== create sql query and execute ==*/ String sql = " SELECT id, name FROM sample "; try { stmt = db.createStatement(); rs = stmt.executeQuery(sql);
|
 |
leo donahue
Ranch Hand
Joined: Apr 17, 2003
Posts: 327
|
|
Hi Rick, In your code you set your Statement stmt = null; Then you are trying to use that stmt variable which gives you your null pointer exception. Which I assume happens here: Why not just: Statement stmt; // where you declare your constants
|
Thanks, leo
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Hold on pardner! Catching and burying Exceptions is not a good thing (except in the movies). I'd bet that if you were to do a simple printStackTrace on your caught exceptions, you'd discover a problem.
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
 |
|
|
subject: question on JDBC connection to MySQL
|
|
|