0

I am trying to connect to a JDBC MySQL database using Java, but I am getting the following error when I try to run my program:-

No suitable driver found for a9442ca6-992c-411b-8bda-a42f00a0ab2e.mysql.sequelizer.com

I downloaded the following Jar file and added it to my project:

 mysql-connector-java5.1.34-bin.jar 

I'm not sure if I need to add some kind of 'import' statement in my declaration maybes? Any help would be much appreciated.

My code is as follows:-

package dvddblibrary; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import javax.swing.JOptionPane; import java.util.Scanner; public class DBConnect { /** * @param args the command line arguments */ public static void main(String[] args) { try { String host = "xxx"; String uName = "xxx"; String uPass = "xxx"; Connection con = DriverManager.getConnection(host, uName, uPass); } catch (SQLException err) { System.out.println(err.getMessage()); } } } 
3
  • 2
    Please, could you at least help us by posting your current code. Commented Feb 2, 2015 at 20:49
  • Did you run the program in command line or in an IDE like Eclipse? If you're running it in command line, you've to include the jar in command for both compilation and execution part Commented Feb 2, 2015 at 20:50
  • What version of Java are you using? Commented Feb 2, 2015 at 21:23

4 Answers 4

1

You need to register the driver first, which is a side effect of loading the class. Try

Class.forName("com.mysql.jdbc.Driver"); 

MySQL even say you need to do

Class.forName("com.mysql.jdbc.Driver").newInstance(); 

for some "broken Java implementations" but I never found that necessary. See http://dev.mysql.com/doc/connector-j/en/connector-j-usagenotes-connect-drivermanager.html.

Sign up to request clarification or add additional context in comments.

8 Comments

If using Java SE 7, this is not needed anymore.
At least when this is done and it produces a ClassNotFoundException we know that the true reason is that the jar is not on the classpath.
But that's not the problem... For me, looks more like OP did a wrong copy/paste of code and has the wrong class name in Class.forName.
But if you have a copy/paste error in Class.forName you get a ClassNotFounException whereas if you don't have it on the classpath or use Java 6 or before and omit the Class.forName() you will get the "no suitable driver" error message. Let's maybe wait for the OP to clarify or paste complete code. My answer was too early.
Thanks for your help. Sorry where do I put this line of code - Class.forName("com.mysql.jdbc.Driver").newInstance();
|
1

Try this :

package dvddblibrary; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnect { public static void main(String[] args) { try { String host = "xxx"; String uName = "xxx"; String uPass = "xxx"; Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection(host, uName, uPass); } catch (SQLException err) { System.out.println(err.getMessage()); } } } 

1 Comment

This code will get a compilation error because the try/catch block is not covering the "ClassNotFoundException" exception.
0

Download the file from [ http://dev.mysql.com/downloads/connector/j/ ]. Extract it and you will find a .jar file inside the lib folder. Copy this file into the lib/ folder of your project.

It may cause that problem.

Comments

0

There are two problems in your code;

1) You have to add this on the top of your try block;

 // Add this Class.forName("com.mysql.jdbc.Driver"); 

2) And your connection string is not valid, try a valid connection string in your host;

 String host = "jdbc:mysql://localhost/XXX"; 

The complete code is as below, (I have no mysql database by the way, just run this to get connection error, not "" error)

package dvddblibrary; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnect { public static void main(String[] args) { try { // Add this Class.forName("com.mysql.jdbc.Driver"); //String host = "xxx"; // this is not valid, update with a valid connection string String host = "jdbc:mysql://localhost/XXX"; String uName = "xxx"; String uPass = "xxx"; Connection con = DriverManager.getConnection(host, uName, uPass); } catch (SQLException err) { System.out.println(err.getMessage()); } catch (ClassNotFoundException e) { System.out.println(e.getMessage()); } } } 

And the output is as below, instead of getting your "No suitable driver found" error, I get a connection error (because I don't have a mysql database on localhost :) );

Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. 

If you just update the code, as far as your mysql database is up and your host is valid, this code will work fine.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.