javaJDBC连接mysql8.0的坑

由于mysql升级,Java所导入的jar包也不一样了
下载地址添加链接描述

       		Class.forName("com.mysql.cj.jdbc.Driver");//mysql8.0
	//        Class.forName("com.mysql.jdbc.Driver");   //Mysql 5.0
			Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root", "");//mysql8.0
        //  Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");  //mysql5.0
package cn.Dong;

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

public class Demo01Jdbc {
    public static void main(String[] args) throws Exception {
        //1.导入驱动jar包
        //2.注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
//        Class.forName("com.mysql.jdbc.Driver");   Mysql 8.0以下
        //3.获取数据库连接对象
        //java连接mysql8.0
        Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC", "root", "");
        //  Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
        //4.定义sql语句
        String sql="update student set age=30 where id=1";
        //5.获取执行sql的对象 Statement
        Statement stmt=conn.createStatement();
        //6.执行sql
        int count=stmt.executeUpdate(sql);
        //7.处理结果
        System.out.println(count);
        //8.释放资源
        stmt.close();
        conn.close();
    }
}

你可能感兴趣的:(java)