Connection to Mysql Database
Hallo.
I'm trying to connect to mysql database using Java JDBC driver.
I have a code like that
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
Connection connection=null;
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName).newInstance();
Connection connnection = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected to database\n");
return connection;
} catch (ClassNotFoundException e) {
//.... // Error handle
}
}
};
but when i lunch this class, i have message connected to database, ok.
but future when i'm trying to use
connection.createStatement();
i recieve an error
java.lang.NullPointerException
Could you tell where could be a problem?
Thanks,
Urmas.
[982 byte] By [
Urrimus2a] at [2007-11-26 15:48:02]

> but when i lunch this class, i have message connected> to database, ok.How can you execute the code? It doesn't compile.Kaj
The whole code is
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class Client {
public static void main(String argv[]){
DB db = new DB();
Connection connection=db.dbConnect("jdbc:mysql://SERVER/DB_NAME", "USERNAME", "PASSWORD");
Statement s=connection.createStatement(); // <- HERE I HAVE NullPointerException Error, connection is null.
}
}
class DB
{
public DB() {}
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
Connection connection=null;
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName).newInstance();
Connection connnection = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected to database\n");
return connection;
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
return null;
} catch (SQLException e) {
System.out.println("SQLException");
return null;
} catch (Exception ex) {
System.out.println("Exception:"+ex.getMessage()+"\n");
return null;
// handle any errors
}
}
};
The connection returned is null, but database DB_NAME exists on SERVER, USERNAME and PASSWORD are correct. AND JConnector also installed properly.
> The connection returned is null, but database DB_NAME
> exists on SERVER, USERNAME and PASSWORD are correct.
> AND JConnector also installed properly.
And you are sure that MySQL is configured to allow that user to connect to the database from the host from which the attempt is made.
Also, do any of those catch blocks print anything?
You should have the catch block print the actual exception message at a minimum (it contains more than just the type of exception) and probably also the StackTrace.
I've had connected to Database using SQL manager 2005, and therefore Username and Pass and Database_Name are correct.
I have a MySql 5 database, maybe JDBS doesn't support MySql 5.0?
And no errors are catched, only returned connection is null.
And connected to database is outputted, i think that connection variable returned, not null from exceptions.
Urmas.
Make sure you have the proper driver for your version. Download a new one from MySQL to be sure.
Also, I find it hard to believe that getConnection is returning null. I have never heard of this before. If anything happens to prevent connection to the DB, getConnection will throw an SQLException, not return null.
I can't think of any conditions under which getConnection() would return null.
In any case, this is much more of a Driver issue than it is a pure Java issue, so I would suggest the following:
1. Download the correct version of the Driver for your database version (as mentioned above) this can be found at http://www.mysql.org/.
2. If the problem persists, ask at the MySQL forums (but search their bug lists/faqs/forums first).
The only problem with your code is
public Connection dbConnect(String db_connect_string,
String db_userid, String db_password)
{
Connection connection=null;
try {
String driverName = "com.mysql.jdbc.Driver";
Class.forName(driverName).newInstance();
Connection connnection = DriverManager.getConnection(db_connect_string, db_userid, db_password);
System.out.println("connected to database\n");
return connection;
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
return null;
} catch (SQLException e) {
System.out.println("SQLException");
return null;
} catch (Exception ex) {
System.out.println("Exception:"+ex.getMessage()+"\n");
return null;
// handle any errors
}
}
there is one connection and one connnection which is the actual reference to the connection. So correct the spell mistackes and remove the declearation of connection before the try block. It will work fine