(一)复制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 is
com.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();
}
}