File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes sql exception thrown Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "sql exception thrown " Watch "sql exception thrown " New topic
Author

sql exception thrown

riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
iam new to jdbc . i hav installed mysql 4.1.
since iam working on java & jdbc, i got the zip of connector/j 3.1.8.
i unzipped it in a folder-a & added the folder to class path env variables.moreover i also added the myconnector-java-3.1.8-bin jar file avl inside the above a - folder.moreover i did not found
the com & org sub directorie in the unziped folder-a.

when i runned this programm after sucessgully starting the mysql server
i got this messg:

SQLException: No suitable driver
SQLState: 08001
VendorError: 0

the programm is :

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

public class start {
public static void main(String[] args) {

Statement st ;
String create;
create = " CREATE TABLE tea ( tea_name VARCHAR (20), id NUMBER (3))";



try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) { }

try {
Connection c = DriverManager.getConnection("jdbc:mysql://127.0.0.1/testuser=riyaz&password=zarina");
st = c.createStatement();
st.executeUpdate (create);
st.close();
c.close();
}
catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
}
}

kindly provide me the rectification for this problm.moreover i dont have mysql driver in my system.thank u .

regards
riyaz
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
well iam not using tomcat as iam working on simple java applications.so i need
help reg the above.thanks
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'm not sure. Sorry, I didn't complete ly understand your explaination. To help reduce the laguage barrier, please try to UseRealWords.

Firstly (and I only recommend doinmg this in the current test, not always) remove all of the try/catch blocks and make the main method throw Exceptions. Having empty try/catch blocks is never a good idea and it may be hiding the real cause. Did you add the mysql JAR to your classpath?

It looks like you downloaded and unzipped the 'installer' and added this to the classpath instead of the myconnector-java-3.1.8-bin.jar file. This file should be displayed on the classpath. You do not need the other entry.

eg set CLASSPATH=c:\mysql\myconnector-java-3.1.8-bin.jar;%CLASSPATH%

Dave
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
to dave: first off all i apolozise for repeating the question. sorry dave.i would see that i dont repeat this mistake.

thanks for sugessions. i would try ur tips and catch u back.

riyaz
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

No problems, I hope you sort the problem out. (except that 'ur' and 'u' are still abbreviations )
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
hello dave , here iam back with clear description. i tried the following after ur reply :


1.F:\riyaz\work> set CLASSPATH=F:\riyaz\tech\dwnld\mysql\connector\
mysql-connector-java-3.1.11-bin.jar;%CLASSPATH%

i also did the same environmental variables.(connector is the unzipped folder)

2.when i tried again with my code i got this error :
class not found error : J (j is the .java file)

3. moreover i hav doubt reg the syntax :
Class.forName("com.mysql.jdbc.Driver");
is there any procedure to configure MYSQL server inorder to recogonize the above class.it seems that there is no relation b/w mysql server & the class specified.

4.Connection c=DriverManager.getConnection("jdbc:mysql//localhost:3306/mysql?user=root&password=zarina");
is that a correct syntax.

dave ,i think the problm is with loading the driver and registering with
drivermanager.kindly reply .thank u .
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
hello dave , here iam back with clear description. i tried the following after ur reply :


1.F:\riyaz\work> set CLASSPATH=F:\riyaz\tech\dwnld\mysql\connector\
mysql-connector-java-3.1.11-bin.jar;%CLASSPATH%

i also did the same environmental variables.(connector is the unzipped folder)

2.when i tried again with my code i got this error :
class not found error : J (j is the .java file)

3. moreover i hav doubt reg the syntax :
Class.forName("com.mysql.jdbc.Driver");
is there any procedure to configure MYSQL server inorder to recogonize the above class.it seems that there is no relation b/w mysql server & the class specified.

4.Connection c=DriverManager.getConnection("jdbc:mysql//localhost:3306/mysql?user=root&password=zarina");
is that a correct syntax.

dave ,i think the problm is with loading the driver and registering with
drivermanager.kindly reply .thank u .

regards
riyaz
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
well dave when i posted first there was an url problem telling to go back & post again..thats why i have same two replies. moreover i will definetly avoid abbreviation like "u" & so...ok .

regards
riyaz
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You need to add the current directory to you classpath so that it can find 'J' -
set CLASSPATH=.;.... blah blah blah

the '.' is the current directory.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

3. Class.forName("com.mysql.jdbc.Driver");
is there any procedure to configure MYSQL server inorder to recogonize the above class.it seems that there is no relation b/w mysql server & the class specified.

Yes there is a trick here. If you want to look it up, it's called a 'static initializer'. It's a block of code that only gets fired up once when the Class is loaded. This allows the Driver to register itself with the DriverManager (and so that you do not have to)

4.Connection c=DriverManager.getConnection ("jdbc:mysql//localhost:3306/mysql?user=root&password=zarina");
is that a correct syntax.

Maybe, I think there is a colon missing. I'll go check.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Your JDBC-URL needs another colon, after the first 'mysql', before the '//localhost' ie like it was in your original post.
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
well dave - hurrah !!! i did it..thanks for getting me boosted with my first challenge. the entire problem was on specifying the current directory.well you said to use static initializer. can you just give me the code reg that , how
to load the com.mysql.jdbc.Driver.coz when i tried it throwed error.

static { set CLASSPATH=f:\riyaz\work;F:\riyaz\tech\dwnld\mysql\connector\mysql-connector-java-3.1.11-bin.jar;%CLASSPATH% }

riyaz
scjp 1.4
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
dave ,

this for forum sake :

see many peoples face problems in loading & connection.so if you post an article on JDBC FAQ reg connections & loading for different databases like oracle, mysql etc, it would be useful for u to direct them to that link.so everyone would be benifitted. this is my sugession & request.thank u.

riyaz
scjp 1.4.
riyaz udeen
Ranch Hand

Joined: Sep 11, 2005
Posts: 66
dave i want to know the loading & connection issues and codings for oracle.
kindly provide me with getconnection, setting class path & class.forname statements, loading the oracle driver details for oracle databse. thank u.

riyaz
scjp 1.4
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It depends on the driver, but typically you'll use whatever the most recent driver JAR is provided (we're using ojdbc14.jar), the class name is "oracle.jdbc.OracleDriver" and the JDBC-URL should be (like) "jdbc:oracle:thin:@servername:1521:SID"
[ October 20, 2005: Message edited by: David O'Meara ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sql exception thrown