komlaaa
asked on
connecting to mysql database on localhost
Hi,
Below is code and and the output i received.
Does anyone know what is wrong?
//////////////code /////////////
package dbconnect;
import java.sql.*;
public class TestDriver
{
private static String url = "jdbc:mysql://localhost/db 1";
private static String login = "achille";
private static String password = "xia0011";
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.od bc.JdbcOdb cDriver");
//Class.forName("org.gjt.m m.mysql.Dr iver");
System.out.println("Loaded Drivers!");
}
catch(Exception e)
{
System.out.println("Driver Error:" + e);
}
try
{
Connection con = DriverManager.getConnectio n(url, login, password);
System.out.println("Connec tion successful!");
}
catch(Exception f)
{
System.out.println("Connec tion Error: " + f);
}
}
}
//////////////// output //////////////////
Loaded Drivers!
Connection Error: java.sql.SQLException: No suitable driver
Below is code and and the output i received.
Does anyone know what is wrong?
//////////////code /////////////
package dbconnect;
import java.sql.*;
public class TestDriver
{
private static String url = "jdbc:mysql://localhost/db
private static String login = "achille";
private static String password = "xia0011";
public static void main(String[] args)
{
try
{
Class.forName("sun.jdbc.od
//Class.forName("org.gjt.m
System.out.println("Loaded
}
catch(Exception e)
{
System.out.println("Driver
}
try
{
Connection con = DriverManager.getConnectio
System.out.println("Connec
}
catch(Exception f)
{
System.out.println("Connec
}
}
}
//////////////// output //////////////////
Loaded Drivers!
Connection Error: java.sql.SQLException: No suitable driver
try "com.mysql.jdbc.Driver"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
1.)
>
//Class.forName("org.gjt.m m.mysql.Dr iver");
you need to uncomment this line
>
2.)
>try "com.mysql.jdbc.Driver"
object and tushaganguli,
when i try both of your suggestions i am getting the error:
Driver Error:java.lang.ClassNotFo undExcepti on: com.mysql.jdbc.Driver
Connection Error: java.sql.SQLException: No suitable driver
>
//Class.forName("org.gjt.m
you need to uncomment this line
>
2.)
>try "com.mysql.jdbc.Driver"
object and tushaganguli,
when i try both of your suggestions i am getting the error:
Driver Error:java.lang.ClassNotFo
Connection Error: java.sql.SQLException: No suitable driver
ASKER
also in the statement: 'Connection con = DriverManager.getConnectio n(url, login, password);'
variable 'con' is red undelined in error by my editor reading: 'the field TestDriver.con is never read locally'
variable 'con' is red undelined in error by my editor reading: 'the field TestDriver.con is never read locally'
you also need the driver jar in your classpath
Just a note about design.
You shouldn't do separate try catch blocks in cases like this.
You are writing a quick one-off class to do something. If anything goes wrong loading the driver code, you die (what else can you do?) If anything goes wrong getting the connection, you die. So since you have no response, the clean way to write this is:
public static void main(String[] args) throws Exception { // if anything goes wrong, show the stack trace
Class.forName(...);
Connection conn = ...
}
Or you could have once try ... catch block if you like, but it's pointless having two. In fact, it's counter-productive, because once you handle the error you keep going and get another one because the code isn't loaded.
Once you are in production with real code, you want to have a static initializer where your classes are loaded:
static {
Class.forName(...);
}
because that is incredibly slow. Since that can throw ClassNotFoundException, you probably want to catch that and log it somewhere.
You shouldn't do separate try catch blocks in cases like this.
You are writing a quick one-off class to do something. If anything goes wrong loading the driver code, you die (what else can you do?) If anything goes wrong getting the connection, you die. So since you have no response, the clean way to write this is:
public static void main(String[] args) throws Exception { // if anything goes wrong, show the stack trace
Class.forName(...);
Connection conn = ...
}
Or you could have once try ... catch block if you like, but it's pointless having two. In fact, it's counter-productive, because once you handle the error you keep going and get another one because the code isn't loaded.
Once you are in production with real code, you want to have a static initializer where your classes are loaded:
static {
Class.forName(...);
}
because that is incredibly slow. Since that can throw ClassNotFoundException, you probably want to catch that and log it somewhere.
This code is working in my environment:
/**
* Used to get DB Connection using JDBC
*/
protected Connection getJdbcConnection()
throws Exception
{
final String CONNECT_STRING = "jdbc:mysql://localhost:33 06/db1?use r=achille& password=x ia0011";
Class.forName( "org.gjt.mm.mysql.Driver" );
return DriverManager.getConnectio n ( CONNECT_STRING );
}
plz make sure that the library of yr mysql is located in classpath, in my case it was jar file named "mm.mysql-2.0.7-bin.jar"
I think u can have the suitable library from
http://dev.mysql.com/downloads/
search for "MySQL Connector/J"
and select appropriate driver
/**
* Used to get DB Connection using JDBC
*/
protected Connection getJdbcConnection()
throws Exception
{
final String CONNECT_STRING = "jdbc:mysql://localhost:33
Class.forName( "org.gjt.mm.mysql.Driver" );
return DriverManager.getConnectio
}
plz make sure that the library of yr mysql is located in classpath, in my case it was jar file named "mm.mysql-2.0.7-bin.jar"
I think u can have the suitable library from
http://dev.mysql.com/downloads/
search for "MySQL Connector/J"
and select appropriate driver
ASKER
>you also need the driver jar in your classpath
>plz make sure that the library of yr mysql is located in classpath, in my case it was jar
>file named "mm.mysql-2.0.7-bin.jar"
I put 'mysql-connector-3.1.10-bi n-jar' library file under: C:\Program Files\Java\jdk1.5.0_04\lib
and the java class is under:
C:\Documents and Settings\Achille Komla\workspace\TestDB\dbc onnect\Tes tDriver.ja va
are these the right path?
>plz make sure that the library of yr mysql is located in classpath, in my case it was jar
>file named "mm.mysql-2.0.7-bin.jar"
I put 'mysql-connector-3.1.10-bi
and the java class is under:
C:\Documents and Settings\Achille Komla\workspace\TestDB\dbc
are these the right path?
ASKER
can someone help out with this?
Can u please echo the CLASSPATH variable in your environment.
If u have windows OS, use:
echo %CLASSPATH%
If u have Linux OS, use:
echo $CLASSPATH
The point is the library should be in any directory but this directory should be defind in the CLASSPATH environment variable in your OS.
There are other methods for seting yr CLASSPATH depending on your application behaviour.
If u mention the nature of your application, we can tell u best ways for doing so.
If u have windows OS, use:
echo %CLASSPATH%
If u have Linux OS, use:
echo $CLASSPATH
The point is the library should be in any directory but this directory should be defind in the CLASSPATH environment variable in your OS.
There are other methods for seting yr CLASSPATH depending on your application behaviour.
If u mention the nature of your application, we can tell u best ways for doing so.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>
Can u please echo the CLASSPATH variable in your environment.
If u have windows OS, use:
echo %CLASSPATH%
>
doing 'echo %CLASSPATH%' i have:
C:\Documents and Settings\Achille Komla>
>There are other methods for seting yr CLASSPATH depending on your application behaviour.
>If u mention the nature of your application, we can tell u best ways for doing so.
As shown in the piece of code above, i just trying to connect to MySql database, on my localhost (simply testing that i can connect to this db without any other tools such us hibernate)
Can u please echo the CLASSPATH variable in your environment.
If u have windows OS, use:
echo %CLASSPATH%
>
doing 'echo %CLASSPATH%' i have:
C:\Documents and Settings\Achille Komla>
>There are other methods for seting yr CLASSPATH depending on your application behaviour.
>If u mention the nature of your application, we can tell u best ways for doing so.
As shown in the piece of code above, i just trying to connect to MySql database, on my localhost (simply testing that i can connect to this db without any other tools such us hibernate)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks all for helping out :)
you need to uncomment this line
you also do not need the following:
Class.forName("sun.jdbc.od