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:34]

# 1
I'm sorry, but why did you cross post here (former thread is http://forum.java.sun.com/thread.jspa?threadID=5127282&tstart=0).
I suggested you ask at the MySQL forums (that is the forums at http://www.mysql.org/) not the JDBC forums here.
As I said in the other thread, getConnection should not return null. If it is, I believe it is a Driver problem, not a Java language problem, so make sure you have the proper driver for your db and ask at mysql (after searching the buglist/faq/forum at http://www.mysql.org/ first).
# 2
I think your code should not compile.Because there are two declearations of connection.Connection connection=null;..Connection connnection = DriverManager.getConnection(....);.
# 3
use this:
public Connection Connect()
{
Connection con = null;
try
{
String userName = "databaseusername";
String password = "databasepassword";
String url = "jdbc:mysql://localhost/databasename";
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
con = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
}
catch (Exception e)
{
System.err.println ("Cannot connect to database server");
}
return con;
}
# 4
Originally posted by the OP in cross post # 3
> Hallo.
> I have to connect to MySql database.
> I've downloaded JDBC driver, and, using Eclipse, all
> works fine, i'm able to connect to database.
> But now i need to create Java Applet.
> I do not use any IDE, use javac and appletwier from
> console,
> and now when i try to connect to MySql Database, i
> recieve error:
>
> ClassNotFoundException
>
> Seems what JDBC driver is not installed properly.
> But in CLASSPATH requiered
> mysql-connector-java-5.0.4-bin.jar is defined.
>
> Please tell where could be the problem.
> Thanks,
> Urmas.
The problem is that you wouldn't know a classpath if it came up and bit you in the ***.
# 5
look carefully at your method dbConnect(...) and don't allways assume that the problem is in the part where you expect it to be.
You have two variables in there one named connection (with 'nn' in the middle) and one named connnection (with 'nnn' in the middle) connection is initialized with null and never(!) changed.
And that is the one you return.
So your problem has nothing to do with JDBC !
# 6
> look carefully at your method dbConnect(...) and
> don't allways assume that the problem is in the part
> where you expect it to be.
>
> You have two variables in there one named connection
> (with 'nn' in the middle) and one named connnection
> (with 'nnn' in the middle) connection is initialized
> with null and never(!) changed.
> And that is the one you return.
>
> So your problem has nothing to do with JDBC !
Already been pointed out in the very first thread that he created for this problem. Although more and more threads are still being created. I believe he is currently up to 5 of them now, and the solution has been posted in the first for quite a while already (the "nnn" compared to "nn" variable name gaffe).