SQL批量处理+JDBC操作大数据及工具类的封装
一、批处理 — 批量处理sql语句
在jdbc的url中添加rewriteBatchedStatements=true参数,可以提高批处理执行效率。
在我们进行大批量数据操作的时候,需要采用批处理的方式来提高程序的运行性能,目的是减少跟数据库交互的次数
1.批量执行多条sql语句,sql语句不相同(statement.addBatch(inserSql))
import com.qf.utils.DBUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class Test01 {
public static void main(String[] args) throws SQLException {
Connection connection = DBUtil.getConnection();
Statement statement = connection.createStatement();
String sql1 = "INSERT INTO student(name,sex,age,salary,course,password) VALUES('小橘子','男',25,15000,'Java','12345678');";
String sql2 = "UPDATE student set password = '66666666'";
//将SQL命令添加到Batch包
statement.addBatch(sql1);
statement.addBatch(sql2);
//将Batch包发送给MySQL
statement.executeBatch();
DBUtil.close(connection,statement,null);
}
}
2.批量插入100条记录,sql语句相同,只是参数值不同(statement.addBatch()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)