我是靠谱客的博主 自信皮皮虾,这篇文章主要介绍JDBC程序实现完整步骤1、加载驱动2.连接的数据库信息(MySQL8)3.获取数据库对象4.执行数据库对象5.写sql语句6.执行sql对象7.关闭连接8.完整代码9.测试结果,现在分享给大家,希望可以做个参考。
目录
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.* 版本
复制代码
1Class.forName("com.mysql.jdbc.Driver");
1.2 MySQL 8 版本
复制代码
1Class.forName("com.mysql.cj.jdbc.Driver");
2.连接的数据库信息(MySQL8)
需要连接的数据库的名字,用户名,密码
复制代码
1
2
3String url="jdbc:mysql://localhost:3306/demo03?serverTimezone=Asia/Shanghai"; String name="root"; String password="813437";
3.获取数据库对象
复制代码
1
2Connection connection = DriverManager.getConnection(url, name, password);
4.执行数据库对象
复制代码
1
2Statement statement = connection.createStatement();
5.写sql语句
复制代码
1
2String sql="update readers set name='lw' where reader_id='34103'";
6.执行sql对象
复制代码
1statement.execute(sql);
7.关闭连接
复制代码
1
2
3resultSet.close(); statement.close(); connection.close();
8.完整代码
复制代码
1
2
3
4
5
6
7
8
9
10
11
12public 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.测试结果
最后
以上就是自信皮皮虾最近收集整理的关于JDBC程序实现完整步骤1、加载驱动2.连接的数据库信息(MySQL8)3.获取数据库对象4.执行数据库对象5.写sql语句6.执行sql对象7.关闭连接8.完整代码9.测试结果的全部内容,更多相关JDBC程序实现完整步骤1、加载驱动2.连接内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复