问题
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());
}
}
}
回答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.
回答2:
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());
}
}
}
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/28286467/connection-to-a-jdbc-mysql-database