
March 24th, 2004, 06:40 AM
|
 |
Registered User
|
|
Join Date: Mar 2004
Location: Sweden
Posts: 3
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Using java to connect to a mysql database
I'm having problem connecting to a mysql database using java. Anyone here who knows this stuff and could give me some help?
I'm using mysql to run a local sql server on my machine (win xp). No password is required for connection and connecting to it locally from the command prompt works like a dance.
But I can't connect to it using java and I'm not sure if the fault is with my code or with the installation of the MySQL ConnectorJ driver. I have been reading tons of documentation about the driver and the mysql server, but I can't find any clear instructions on how to install it other than putting the jar file in the java classpath which is what I have done.
The code used for the connection looks like this:
Code:
import java.sql.*;
import javax.swing.*;
public class javasql{
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();
}
try{
Connection c =
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?user=Someone");
c.close();
}catch (SQLException e){
e.printStackTrace();
}
Statement stmt = null;
ResultSet rs = null;
try {
if (stmt.execute("SELECT name FROM test")) { //this is row 38
rs = stmt.getResultSet();
}
}
catch(Exception e){
e.printStackTrace();
}
And the error message I receive is:
Quote: java.lang.NullPointerException
at javasql.main(javasql.java:38) |
Btw, I use the NetBeans IDE 3.5.1 editor.
|