wood burning stoves 2.0*
The moose likes JDBC and the fly likes unable to establish a connection betwen JDBC and  MySQL server using a java class. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "unable to establish a connection betwen JDBC and  MySQL server using a java class." Watch "unable to establish a connection betwen JDBC and  MySQL server using a java class." New topic
Author

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

sagar podilapu
Greenhorn

Joined: Feb 25, 2009
Posts: 13
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.


If nothing goes right
go left
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
How are you adding the JDBC driver's jar file to the classpath?


Ping & DNS - my free Android networking tools app
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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.


My Blog: Down Home Country Coding with Scott Selikoff
sagar podilapu
Greenhorn

Joined: Feb 25, 2009
Posts: 13
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

This is one of those often rare cases where the exception text is extremely useful
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3704
    
    5

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Feb 25, 2009
Posts: 13
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

Joined: Feb 25, 2009
Posts: 13
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
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: May 20, 2012
Posts: 18
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

Joined: May 22, 2012
Posts: 867
    
    5
> 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.



 
Don't get me started about those stupid light bulbs.
 
subject: unable to establish a connection betwen JDBC and MySQL server using a java class.