please help me to resolve the databasbase connectivity problem in java for mysql .i am using jdk 1.5 ,if there is need to put a file then please mention where it should be copied ,and i am writing the code and error occuring that code.

code:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.*;
import java.io.*;

public class ConnBean 
{
Connection conn;
Statement st ;
  public ConnBean()
  {
     String sqlDriver= "com.mysql.jdbc.Driver";
     String hostName    = "localhost:3306/";
     String dbName  = "mydb";
     String userID  = "root";
      String password   = "webportal";

    //databse URL
    String dbURL = "jdbc:mysql://" + hostName +  dbName;

    try {
      Class.forName(sqlDriver);
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(dbURL, userID, password);
      st = conn.createStatement();
      System.out.println("db connected");
    }
    catch (SQLException e) {

      System.out.println("Exception occured during get connection" + e);
    }
  }

public static void main(String asdas[]) throws SQLException
  {

  ConnBean conn=new ConnBean();


 //ResultSet rs=conn.executeQ("select * from clients");
  }
}

Error

D:\Program Files\Java\jdk1.5.0\bin\java.exe   ConnBean
Working Directory - C:\final project\jsp code\
Class Path - .;c:\Kawapro5.0\kawaclasses.zip;d:\program files\java\jdk1.5.0\lib\tools.jar;d:\program files\java\jdk1.5.0\jre\lib\rt.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Class.java:164)

    at ConnBean.<init>(ConnBean.java:25)

    at ConnBean.main(ConnBean.java:44)

Exception occured during get connectionjava.sql.SQLException: No suitable driver

Process Exit...
peter_budo commented: Multiposting is not welcome here -1

Recommended Answers

You need to add the mysql driver jar to your classpath.

Jump to Post

That is exactly what he got as naswer in JSP section , plus closer description and he still doesn't do it:@

Jump to Post

Hi..........in general what you can do is .....create one lib folder in your project and copy the driver file for connection in that and set calss path to your lib folder...........

set classpath ....u have to do this stem into system variables
My computer -: properties -: Advanced -:Environment …

Jump to Post

Classpath can also be set as a parameter in the javac command with the -cp (or -classpath) switch

javac -cp .;%CLASSPATH%;c:/myProject/lib/aJarFile.jar myProgram.java
Jump to Post

All 14 Replies

You need to add the mysql driver jar to your classpath.

That is exactly what he got as naswer in JSP section , plus closer description and he still doesn't do it:@

please tell me how to put the mysql driver in the class path please mention the method kindly.thanks

you need to set the classpath.
copy the mysql-------.jar to in working directory and set the classpath.

set classpath=%classpath%;<your mysql-----.jar place path>;.;

try this and get back with result

please help me to resolve the databasbase connectivity problem in java for mysql .i am using jdk 1.5 ,if there is need to put a file then please mention where it should be copied ,and i am writing the code and error occuring that code.

code:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.*;
import java.io.*;

public class ConnBean 
{
Connection conn;
Statement st ;
  public ConnBean()
  {
     String sqlDriver= "com.mysql.jdbc.Driver";
     String hostName    = "localhost:3306/";
     String dbName  = "mydb";
     String userID  = "root";
      String password   = "webportal";

    //databse URL
    String dbURL = "jdbc:mysql://" + hostName +  dbName;

    try {
      Class.forName(sqlDriver);
    }
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      conn = DriverManager.getConnection(dbURL, userID, password);
      st = conn.createStatement();
      System.out.println("db connected");
    }
    catch (SQLException e) {

      System.out.println("Exception occured during get connection" + e);
    }
  }

public static void main(String asdas[]) throws SQLException
  {

  ConnBean conn=new ConnBean();


 //ResultSet rs=conn.executeQ("select * from clients");
  }
}

Error

D:\Program Files\Java\jdk1.5.0\bin\java.exe   ConnBean
Working Directory - C:\final project\jsp code\
Class Path - .;c:\Kawapro5.0\kawaclasses.zip;d:\program files\java\jdk1.5.0\lib\tools.jar;d:\program files\java\jdk1.5.0\jre\lib\rt.jar
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

    at java.security.AccessController.doPrivileged(Native Method)

    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)

    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)

    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)

    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

    at java.lang.Class.forName0(Native Method)

    at java.lang.Class.forName(Class.java:164)

    at ConnBean.<init>(ConnBean.java:25)

    at ConnBean.main(ConnBean.java:44)

Exception occured during get connectionjava.sql.SQLException: No suitable driver

Process Exit...

end quote.

Hi..........in general what you can do is .....create one lib folder in your project and copy the driver file for connection in that and set calss path to your lib folder...........

set classpath ....u have to do this stem into system variables
My computer -: properties -: Advanced -:Environment variables

Classpath can also be set as a parameter in the javac command with the -cp (or -classpath) switch

javac -cp .;%CLASSPATH%;c:/myProject/lib/aJarFile.jar myProgram.java

and RTFM. It's well explained in the documentation, if you took the trouble to read it which you didn't or you'd not have this "problem".

Unfortunately for us, our friend muskan made so many post about his problem on this forum that I lost track of which post is from which day or time. What I say for the sake of it is, I gave answer for this problem too, this was directly to Java Sun tutorial site explaining seting up PATH and CLASSPATH.
However muskan so far did not pay attention, if he/she not willing to the job who else should?

sounds like perfect manager material for a callcenter...

thanx to all of u friends. mt problem was solved by putting connector :mysql-connector-java-3.0.16-ga-bin.jar in jdk ----jre folder in lib----ext and also copy it in tomcat---lib.

that is great , but u can register ur driver by putting this line
DriverManager.registerDriver((driver)d.newInstance());
before u creat the connection
it is good programming practice
and
also u would update JDK to solve this problem in the code
Zizo

thanx to all of u friends. mt problem was solved by putting connector :mysql-connector-java-3.0.16-ga-bin.jar in jdk ----jre folder in lib----ext

Which is exactly the wrong place for it. Never place third party libraries here.

and also copy it in tomcat---lib.

tomcat/common/lib is the proper place (or is it tomcat/lib/common, anyways, only one of those places exist)

You are facing problem while connecting to mysql database ......
i am forwarding you sample code hope this will ressolve u r problem.

code:
import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

follow simple code and make sure mysql connector jar should be in u r classpath.

You are facing problem while connecting to mysql database ......
i am forwarding you sample code hope this will ressolve u r problem.

code:
import java.sql.*;

public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/";
String dbName = "jdbctutorial";
String driver = "com.mysql.jdbc.Driver";
String userName = "root";
String password = "root";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}

completely unnecessary, nothing to do with the problem, and poorly written. Close the resources in a finally block.

follow simple code and make sure mysql connector jar should be in u r classpath.

This was the problem. Of course, you do realise that this thread is over a year old, right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.