Yunus Eren Güzel
Yunus Eren Güzel

Reputation: 3088

JAVA MySQL Connection ClassNotFound Exception

package cs352hw;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
   public static void main(String[] args) {
    // TODO code application logic here
      DB db = new DB();
            Connection conn=db.dbConnect(
              "jdbc:mysql://dijkstra.ug.bcc.bilkent.edu.tr",
              "",
              "");
      }

}


      //This Class is taken from http://www.java-tips.org/other-api-tips/jdbc/how-to-connect-mysql-server-using-jdbc.html
class DB
   {
    public DB() {}

    public Connection dbConnect(String db_connect_string,
      String db_userid, String db_password)
    {
            try
            {
                    Class.forName("com.mysql.jdbc.Driver").newInstance();
                    Connection conn = DriverManager.getConnection(
                      db_connect_string, db_userid, db_password);

                    System.out.println("connected");
                    return conn;

            }
            catch (Exception e)
            {
                    e.printStackTrace();
                    return null;
            }
    }
};

Hey guys my classes are given above

I am new at jdbc stuff

Please help me I am getting run time error such that

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

Thanks for any advice

Note: I don't know How to put the jar file into my classpath and bind it dynamically :S

Upvotes: 0

Views: 3579

Answers (1)

Sangeet Menon
Sangeet Menon

Reputation: 9905

You need to download the MySQL Connector/J and add the .jar file in your application

Add the jar file in the WEB-INF/lib, if its a web-app, else in the lib folder of your project and then try to run

Upvotes: 1

Related Questions