jdbc之Statement和Preparement


  1 Jdbc DML 操作
  2 Statement:靜態SQL操作
  3 每次操作都會將sql語句提交到數據庫執行一次,性能比較低
  4     // 1.加載驅動程序
  5     Class.forName(driverName);
  6     // 2.獲取數據庫連接
  7     Connection conn = DriverManager.getConnection(url, user, password);
  8     // 3.構建SQL語句
  9     String sql = "insert into t_user(userid,username,password)values(seq_t_user.nextval,'abc','123456')";
 10     // 4.執行SQL語句
 11     Statement sts = conn.createStatement();
 12     // 返回的結果是影響的行數
 13     int count = sts.executeUpdate(sql);
 14     System.out.println("影響的行數:" + count);
 15     // 5.關閉資源
 16     sts.close();
 17     conn.close();
 18 
 19 
 20 Jdbc DML 操作
 21 PrepareStatement:動態sql操作  預編譯操作
 22 優點:性能比Statement高
 23     Statement對象能做的操作Preparedstatement都能做
 24     Preparedstatement能做的Statement不一定能做
 25     通過PrepareStatement對象更新用戶數據
 26     execute:DDL操作
 27     executeUpdate:DML操作
 28     executeQuery:DQL操作
 29     executeBatch:批量處理 一次性執行多條sql語句
 30 
 31     // 1.加載驅動程序
 32     Class.forName(driverName);
 33     // 2.獲取數據庫的連接通道
 34     Connection conn = DriverManager.getConnection(url, user, password);
 35     // 3.構建SQL語句
 36     String sql = "update t_user set password=? where userid=?";
 37     // 4.執行sql語句
 38     // 獲取Statement對象
 39     PreparedStatement ps = conn.prepareStatement(sql);
 40     ps.setString(1, "hehe");
 41     ps.setInt(2, 3);
 42     int count = ps.executeUpdate();
 43     System.out.println("影響的行數:"+count);
 44     // 關閉資源
 45     ps.close();
 46 
 47 
 48 Jdbc查詢:
 49     // 1.加載驅動程序
 50     Class.forName(driverName);
 51     // 2.獲取數據庫連接通道
 52     Connection conn = DriverManager.getConnection(url, user, password);
 53     // 3.構建SQL語句
 54     String sql = "select * from t_user order by userid";
 55     // 4.執行SQL語句獲取查詢結果
 56     PreparedStatement ps = conn.prepareStatement(sql);
 57     // 執行查詢  ResultSet 類似於我們講的游標 迭代器
 58     ResultSet rs = ps.executeQuery();// 其實並沒有獲取到數據,而是指向了第一條數據之前
 59     // 判斷是否有下一條記錄並把該記錄保存到ResultSet對象中
 60     while(rs.next()){// 每循環一次取出一條新的記錄
 61         //int id = rs.getInt("userid");
 62         int id = rs.getInt(1); // 通過列的下標取字段內容,從1開始
 63         String userName = rs.getString("username");
 64         String userPassword = rs.getString("password");
 65         System.out.println(id+" "+userName+" "+userPassword);
 66     }
 67     // 5.關閉相關的資源
 68     rs.close();
 69     ps.close();
 70     conn.close();
 71 
 72 
 73 批處理:
 74     /**
 75      * 通過Statement執行批處理操作
 76      *    適用於執行多條不同的sql語句
 77      * @throws Exception
 78      */
 79     @Test
 80     public void testStatementBatch() throws Exception {
 81         conn = DbUtils.getConnection();
 82         Statement sts = conn.createStatement();
 83         sql1 = "insert into t_user(userid,username,password)values(seq_t_user.nextval,'a','b')";
 84         sql2 = "update t_user set password='111' where userid=43";
 85         // 添加批處理的sql語句
 86         sts.addBatch(sql1);
 87         sts.addBatch(sql2);
 88         sts.executeBatch();
 89         DbUtils.close(sts, conn);
 90 
 91     }
 92     
 93     /**
 94      * 通過PreparedStatement對象執行批處理操作
 95      * @throws Exception 
 96      * @throws ClassNotFoundException 
 97      */
 98     @Test
 99     public void testPreparedStatement() throws  Exception {
100         conn = DbUtils.getConnection();
101         sql = "insert into t_user(userid,username,password)values(seq_t_user.nextval,?,?)";
102         PreparedStatement ps = conn.prepareStatement(sql);
103         /*ps.setString(1, "a1");
104         ps.setString(2, "b1");
105         ps.addBatch();
106         ps.setString(1, "a2");
107         ps.setString(2, "b3");
108         ps.addBatch();*/
109         for(int i = 0 ; i < 10 ; i++){
110             ps.setString(1, "a"+i);
111             ps.setString(2, "b"+i);
112             ps.addBatch();
113         }
114         
115         ps.executeBatch();
116         DbUtils.close(ps, conn);
117     }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM