从零开始学JDBC--1.4 模仿上节DDL过程写出DML过程
来源:互联网 发布:蓝洞游戏公司 知乎 编辑:程序博客网 时间:2024/06/07 06:30
参照上一节的DDL过程,照猫画虎写出来这个DML过程,其中包含增加新的一行,删除一行,修改一行
(代码太长啦!!放在最后面)
仔细观察代码,发现其实变化的地方只不过是那么几行,集中在sql语句那一行,所以这样写代码,累死程序员了!!
下一节内容,将对此作出功能抽取以简化代码的结构,防止重复工作的编写,简化程序员的工作
public class Demo2 { private String url = "jdbc:mysql://localhost:3306/day17"; private String user = "root"; private String password = "123"; @Test public void testInsert() { Connection conn = null; Statement stmt = null; try { // 1.驱动程序的注册 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 conn = DriverManager.getConnection(url,user,password); // 3.创建statment stmt = conn.createStatement(); // 4.准备sql String sql = "INSERT INTO student (NAME,gender)VALUES('齐八','女')"; // 5.执行sql语句,得到返回结果 int count = stmt.executeUpdate(sql); // 6、获取返回结果 System.out.println("本次执行共影响了:" + count + "行数据"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭) if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } } /** * 更新操作 */ @Test public void testUpdate() { Connection conn = null; Statement stmt = null; String name = "齐八"; String gender = "男"; try { // 1.驱动程序的注册 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 conn = DriverManager.getConnection(url,user,password); // 3.创建statment stmt = conn.createStatement(); // 4.准备sql String sql = "UPDATE student SET gender='"+gender+"' WHERE id='"+gender+"'"; // 5.执行sql语句,得到返回结果 int count = stmt.executeUpdate(sql); // 6、获取返回结果 System.out.println("本次执行共影响了:" + count + "行数据"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭) if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } } /** * 删除操作 */ @Test public void testDelete() { Connection conn = null; Statement stmt = null; String name = "齐八"; try { // 1.驱动程序的注册 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接 conn = DriverManager.getConnection(url,user,password); // 3.创建statment stmt = conn.createStatement(); // 4.准备sql String sql = "delete from student where NAME='"+name+"'"; // 5.执行sql语句,得到返回结果 int count = stmt.executeUpdate(sql); // 6、获取返回结果 System.out.println("本次执行共影响了:" + count + "行数据"); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally {// 7.关闭连接资源(注意顺序:后打开的先关闭) if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } }}
0 0
- 从零开始学JDBC--1.4 模仿上节DDL过程写出DML过程
- 从零开始学JDBC--1.3 一个完整的DDL过程(含代码)
- 从零开始学JDBC--1.8 CallableStatement-- 存储过程
- 从零开始学JDBC--1.5 DML代码抽取,结构简化
- oracle存储过程处理ddl与dml语句
- 我从零开始学黑莓开发的过程
- SQL DDL & DML from Oracle JDBC toturial
- 【转载】Java JDBC 执行 DDL 和 SQLJ 存储过程
- DDL.DML
- DML DDL
- DML&DDL
- DDL,DML
- ddl dml
- DDL,DML
- DDL DML
- DDL、DML
- DML DDL
- JDBC(3):实现DDL、DML、DQL和代码优化
- Java研发方向如何准备BAT技术面试答案(中)
- 线程的五大状态
- iOS-RSA加密-生成秘钥的使用
- iOS开发--适配iOS 10以及Xcode 8
- Servlet-HttpSession常用方法及状态
- 从零开始学JDBC--1.4 模仿上节DDL过程写出DML过程
- Logstash使用详解
- Android系统Recovery工作原理之使用update.zip升级过程分析(二)---update.zip差分包问题的解决
- 数正方形
- input number 标签
- RequireJs的使用和快速理解
- [C语言] leetcode 66. Plus One
- 开发基于全志V3的LinuxOS
- ios 10 开发-让我们一次性解决导航栏的所有问题