目录
1、加载驱动
固定写法
1.1 MySQL 5.* 版本
Class.forName("com.mysql.jdbc.Driver");
1.2 MySQL 8 版本
Class.forName("com.mysql.cj.jdbc.Driver");
2.连接的数据库信息(MySQL8)
需要连接的数据库的名字,用户名,密码
String url="jdbc:mysql://localhost:3306/demo03?serverTimezone=Asia/Shanghai";
String name="root";
String password="813437";
3.获取数据库对象
Connection connection = DriverManager.getConnection(url, name, password);
4.执行数据库对象
Statement statement = connection.createStatement();
5.写sql语句
String sql="update readers set name='lw' where reader_id='34103'";
6.执行sql对象
statement.execute(sql);
7.关闭连接
resultSet.close();
statement.close();
connection.close();
8.完整代码
public void demo01() throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/demo03?serverTimezone=Asia/Shanghai";
String name="root";
String password="813437";
Connection connection = DriverManager.getConnection(url, name, password);
Statement statement = connection.createStatement();
String sql="update readers set name='lw' where reader_id='34103'";
statement.execute(sql);
statement.close();
connection.close();
9.测试结果