Java JDBCI批量插入数据

 

 

智能插入:将整批分批,每一千条提交一次,sql注入(安全,使用软解析,提高效率)

sql注入攻击:简单例子

select * from table where name=‘"+un+"‘ and psw=‘"+pw+"‘",

假如可以输入任何字符,un随便输入12345‘ or 1=1--这样提交过来的aa就会select * from table where name=‘12345‘ or 1=1--password

String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000;
int count = 0;
for (Employee employee: employees) {
    ps.setString(1, employee.getName());
    ps.setString(2, employee.getCity());
    ps.setString(3, employee.getPhone());
    ps.addBatch();
    if(++count % batchSize == 0) {
        ps.executeBatch();
    }
}
ps.executeBatch(); // insert remaining records
ps.close();
connection.close();

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。