Java: mysql-connector-java

java -cp ".;./mysql-connector-java-8.0.28.jar" zak.Zak

https://stackoverflow.com/questions/2839321/connect-java-to-a-mysql-database

 

package zak;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class Zak{

  public static void main(String[] args) throws SQLException, ClassNotFoundException{
    Class.forName("com.mysql.cj.jdbc.Driver");
    String url = "jdbc:mysql://localhost:3306/java?useSSL=true&serverTimezone=GMT";
    String user = "java";
    String password = "java";
    Connection connection = DriverManager.getConnection(url, user, password);
    Statement statement = connection.createStatement();

    String drop = "drop table if exists wares";
    statement.execute(drop);

    String create = "create table wares (id int, name varchar(32), price double, introduce text)";
    statement.execute(create);

    String insert = "insert into wares (id, name, price, introduce) values (5, 'rtyu', 55, 'introduce')";
    statement.execute(insert);

    statement.close();
    connection.close();
    System.out.println(url);
  }
}

 

posted @ 2022-04-04 22:00  ascertain  阅读(192)  评论(0编辑  收藏  举报