我的目标是在批量插入查询中实现sql转义。例如:
INSERT INTO log VALUES (0,5,-7,'str'), (4,0,0,'str'), (0,0,0,'str');该代码每30秒在表中插入大约100-200条记录。(日志池)。
我没有找到使用PreparedStatement进行大容量插入方法,所以我不得不通过StringBuilder手动构建查询。
但是我不知道如何转义字符串,也不是真的想要应用一些东西,比如杂乱无章的修复(引号通过regex-replace来转义等等)。
有什么方便的方法吗?
发布于 2012-04-03 04:12:22
您需要使用PreparedStatement,可能还需要使用批插入。请参阅http://www.exampledepot.com/egs/java.sql/BatchUpdate.html
发布于 2013-01-04 17:32:40
到目前为止,我只知道两种方法。
第一路
Its insert record one by one
final String sql = "INSERT INTO tablename(columnname) Values(?)";
PreparedStatement statement = connection.prepareStatement(sql);
while (condition) {
statement.setString(1,value);
statement.executeUpdate();
}它以大容量插入的形式插入所有记录
final String sql = "INSERT INTO tablename(columnname) Values(?)";
PreparedStatement statement = connection.prepareStatement(sql);
while (condition) {
statement.setString(1,value);
statement.addBatch();
}
statement.executeBatch();https://stackoverflow.com/questions/9982988
复制相似问题