Java web学习笔记01--JDBC

最近跟着黑马程序班学习JDBC操作,按照视频写了一个jdbc连接mysql数据库的操作,代码如下:
rt java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * JDBC快速入门
 */
public class JdbcDemo01 {
    public static void main(String[] args) throws Exception {
        //1.导入jar包
        //2.注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //3.获取数据库连接对象
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","123");
        //4.定义SQL语句
        String sql = "update stu set name = '张三丰' where id = 1001";
        //5.获取执行SQL的对象 Statement
        Statement stmt = conn.createStatement();
        //6.执行SQL
        int count = stmt.executeUpdate(sql);
        //7.处理结果
        System.out.println(count);
        //8.释放资源
        stmt.close();
        conn.close();
    }
}

在运行时出现下列错误:
Loading class com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver’. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

Exception in thread “main” java.sql.SQLException: The server time zone value ‘�й���׼ʱ��’ is unrecognized or represents more than one time zone.You must configure either the server or JDBC driver (via the ‘serverTimezone’ configuration property) to use a more specifc time zone value if you want to utilize time zone support.

第一个问题是是说class.forname中的内容应该改为
“com.mysql.cj.jdbc.Driver”
第二个问题好像是关于时区支持需要配置驱动程序什么的。在查阅网上资料后发现是时区错误的问题,采用了博客(https://blog.csdn.net/Wei_NiZi/article/details/81509822)的方法,将"jdbc:mysql://localhost:3306/db1"改为"jdbc:mysql://localhost:3306/db1?serverTimezone=GMT%2B8"后问题得到解决

你可能感兴趣的:(学习笔记)