wood burning stoves 2.0*
The moose likes JDBC and the fly likes Connection to MySql Database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Connection to MySql Database" Watch "Connection to MySql Database" New topic
Author

Connection to MySql Database

Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
Hi all,
I tried to run the following program and get the error message

SQLState: 08001
Message : No suitable driver

Jar file ("mysql-connector-java-3.1.10-bin.jar") is already available in the same directory where I run this program.

I get this error message when I run the command
java -classpath "e:\java_app" MySQLTest

I also setup environment variable (System variable) as

Variable name = CLASSPATH
Variable value =
C:\Program Files\Java\jdk1.5.0\bin;E:\Java_App\mysql-connector-java-3.1.10-bin.jar

What is the cause of this problem?

Thanks in advance.

Regards,
Indika


Here is the source code

import java.sql.*;
import java.io.*;

class MySQLTest
{ public static void main (String args[])
{ try
{
Connection con = getConnection();
con.close();
}
catch (SQLException ex)
{ System.out.println ("SQLException:");
while (ex != null) {
System.out.println ("SQLState: "+ ex.getSQLState());
System.out.println ("Message: "+ ex.getMessage());
System.out.println ("Vendor: "+ ex.getErrorCode());
ex = ex.getNextException();
}
}
catch (IOException ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace ();
}
}

public static Connection getConnection()throws SQLException, IOException {

Properties props = new Properties();
String fileName = "MySQLProp.properties";
FileInputStream in = new FileInputStream(fileName);
props.load(in);

String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

return DriverManager.getConnection(url, username, password);
}

}



=========================
Here is the property file

jdbc.drivers=com.mysql.jdbc.jdbcDriver
jdbc.url=jdbc:mysql://localhost:3306/employee
jdbc.username=test
jdbc.password=test
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
Your "jdbc.driver" value is wrong

It should be

com.mysql.jdbc.Driver


This is the right driver string

Correct it. The program will run


Lack of will power has caused more failure than lack of intelligence or ability.
My Blog | Red5 Resources | Technology Update | Daily Technology Tips
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
Thanks for the reply. That is not the actual value for the drivers. We get the relevant value for "jdbc.driver" from the properties file that is "com.mysql.jdbc.jdbcDriver". So I think nothing wrong with that statement.
Sunil Kumar Gupta
Ranch Hand

Joined: Aug 26, 2005
Posts: 824
But the driver URL is


com.mysql.jdbc.Driver not com.mysql.jdbc.jdbcDriver


Correct it in ur properties file

when u download any driver for jdbc, why dont u

go through the documentation provided with the driver

u r writing the wrong URL for driver class


check this example


http://www.stardeveloper.com/articles/display.html?article=2003090401&page=1
Indika Hewage
Ranch Hand

Joined: May 18, 2004
Posts: 35
You are absolutely correct. Thanks for that.

Regards,
Indika
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection to MySql Database
 
Similar Threads
Multiple Drivers
Help finding database.properties file in Eclipse
NoClassDefFoundError re JDBC
jdbc-odbc bridge problem
Can not find driver?