首页 > 经验记录 > JDBC批处理代码实现

JDBC批处理代码实现

没什么用,记录一下

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
public class TestBatch {
	public static void main(String[] args) throws SQLException {
		Connection conn = DBUtil.getConnection();
		/*conn.setAutoCommit(false);
		conn.commit();*/
		/*String sql1 = "insert into test values(2,'jim')";
		String sql2 = "update test set tname='admin' where tid=1";
		Statement stmt = conn.createStatement();
		stmt.addBatch(sql1);
		stmt.addBatch(sql2);
		//执行:返回的是一个int[]
		stmt.executeBatch();
		//清楚批处理命令
		stmt.clearBatch();*/
		String sql = "insert into test values(?,?)";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, 3);
		ps.setString(2, "marry");
		ps.addBatch();
		ps.setInt(1, 4);
		ps.setString(2, "abc");
		ps.addBatch();
		ps.executeBatch();
	}
}

 

           


CAPTCHAis initialing...
EA PLAYER &

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

      00:00/00:00