Swapnil
Swapnil

Reputation: 1134

Error on testing connection with Mysql Database

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;

    //This class is for testing connection with mysql database 
    class JDBCTest {
    // path to database is stored in string url

        private static final String url = "jdbc:mysql://localhost";

    // username is stored  in string root

        private static final String user = "root"; //username

   // password is stored in string password 

       private static final String password = "swapnil";//password

        public static void main(String args[]) {
            try {
//i have stored driver in c:\javap\
            Class.forName("com.mysql.jdbc.Driver").newInstance(); 
                Connection con = DriverManager.getConnection(url, user, password);
                System.out.println("Success");

            } catch (Exception e) {
                System.out.println("hi");
                e.printStackTrace();
            }
        }
    }

whenever I try to run this program I get the exception
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

I am using mysql database my operating system is windows 7 64 bit. I have included the mysql-connector-java-5.1.22-bin in jdk/jre/lib/ext I have also set up the CLASSPATH Environment variable but nothing work me out

Upvotes: 0

Views: 192

Answers (2)

Lakshmi
Lakshmi

Reputation: 2294

The URL is incomplete use:

private static final String url = "jdbc:mysql://localhost:3306/databasename";

also as @JB Nizet mentioned do not put jars in jdk's lib.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 692161

First of all, you should not put anything under the JDK's jre/lib/ext directory.

Instead, use the -cp option when launching your application, and make sure to have the jar of the driver (and not the bin directory) in the classpath:

java -cp mysql-xx.jar;... com.foo.bar.JDBCTest

Upvotes: 3

Related Questions