JDBC bash批处理数据

这篇文章来自http://asialee.iteye.com/blog/949250,感谢知识共享的人

检测数据库是否支持batch

   DatabaseMetaData.supportsBatchUpdates()

 

   然后就是三个比较有用的方法:

 

   addBatch: 将Statement, PreparedStatement, and CallableStatement添加进batch里面

   

   executeBatch: 返回各个语句的执行结果

   

   clearBatch: 将batch里面的sql语句清除掉

 

   在这个里面有一个值得注意的是要设置connection的事务提交类型

 

   setAutoCommit(false)为手动提交

 // Create statement object
	Statement stmt = conn.createStatement();

	// Set auto-commit to false
	conn.setAutoCommit(false);

	// Create SQL statement
	String SQL = "INSERT INTO Employees (id, first, last, age) " +
				 "VALUES(200,'Zia', 'Ali', 30)";
	// Add above SQL statement in the batch.
	stmt.addBatch(SQL);

	// Create one more SQL statement
	String SQL = "INSERT INTO Employees (id, first, last, age) " +
				 "VALUES(201,'Raj', 'Kumar', 35)";
	// Add above SQL statement in the batch.
	stmt.addBatch(SQL);

	// Create one more SQL statement
	String SQL = "UPDATE Employees SET age = 35 " +
				 "WHERE id = 100";
	// Add above SQL statement in the batch.
	stmt.addBatch(SQL);

	// Create an int[] to hold returned values
	int[] count = stmt.executeBatch();

	//Explicitly commit statements to apply changes
	conn.commit();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值