Reputation: 384
The error:
No suitable driver found for jdbc:mysql://localhost:3306
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
And here is my code.
public Connection getConnection() throws SQLException {
Connection conn;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "pass");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" , connectionProps);
System.out.println("Connected to database");
return conn;
}
Upvotes: 1
Views: 401
Reputation: 22
did you import it? import java.sql.*;
try putting in the top of class.
Also put this Class.forName("com.mysql.jdbc.Driver");
in before the DriverManager.getconnection
for class to identify the driver.
Upvotes: 1
Reputation: 5828
You need to register your driver before getting connection (if it's already registered, nothing will be done):
public Connection getConnection() throws SQLException {
String myDriver = "com.mysql.jdbc.Driver";
String myUrl = "jdbc:mysql://localhost:3306/";
Class.forName(myDriver);
Connection conn;
Properties connectionProps = new Properties();
connectionProps.put("user", "root");
connectionProps.put("password", "pass");
conn = DriverManager.getConnection(myUrl , connectionProps);
System.out.println("Connected to database");
return conn;
}
Upvotes: 1