0
votes

I am trying to write a java program connecting to a MySQL database. But I am getting this error:

No suitable driver found for jdbc:mysql//localhost:3306/test

I have already installed mysql-connector-java.5.1.23-bin.jar. I am still getting this error.

Here is my program:

package database_practice;

import java.sql.*;

public class CreateConnection {
        private Connection conn = null;

        public static void main(String args[]) {
            CreateConnection conn = new CreateConnection();
                conn.getConnection();
        }


        public Connection getConnection() {
            try {
                String url = "jdbc:mysql//localhost:3306/test";
                String username = "root";
                String password = "admin1234";

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(url, username, password);
            System.out.println("Database Created...!!!");
        }

        catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error occured while connecting to database");
        }
            return conn;
    }
}
1

1 Answers

12
votes

Your connection string is wrong. This:

"jdbc:mysql//localhost:3306/test"

should be:

"jdbc:mysql://localhost:3306/test"

Note the colon after "mysql".