1

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;

    }
1
  • What does the documentation state about drivers? What have you tried? Commented Dec 11, 2016 at 16:48

2 Answers 2

1

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.

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

1 Comment

Oh thank you , sir. I added Class.forName("com.mysql.jdbc.Driver"); before getConnection method and it works . Thank you again.
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.