aspose file tools*
The moose likes JDBC and the fly likes question on JDBC connection to MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "question on JDBC connection to MySQL" Watch "question on JDBC connection to MySQL" New topic

question on JDBC connection to MySQL

rick collette
Ranch Hand

Joined: Mar 22, 2002
Posts: 208
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 = "";
/*== setup database driver and connect ==*/
}catch (Exception ex){}
String conurl = "jdbc:mysql://localhost:3336 atabase01";
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

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]
I agree. Here's the link:
subject: question on JDBC connection to MySQL