JavaIDEA连接MySQL教程及常见错误解决

(一)复制jar包

将对应MySQL版本的jar包,复制libs目录中(libs是自己创建的)
在这里插入图片描述

(二)添加library

复制成功后,右击libs,选择Add to Library…

在这里插入图片描述

(三)注册驱动

Class.forName("com.mysql.jdbc.Driver");

注意,此处可能会出现问题:

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.

将代码改成这样既可:

Class.forName("com.mysql.cj.jdbc.Driver");

(四)获取数据库的连接对象

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");

此处可能会出现错误:
错误一:

** BEGIN NESTED EXCEPTION ** javax.net.ssl.SSLException MESSAGE: closing inbound before receiving peer’s close_notify STACKTRACE:
javax.net.ssl.SSLException: closing inbound before receiving peer’s
close_notify
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:129)
at java.base/sun.security.ssl.Alert.createSSLException(Alert.java:117)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:308)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:264)
at java.base/sun.security.ssl.TransportContext.fatal(TransportContext.java:255)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:645)
at java.base/sun.security.ssl.SSLSocketImpl.shutdownInput(SSLSocketImpl.java:624)
at com.mysql.cj.protocol.a.NativeProtocol.quit(NativeProtocol.java:1312)
at com.mysql.cj.NativeSession.quit(NativeSession.java:182)
at com.mysql.cj.jdbc.ConnectionImpl.realClose(ConnectionImpl.java:1750)
at com.mysql.cj.jdbc.ConnectionImpl.close(ConnectionImpl.java:720)
at org.mybatis.generator.config.Context.closeConnection(Context.java:535)
at org.mybatis.generator.config.Context.introspectTables(Context.java:468)
at org.mybatis.generator.api.MyBatisGenerator.generate(MyBatisGenerator.java:222)
at org.mybatis.generator.api.ShellRunner.main(ShellRunner.java:117)

** END NESTED EXCEPTION **

解决方法:

原因应该是MySQL的版本太高导致的。
设置useSSL为false既可

错误二:

The server time zone value ‘Öйú±ê׼ʱ¼ä’ is unrecognized or
represents more than one time zone.

原因是MySQL的时区不统一

键盘上这个键
在这里插入图片描述

  • R 输入cmd

在这里插入图片描述
进入命令行

在这里插入图片描述

在这里插入图片描述

输入

show variables like '%time_zone%';

查看时区,如果是System, 则进行修改

set global time_zone='+8:00';

就该为正确的既可。

当然还有另外一种方法:

修改代码中的URL:

Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1" +
                "?serverTimezone=GMT%2B8&useSSL=false", "root", "root");

(五)定义sql语句

String sql = "update students set age =10 where id ='1'";

(六)获取sql 的对象

Statement stmt = connection.createStatement();

(七)执行sql语句

int count = stmt.executeUpdate(sql);

(八)打印结果

System.out.println(count);

(九)释放资源

stmt.close();
connection.close();

完整代码:

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

public class demo01_jdbc {

    public static void main(String[] args) throws Exception{
        //1. 复制jar包到libs 目录下右击libs 点击add as lib...
        //2. 注册驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //3. 获取数据库的连接对象
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1" +
                "?serverTimezone=GMT%2B8&useSSL=false", "root", "root");
        //4. 定义sql语句
        String sql = "update students set age =10 where id ='1'";//?serverTimezone=GMT%2B8
        //5.获取sql 的对象
        Statement stmt = connection.createStatement();
        //6.执行sql语句
        int count = stmt.executeUpdate(sql);
        //7.打印结果
        System.out.println(count);
        //8.释放资源
        stmt.close();
        connection.close();
    }
}
  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值