954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

facing problem in database connectivity in java to mysql

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.(ConnBean.java:25)

at ConnBean.main(ConnBean.java:44)

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

Process Exit...

muskan
Newbie Poster
11 posts since Jul 2007
Reputation Points: 9
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

muskan
Newbie Poster
11 posts since Jul 2007
Reputation Points: 9
Solved Threads: 0
 

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

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.(ConnBean.java:25)

at ConnBean.main(ConnBean.java:44)

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

Process Exit...

cnbaluramesh
Newbie Poster
1 post since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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

staneja
Junior Poster in Training
64 posts since Dec 2006
Reputation Points: 10
Solved Threads: 2
 

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
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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".

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

sounds like perfect manager material for a callcenter...

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

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.

muskan
Newbie Poster
11 posts since Jul 2007
Reputation Points: 9
Solved Threads: 0
 

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

Ahmed Zizo
Newbie Poster
2 posts since Dec 2007
Reputation Points: 10
Solved Threads: 1
 
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)

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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.

vaibhav.galne
Newbie Poster
1 post since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

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?

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You