JDBC程序实现完整步骤

 

目录

1、加载驱动

1.1 MySQL 5.* 版本

1.2 MySQL 8 版本

2.连接的数据库信息(MySQL8)

3.获取数据库对象

4.执行数据库对象

5.写sql语句

6.执行sql对象

7.关闭连接

8.完整代码

9.测试结果


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.测试结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值