Statement and PreparedStatement The difference is not much nonsense, directly said PreparedStatement the most important addbatch () structure of the use.
1. Establish the link, (telephone dialing)
Connection Connection =getconnection ();
2. Do not automatically Commit (melon seeds are not one to eat, all stripped open on the table, and then licked)
Connection.setautocommit (FALSE);
3. Precompiled SQL statements, only compile one time oh, high efficiency ah. (Invent a way to peel the melon seeds, don't always want to peel the melon seeds in the future.)
PreparedStatement statement = connection.preparestatement ("INSERT into TableX VALUES (?,?)");
4. One to peel and then put on the table.
Record 1
Statement.setint (1, 1);
Statement.setstring (2, "Cujo");
Statement.addbatch ();
Record 2
Statement.setint (1, 2);
Statement.setstring (2, "Fred");
Statement.addbatch ();
Record 3
Statement.setint (1, 3);
Statement.setstring (2, "Mark");
Statement.addbatch ();
Execute the above 3 statements in bulk. Swallow it, it's cool.
int [] counts = Statement.executebatch ();
Commit it to swallow, into the belly (DB) inside
Connection.commit ();
Stmt.addbatch ("update TABLE1 set topic =" Midsummer Speech foot Health 1 "where id=" 3407 ");
Stmt.addbatch ("update TABLE1 set topic =" Summer heat stroke prevention meal 1 "where id=" 3408 ");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 11 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 12 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 13 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 14 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 15 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 16 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 17 "," 12 "," 13 "," "", ")");
Stmt.addbatch ("INSERT into TABLE1 VALUES (" 18 "," 12 "," 13 "," "", ")");
int [] Updatecounts=stmt.executebatch ();
Cn.commit ();
For example:
public static void Exectebatch (Connection conn) throws exception{
& nbsp; String sql1 = "Delete from student where id = 3";
String sql2 = "Delete from student where id = 5";
String sql3 = "Delete from student where id = 6";
String sql4 = "Delete from student where id = 7";
PreparedStatement pstmt = conn.preparestatement (SQL1);
pstmt.addbatch ();
Pstmt.addbatch (SQL2);
Pstmt.addbatch (SQL3);
Pstmt.addbatch (SQL4);
pstmt.executebatch ();
};