jdbc java数据库连接 4)PreParedStatement接口 之 区别和例子

Statement 和 PreparedStatement 的区别:

  1)语句不同

    PreparedStatement需要预编译以及需要参数

  2)由于PreparedStatement有缓存区,所以效率更高

  3)由于PreparedStatement有缓存区,所以更安全,防止了注入(1=1)

 

  PreparedStatement接口(推荐使用):

  代码1:

 1     /**
 2      * 添加数据语句
 3      **/
 4 
 5     private static void Intinsert() {
 6 
 7         Connection conn = null;
 8         PreparedStatement stsm = null;
 9         try {
10             // 1:调用工具类获取连接
11             conn = Jdbcutil.getConnection();
12 
13             // 2:准备sql预编译语句
14             // ?占用一个参数位
15             String sql = "INSERT INTO person (NAME,sex,age) VALUES (?,?,?);";
16 
17             // 3:执行sql预编译语句(检查语法)
18             stsm = conn.prepareStatement(sql);
19 
20             // 4:设置传递的参数
21 
22             stsm.setString(1, "张三");
23             stsm.setString(2, "男");
24             stsm.setInt(3, 20);
25 
26             // 5:发送参数,执行sql
27             // 注意:这里的方法后面没有参数
28             int result = stsm.executeUpdate();
29             System.out.println("影响了" + result + "行");
30         } catch (Exception e) {
31             e.printStackTrace();
32         } finally {
33             // 6:关闭连接
34             Jdbcutil.close(conn, stsm);
35         }
36     }

 

  代码2:

 1 /**
 2      * 修改语句
 3      */
 4 
 5     private static void testUpdate() {
 6 
 7         Connection conn = null;
 8         PreparedStatement stsm = null;
 9 
10         try {
11             // 1:创建连接
12             conn = Jdbcutil.getConnection();
13 
14             // 2:创建sql预编译语言
15             String sql = "UPDATE person SET NAME = ?WHERE id = ?;";
16 
17             // 3:执行sql预编译语言
18             stsm = conn.prepareStatement(sql);
19 
20             // 4: 设置参数
21             stsm.setString(1, "李四");
22             stsm.setInt(2, 3);
23 
24             // 5:发送参数,执行sql
25             int result = stsm.executeUpdate();
26             System.out.println("影响了" + result + "行");
27         } catch (Exception e) {
28             e.printStackTrace();
29         } finally {
30             Jdbcutil.close(conn, stsm);
31         }
32 
33     }

 

  代码3: 

 1 /**
 2      * 查询语句
 3      */
 4 
 5     private static void testSelect() {
 6 
 7         ResultSet rs = null;
 8         Connection conn = null;
 9         PreparedStatement stsm = null;
10         try {
11             // 1:创建连接
12             conn = Jdbcutil.getConnection();
13 
14             // 2:创建sql预编译语言
15             String sql = "SELECT * FROM person;";
16 
17             // 3:执行预编译语言
18             stsm = conn.prepareStatement(sql);
19 
20             // 这里不需要设置参数
21             // 4:执行sql语言
22             rs = stsm.executeQuery();
23 
24             // 5:查看所有数据
25             while (rs.next()) {
26                 int id = rs.getInt("id");
27                 String name = rs.getString("name");
28                 String sex = rs.getString("sex");
29                 System.out.println(id + "," + name + "," + sex);
30             }
31         } catch (Exception e) {
32             e.printStackTrace();
33             throw new RuntimeException(e);
34         } finally {
35             Jdbcutil.close(conn, stsm, rs);
36         }
37 
38     }

 

posted @ 2016-10-30 13:34  卡拉瓦  阅读(593)  评论(0编辑  收藏  举报