jdbc connect to mysql database

M

Marcus Krieger

I am using JBuilder and I would like to connect to a mysql database.
I was able to connect to a oracle database, however it does not work with
mysql!
I downloaded the newest drivers from the mysql webseite and put them into my
java/bin directory.
As I don't know exactly which java folder Jbuilder is using I have put them
in both folder - my java folder and the java folder in jbuilder.
Additionally, I tried to import the file from Jbuilder, and put them into
"Project", "User Home" and "Jbuilder".

Whatever that exactly does, with oracle it had worked.
Using mysql, I get the following error message:

driver:com.mysql.jdbc.Driver url:jdbc:mysql:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver

That's my code:
[..]
jdbc_driver="com.mysql.jdbc.Driver";
db_login = "root";
db_password = "";
db_url = "jdbc:mysql://localhost:3306/dbname";
try
{
Class.forName(jdbc_driver);
}
[..]
DriverManager.getConnection(db_url, db_login, db_password);

Any ideas, what I am doing wrong here?

Thanks,

Marcus
 
C

Collin VanDyck

Marcus said:
driver:com.mysql.jdbc.Driver url:jdbc:mysql:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
java.sql.SQLException: No suitable driver
java.sql.SQLException: No suitable driver

Most likely the connector jar is not on your classpath. If you cannot
figure out where to place it, it SHOULD work if you place it in your
%SYSTEM_JRE%/lib/endorsed directory.
 
I

Ian T

Marcus Krieger wrote:

If you Read the first page of The Fine Documentation that comes with the
official jdbc driver (www.mysql.com), you will find a code example that
will work immediately. My java implementation would only work with the
newInstance code below.

Something like:

try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception ex) { // handle the error }
java.sql.Connection c =
DriverManager.getConnection("jdbc:mysql://localhost/recruitment?user=root&password=");
java.sql.ResultSet r = s.executeQuery("SELECT * FROM Candidates");
while ( r.next())
{
System.out.println(r.getString("fullname"));
System.out.println(r.getString("phone"));
}
s.close();
c.close();


Ian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top