Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to establish a connection betwen JDBC and MySQL server using a java class.

 
sagar podilapu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
I was trying to establish a connection between JDBC and MySQL server using a simple java program.
my code is..
"[b]
import java.sql.*;

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


Statement stmt = null;
ResultSet rs = null;

try {
Connection con =DriverManager.getConnection
("jdbc:mysql://localhost/test?" +
"user=monty&password=greatsqldb");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT foo FROM bar");


if (stmt.execute("SELECT foo FROM bar")) {
rs = stmt.getResultSet();
}


}
catch (SQLException ex){
// handle any errors
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally {


if (rs != null) {
try {
rs.close();
} catch (SQLException sqlEx) { }

rs = null;
}

if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlEx) { }

stmt = null;
}
}}}
[\b]"

it gives the following message...
SQLException: No suitable driver found for jdbc:mysql://localhost/test?user=monty&password=greatsqldb
SQLState: 08001
VendorError: 0


Please help me.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you adding the JDBC driver's jar file to the classpath?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java cannot connect to any database server on its own, it requires a JDBC jar driver specific to the database you are using. Luckily, MySQL like many others have this driver available on their website. You need to install it and add it to the class-path for it to work.
 
sagar podilapu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your reply.
i have done it.
I just wanna know what does the error mean?
it says"no suitable driver found."
what does this mean.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sagar podilapu wrote:
I just wanna know what does the error mean?
it says"no suitable driver found."
what does this mean.


And its meaning is same, when you call an URL "jdbc:mysql://localhost/test", DriverManager class, searches a suitable driver as per protocol used, here it searches for MySQL driver, and you know where Java searches for classes, either in JDK_HOME/lib directory or on CLASSPATH ..
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of those often rare cases where the exception text is extremely useful
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scott Selikoff wrote:This is one of those often rare cases where the exception text is extremely useful

Agreed, not only about this case, but any Java class which throws exception, the exception text and stack trace comes handy !!

Many time I control my temper when I found code with empty catch blocks, like
 
Scott Selikoff
author
Saloon Keeper
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I could agree but unfortunately most exception only give you a symptom of the problem and, unlike this case, not exactly what is wrong. Furthermore NullPointerExceptions are still extremely common and often tell you nothing except where the error occurred.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, but many times the understanding of exception messages grows with experience, I too found NPE extremely hard to tackle, but now a days, I can easily avoid them..

After all they are there to help us with best possible reason
 
sagar podilapu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey thanks guys for your reply.
I could not understand what you guys talking about.
I am very new to java concepts and I am directly on JDBC concepts.
Could some one help me to rectify the error please.

thank you.
I have done the classpath seting by copying the .jar file of Connector to ext folder of MySQL
 
sagar podilapu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one more query here.
how to solve the
com.mysql.jdbc.driver error.
I even tried
org.gjt.mm.mysql.Driver

I dont wanna post this in new thread as it is related to connecting to the server

THANK YOU.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sagar podilapu wrote:I have done the classpath seting by copying the .jar file of Connector to ext folder of MySQL

This doesn't sound as if you understand what the classpath is. Do you know what the "-classpath" switch of the java and javac executables do?
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sagar podilapu wrote:I have one more query here.
how to solve the
com.mysql.jdbc.driver error.


Which error ? You have to post here an actual error you got
 
Nikhil B. Singh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there..!!!

However it is too late to post reply in this thread to solve the problem for this user. Any way, I am sure that this thread will keep included in the similar thread facility. So it is necessary to post some reply in order to make it complete.

Well.. this error occurred because we had not put the appropriate jar file on this path, it is as follows:

C:\Program Files\Java\jre\lib\ext\mysql_connector_driver_file.jar (connector can be downloaded from www.mysql.com, and it may have different file name as per i have specified here for demo purposes only.)

So friends please add this file at this path and then re-compile your program and check your result.

Good Luck and wish you very happy programming.
 
Ivan Jozsef Balazs
Rancher
Posts: 979
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> no suitable driver found.

The driver class must be known to the connection Driver.
An example with the Oracle driver before trying to establish the connection.
The driver class registers itself with Driver, but only after you made it get loaded for example like this:
I used the dynamic way so that the driver itself is not needed at compile time, only at run time.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic