首页 > 代码库 > JDBC批量操作
JDBC批量操作
/**
* 批量执行预定义模式的SQL
*/
public static void exeBatchParparedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.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.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 批量执行混合模式的SQL、有预定义的,还有静态的
*/
public static void exeBatchMixedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.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 testdb.book set kind = ‘JAVA‘ where kind=‘java‘");
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 执行批量静态的SQL
*/
public static void exeBatchStaticSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
Statement stmt = conn.createStatement();
//连续添加多条静态SQL
stmt.addBatch("insert into testdb.book (kind, name) values (‘java‘, ‘java in aciton‘)");
stmt.addBatch("insert into testdb.book (kind, name) values (‘c‘, ‘c in aciton‘)");
stmt.addBatch("delete from testdb.book where kind =‘C#‘");
stmt.addBatch("update testdb.book set kind = ‘JAVA‘ where kind=‘java‘");
// stmt.addBatch("select count(*) from testdb.book"); //批量执行不支持Select语句
//执行批量执行
stmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
* 批量执行预定义模式的SQL
*/
public static void exeBatchParparedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.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.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 批量执行混合模式的SQL、有预定义的,还有静态的
*/
public static void exeBatchMixedSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
String sql = "insert into testdb.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 testdb.book set kind = ‘JAVA‘ where kind=‘java‘");
//批量执行预定义SQL
pstmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
/**
* 执行批量静态的SQL
*/
public static void exeBatchStaticSQL() {
Connection conn = null;
try {
conn = DBToolkit.getConnection();
Statement stmt = conn.createStatement();
//连续添加多条静态SQL
stmt.addBatch("insert into testdb.book (kind, name) values (‘java‘, ‘java in aciton‘)");
stmt.addBatch("insert into testdb.book (kind, name) values (‘c‘, ‘c in aciton‘)");
stmt.addBatch("delete from testdb.book where kind =‘C#‘");
stmt.addBatch("update testdb.book set kind = ‘JAVA‘ where kind=‘java‘");
// stmt.addBatch("select count(*) from testdb.book"); //批量执行不支持Select语句
//执行批量执行
stmt.executeBatch();
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBToolkit.closeConnection(conn);
}
}
来源: <http://lavasoft.blog.51cto.com/62575/238651/>
?上面三种方法都是实质上都是用来addBatch(),executeBatch(),方法,PreparedStatement 是Statement 子类。如果只有insert时完全可以在values后面跟多个(),()……的形式插入多条记录。自增长ID默认可以不插入,但也可以赋值插入或更新。来自为知笔记(Wiz)
JDBC批量操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。