Link to home
Start Free TrialLog in
Avatar of komlaaa
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/db1";
    private static String login = "achille";
    private static String password = "xia0011";
       
    public static void main(String[] args)
    {        
       
         try
         {
              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            //Class.forName("org.gjt.mm.mysql.Driver");
              System.out.println("Loaded Drivers!");
         }
         catch(Exception e)
         {
              System.out.println("Driver Error:" + e);
         }
         
         try
         {
                   Connection con = DriverManager.getConnection(url, login, password);
                   System.out.println("Connection successful!");
         }
         catch(Exception f)
         {
              System.out.println("Connection Error: " + f);
         }
    }

}

//////////////// output //////////////////
Loaded Drivers!
Connection Error: java.sql.SQLException: No suitable driver


Avatar of Mick Barry
Mick Barry
Flag of Australia image

//Class.forName("org.gjt.mm.mysql.Driver");

you need to uncomment this line


you also do not need the following:

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Avatar of tusharganguli
tusharganguli

try "com.mysql.jdbc.Driver"
SOLUTION
Avatar of tusharganguli
tusharganguli

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of komlaaa

ASKER

1.)
>
//Class.forName("org.gjt.mm.mysql.Driver");

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.ClassNotFoundException: com.mysql.jdbc.Driver
Connection Error: java.sql.SQLException: No suitable driver

Avatar of komlaaa

ASKER

also in the statement: 'Connection con = DriverManager.getConnection(url, login, password);'
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.


 
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:3306/db1?user=achille&password=xia0011";
       
        Class.forName( "org.gjt.mm.mysql.Driver" );
        return DriverManager.getConnection ( 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
Avatar of komlaaa

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-bin-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\dbconnect\TestDriver.java

are these the right path?
Avatar of komlaaa

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of komlaaa

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)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of komlaaa

ASKER

thanks all for helping out :)