Reputation: 179
I'm working on OS X, Eclipse, Java 8 and MySQLWorkBench.
I realize with the last a database (can I say schema?), named "mydb" located at localhost:3306 (I don't know exactly what it means)...
Now I would like to connect via a java program to this db. I'm trying with
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb");
It follows the error message
Exception in thread "main" java.sql.SQLException:
No suitable driver found for jdbc:mysql://localhost:3306/mydb
Can someone tell me what's wrong?
Upvotes: 0
Views: 825
Reputation: 477
After download and adding the Connector/J to your build path like @Jacques said.
Try this code:
try {
// Load the JDBC driver
@SuppressWarnings("rawtypes")
Class driver_class = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) driver_class.newInstance();
DriverManager.registerDriver(driver);
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", getDbUser(), getDbPassword());
return conn;
} catch (Exception e) {
// TODO: handle exception
}
Where getDbUser() is your database username and getDbPassword() is your database password.
Upvotes: 0
Reputation: 871
You need to download the jdbc driver and add it to your project
Example on how to add the driver in eclipse
Also you need to include the username and password in the connection statement and make sure you call the Class for name to get the driver loaded
Class.forName("com.mysql.jdbc.Driver");
DriverManager.getConnection(DB_URL,USER,PASS);
Upvotes: 4
Reputation: 3591
You haven't added required library to connect java application with MySql database.you can download it form here.After once you get downloaded , just extract zip wherever you want.And add it into your project library folder. Right click on project goto property -> java build path -> add external jar .
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb","userName","userPass");
where userName
is your database username and userPass
is your password corresponding to that user.
Upvotes: 0