944,043 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3002
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 31st, 2007
-1

facing problem in database connectivity in java to mysql

Expand Post »
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...
Similar Threads
Reputation Points: 9
Solved Threads: 0
Newbie Poster
muskan is offline Offline
11 posts
since Jul 2007
Jul 31st, 2007
0

Re: facing problem in database connectivity in java to mysql

You need to add the mysql driver jar to your classpath.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jul 31st, 2007
0

Re: facing problem in database connectivity in java to mysql

That is exactly what he got as naswer in JSP section , plus closer description and he still doesn't do it
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

please tell me how to put the mysql driver in the class path please mention the method kindly.thanks
Reputation Points: 9
Solved Threads: 0
Newbie Poster
muskan is offline Offline
11 posts
since Jul 2007
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

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


Click to Expand / Collapse  Quote originally posted by muskan ...
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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cnbaluramesh is offline Offline
1 posts
since Aug 2007
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

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
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
staneja is offline Offline
63 posts
since Dec 2006
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

Classpath can also be set as a parameter in the javac command with the -cp (or -classpath) switch
Java Syntax (Toggle Plain Text)
  1. javac -cp .;%CLASSPATH%;c:/myProject/lib/aJarFile.jar myProgram.java
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

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".
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 1st, 2007
0

Re: facing problem in database connectivity in java to mysql

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?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Aug 2nd, 2007
0

Re: facing problem in database connectivity in java to mysql

sounds like perfect manager material for a callcenter...
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Having all kinds of problems
Next Thread in Java Forum Timeline: HELP - - - Longest ERROR MESSAGE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC