日期:2014-05-16  浏览次数:20422 次

JSP复习笔记——第10章 连接数据库 之 JDBC相关
批处理:

Java代码
1.import java.sql.*;  
2.public class TestBatch {  
3. 
4. 
5.    public static void main(String[] args) throws Exception {  
6.        Class.forName("oracle.jdbc.driver.OracleDriver");  
7.        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521: sky", "scott", "tiger");  
8.        /* 
9.        Statement stmt = conn.createStatement(); 
10.        stmt.addBatch("insert into dept2 values (51, '500', 'haha')"); 
11.        stmt.addBatch("insert into dept2 values (52, '500', 'haha')"); 
12.        stmt.addBatch("insert into dept2 values (53, '500', 'haha')"); 
13.        stmt.executeBatch(); 
14.        stmt.close(); 
15.        */ 
16.          
17.        PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");  
18.        ps.setInt(1, 61);  
19.        ps.setString(2, "haha");  
20.        ps.setString(3, "bj");  
21.        ps.addBatch();  
22.          
23.        ps.setInt(1, 62);  
24.        ps.setString(2, "haha");  
25.        ps.setString(3, "bj");  
26.        ps.addBatch();  
27.          
28.        ps.setInt(1, 63);  
29.        ps.setString(2, "haha");  
30.        ps.setString(3, "bj");  
31.        ps.addBatch();  
32.          
33.        ps.executeBatch();  
34.        ps.close();  
35.          
36.        conn.close();  
37. 
38.    }  
39. 
40.} 
import java.sql.*;
public class TestBatch {


public static void main(String[] args) throws Exception {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521: sky", "scott", "tiger");
/*
Statement stmt = conn.createStatement();
stmt.addBatch("insert into dept2 values (51, '500', 'haha')");
stmt.addBatch("insert into dept2 values (52, '500', 'haha')");
stmt.addBatch("insert into dept2 values (53, '500', 'haha')");
stmt.executeBatch();
stmt.close();
*/

PreparedStatement ps = conn.prepareStatement("insert into dept2 values (?, ?, ?)");
ps.setInt(1, 61);
ps.setString(2, "haha");
ps.setString(3, "bj");
ps.addBatch();

ps.setInt(1, 62);