welcome

通过自己写的JDBC工具类实现增删改查

 1 package day28_02;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.sql.Statement;
 8 import java.util.ResourceBundle;
 9 
10 public class JdbcUtil {
11     static final String CLASSDRIVER;
12     static final String URL;
13     static final String NAME;
14     static final String PASSWORD;
15 
16     static {
17         ResourceBundle rb = ResourceBundle.getBundle("Config");
18 
19         CLASSDRIVER = rb.getString("classDriver");
20         URL = rb.getString("url");
21         NAME = rb.getString("name");
22         PASSWORD = rb.getString("password");
23         try {
24             Class.forName(CLASSDRIVER);
25         } catch (ClassNotFoundException e) {
26             // TODO Auto-generated catch block
27             e.printStackTrace();
28         }
29     }
30 
31     public static Connection getConnection() throws SQLException {
32 
33         
34         return DriverManager.getConnection(URL, NAME, PASSWORD);
35 
36         
37     }
38 
39     public static void closeResource(Connection conn, Statement stmt, ResultSet rs) {
40         closeResult(rs);
41         closeStatement(stmt);
42         closeConnection(conn);
43     }
44 
45     private static void closeConnection(Connection conn) {
46         if (conn != null) {
47             try {
48                 conn.close();
49             } catch (SQLException e) {
50                 e.printStackTrace();
51             }
52         }
53 
54         conn = null;
55     }
56 
57     private static void closeStatement(Statement stmt) {
58         if (stmt != null) {
59             try {
60                 stmt.close();
61             } catch (SQLException e) {
62                 e.printStackTrace();
63             }
64         }
65 
66         stmt = null;
67 
68     }
69 
70     private static void closeResult(ResultSet rs) {
71         if (rs != null) {
72             try {
73                 rs.close();
74             } catch (SQLException e) {
75                 e.printStackTrace();
76             }
77         }
78 
79         rs = null;
80     }
81 }



以上是JDBC工具类

 

  以下代码执行增删改查操作

 

  1 package day28_02;
  2 
  3 import java.sql.Connection;
  4 import java.sql.PreparedStatement;
  5 import java.sql.ResultSet;
  6 import java.sql.SQLException;
  7 
  8 import org.junit.Test;
  9 
 10 public class JdbcUtilTest {
 11     /**
 12      * 查询全部信息
 13      */
 14     @Test
 15     public void test() {
 16         // 驱动,连接
 17         Connection conn = null;
 18         ResultSet rs = null;
 19         PreparedStatement pst = null;
 20         try {
 21             conn = JdbcUtil.getConnection();
 22 
 23             // 编写sql
 24             String sql = "SELECT * FROM a_students";
 25 
 26             // 语句执行者
 27             pst = conn.prepareStatement(sql);
 28 
 29             // 设置参数
 30 
 31             // 执行,获取结果
 32             rs = pst.executeQuery();
 33 
 34             // 处理结果
 35             while (rs.next()) {
 36                 System.out.println(
 37                         rs.getString(1) + "---" + rs.getString(2) + "---" + rs.getString(3) + "---" + rs.getString(4));
 38             }
 39         } catch (SQLException e) {
 40             // TODO Auto-generated catch block
 41             e.printStackTrace();
 42         }
 43 
 44         // 释放资源
 45         JdbcUtil.closeResource(conn, pst, rs);
 46     }
 47 
 48     /**
 49      * 修改信息
 50      */
 51     @Test
 52     public void update() {
 53         Connection conn = null;
 54         PreparedStatement pst = null;
 55 
 56         try {
 57             // 获得连接
 58             conn = JdbcUtil.getConnection();
 59             // 编写sql
 60             String sql = "UPDATE a_students SET s_id = ? WHERE s_id = ?";
 61             // 获得语句执行者
 62             pst = conn.prepareStatement(sql);
 63             // 设置变量
 64             pst.setString(1, "owen17082413");
 65             pst.setString(2, "owen17082414");
 66 
 67             int i = pst.executeUpdate();
 68             // System.out.println(1);
 69             // System.out.println(i);
 70             if (i == 1) {
 71                 System.out.println("success");
 72             }
 73         } catch (SQLException e) {
 74             e.printStackTrace();
 75         } finally {
 76             JdbcUtil.closeResource(conn, pst, null);
 77         }
 78     }
 79 
 80     /**
 81      * 添加数据
 82      */
 83     @Test
 84     public void insert() {
 85         Connection conn = null;
 86         PreparedStatement pst = null;
 87         try {
 88             // 获取连接
 89             conn = JdbcUtil.getConnection();
 90             // 编写sql
 91             String sql = "INSERT INTO a_students VALUES(?,?,?,?)";
 92 
 93             // 创建语句执行者
 94             pst = conn.prepareStatement(sql);
 95 
 96             // 设置参数
 97             pst.setString(1, "owen17082415");
 98             pst.setString(2, "吴奇隆");
 99             pst.setString(3, "黄淑宁");
100             pst.setString(4, "15636316713");
101 
102             // 执行sql,获取结果
103             int i = pst.executeUpdate();
104 
105             // 处理结果
106             if (i == 1) {
107                 System.out.println("success");
108             } else {
109                 System.out.println("fail");
110             }
111 
112         } catch (SQLException e) {
113             e.printStackTrace();
114         } finally {
115             JdbcUtil.closeResource(conn, pst, null);
116         }
117     }
118 
119     /**
120      * 删除数据
121      */
122     @Test
123     public void delete() {
124         Connection conn = null;
125         PreparedStatement pst = null;
126 
127         try {
128             // 获取连接
129             conn = JdbcUtil.getConnection();
130 
131             // 编写sql
132             String sql = "DELETE FROM a_students WHERE s_id = ?";
133 
134             // 创建语句执行者
135             pst = conn.prepareStatement(sql);
136 
137             // 设置参数
138             pst.setString(1, "owen17082415");
139 
140             // 执行sql,获得结果
141             int i = pst.executeUpdate();
142 
143             // 处理结果
144             if (i == 1) {
145                 System.out.println("success");
146             } else {
147                 System.out.println("fail");
148             }
149 
150         } catch (SQLException e) {
151             e.printStackTrace();
152         } finally {
153             JdbcUtil.closeResource(conn, pst, null);
154         }
155     }
156 }

 

posted @ 2017-08-24 20:20  hsn  阅读(545)  评论(0编辑  收藏  举报