0

I try to insert data into MySQL using Java and I have error all the time. I use this video to understand how to do to the connection.

This is my Java code:

import java.sql.*;

public class Main {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Connection conn = null;
    //DriverManager.registerDriver(new com.mysql.jdbc.Driver ());
    try {
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/try1-progrem", "root", "123456");
        Statement st = conn.createStatement();

        String username = "kola";
        String password = "24688642";

        String insert = "INSERT INTO login  VALUES ('"+username+"','"+password+"')";

        st.executeUpdate(insert);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

Get the error:

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/try1-progrem at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at Main.main(Main.java:11)

And here to SQL image: sql data:
sql data

** I learned from YouTube how to make the connection, if there is a good guild, I will glad to take him.

*EDIT1: build

idan
  • 69
  • 1
  • 7

1 Answers1

0

To fix this you should follow the steps below:

  1. You need to download the correct ConnectorJ from the link below.

  2. After downloading the correct installer for your machine you will need to run the installer to install only the correct jdbc connector for you. (A series of prompts will lead you to selecting the connector and not all the MySQL downloads)

  3. Then you can put the .jar file (connectorJDBC file) into your classpath appropriately and this should fix your problem as I encountered a similar problem while trying to connect to a SQL server with java.

Hopefully this fixes your problem, comment if it doesn't. :)

ConnectorJ Download

sloughy
  • 78
  • 4