简单的JDBC的增删改查操作->抽取了基类,附源码

时间:2020-12-12 22:48:57

1.主程序

 1 package com.xyyz.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 
 8 import com.xyyz.utils.JDBCUtils;
 9 
10 public class JDBCDemo {
11 
12     public static void main(String[] args) throws Exception {
13         query();
14         insert();
15         query();
16         update();
17         query();
18         delete();
19         query();
20     }
21 
22     /**
23      * 查询代码
24      * 
25      * @throws Exception
26      */
27     public static void query() throws Exception {
28         Connection connection = JDBCUtils.getConnection();
29         // 获取数据库连接
30         String sql = "select * from jdbctestdata";
31         // 获取预编译对象
32         PreparedStatement prepareStatement = connection.prepareStatement(sql);
33         // 执行sql语句
34         ResultSet resultSet = prepareStatement.executeQuery();
35         // 遍历打印sql语句
36         while (resultSet.next()) {
37             System.out.println(resultSet.getInt(1) + "--" + resultSet.getString(2));
38         }
39         // 关闭释放资源
40         JDBCUtils.closeAll(resultSet, prepareStatement, connection);
41     }
42 
43     /**
44      * 插入数据
45      */
46     public static void insert() throws Exception {
47         // 获取数据库连接
48         Connection connection = JDBCUtils.getConnection();
49         String sql = "insert into jdbctestdata value(?,?,?)";
50         // 获取预编译对象
51         PreparedStatement prepare = connection.prepareStatement(sql);
52         prepare.setString(1, "6");
53         prepare.setString(2, "小白");
54         prepare.setString(3, "30");
55         int i = prepare.executeUpdate();
56         System.out.println("i=" + i);
57         // 关闭释放资源
58         JDBCUtils.closeAll(null, prepare, connection);
59     }
60 
61     /**
62      * 更新数据
63      */
64     public static void update() throws Exception {
65         
66         // 获取数据库连接
67         Connection connection = JDBCUtils.getConnection();
68         String sql = "update jdbctestdata set name=? , age=? where id=?";
69         // 获取预编译对象
70         PreparedStatement prepare = connection.prepareStatement(sql);
71         prepare.setString(1, "小红");
72         prepare.setString(2, "20");
73         prepare.setString(3, "6");
74         int i = prepare.executeUpdate();
75         System.out.println("i=" + i);
76         // 执行sql语句
77         JDBCUtils.closeAll(null, prepare, connection);
78     }
79 
80     /**
81      * 删除数据
82      */
83     public static void delete() throws Exception {
84         // 获取数据库连接
85         Connection connection = JDBCUtils.getConnection();
86         String sql = "delete from jdbctestdata where id = ?";
87         // 获取预编译对象
88         PreparedStatement prepare = connection.prepareStatement(sql);
89         prepare.setString(1, "6");
90         int i = prepare.executeUpdate();
91         System.out.println("i=" + i);
92         // 执行sql语句
93         JDBCUtils.closeAll(null, prepare, connection);
94     }
95 
96 }

2.工具类

 1 package com.xyyz.utils;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.sql.Statement;
 9 import java.util.Objects;
10 
11 public class JDBCUtils {
12     // 连接数据需要的参数
13     private static String url = "jdbc:mysql:///jdbctestdata";
14     private static String user = "root";
15     private static String password = "root";
16 
17     static {
18         // 注册驱动
19         try {
20             Class.forName("com.mysql.jdbc.Driver");
21         } catch (ClassNotFoundException e) {
22             System.out.println("没有加masql的jar包");
23         }
24 
25     }
26 
27     /**
28      * 获取connection
29      */
30     public static Connection getConnection() throws SQLException {
31         return DriverManager.getConnection(url, user, password);
32     }
33 
34     // 关闭资源的方法
35     public static void closeAll(ResultSet resultSet, PreparedStatement prepareStatement, Connection connection) {
36         // 关闭资源
37         if (resultSet != null) {
38             try {
39                 resultSet.close();
40             } catch (Exception e) {
41                 e.printStackTrace();
42             }
43             resultSet = null;
44         }
45         if (prepareStatement != null) {
46             try {
47                 prepareStatement.close();
48             } catch (Exception e) {
49                 e.printStackTrace();
50             }
51             prepareStatement = null;
52         }
53         if (connection != null) {
54             try {
55                 connection.close();
56             } catch (Exception e) {
57                 e.printStackTrace();
58             }
59             connection = null;
60         }
61     }
62 }

http://download.csdn.net/detail/u010798073/9888920