同样的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
class
TestBatch {
public
static
void
main(String[] args)
throws
SQLException, ClassNotFoundException {
//Class.forName("oracle.jdbc.driver.OracleDriver");
//Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","guojje","guojje");
Class.forName(
"com.mysql.jdbc.Driver"
);
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test"
,
"root"
,
""
);
String sql =
"insert into book (kind, name) values (?,?)"
;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(
1
,
"java"
);
pstmt.setString(
2
,
"jjjj"
);
pstmt.addBatch();
pstmt.setString(
1
,
"ccc"
);
pstmt.setString(
2
,
"dddd"
);
pstmt.addBatch();
//添加一次静态SQL
pstmt.addBatch(
"update book set kind = 'JAVA' where kind='java'"
);
//批量执行预定义SQL
pstmt.executeBatch();
}
}
|
MySQL是通过,对于Oracle则会报不支持特性, 原因在于addBatch动态sql之后,
不能再addBatch静态sql. 已此记录。
本文转自 anranran 51CTO博客,原文链接:http://blog.51cto.com/guojuanjun/1652085