1

I'm trying to connect to my msql database, but keep getting the error: No suitable driver found for jdbc:mysql://localhost:3306/test3.

try{

     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test3" , "root", "julius");

        Statement statement = (Statement) con.createStatement();

        String insert = "INSERT INTO testtable VALUE ('Guy' , '0156421";

        statement.executeUpdate(insert);

    }catch(Exception e){

    System.out.println("ERROR: " + e.getMessage());

    }

I have not installed any mysql connector, but since I could import java.sql.Connection;, java.sql.DriverManager;, java.sql.Statement; I guess that wasn't needed.

Jullix993
  • 115
  • 10

1 Answers1

2

You have to register a JDBC driver before creating a connection. In your case it is MySQL driver. Download mysql-connector jar and then add it to your classpath and then run your application like the below code

try{
  //Register JDBC driver
  Class.forName("com.mysql.jdbc.Driver");

  //Open a connection
  Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);

  //Create a statement
  Statement stmt = conn.createStatement();

  //Execute the query
  String insert = "INSERT INTO testtable VALUE ('Guy' , '0156421')";
  stmt.executeUpdate(insert);
}
catch(Exception e){
  System.out.println("Exception is " + e.getMessage());
}
Ronald Randon
  • 1,121
  • 3
  • 13
  • 19