Java 代码优化:提高数据插入效率
public void insertData(outqjSqlStruct pressure, String sqlname) {
String sql = 'INSERT ignore into ' + sqlname + ' (entity_id, key, ts, dbl_v, json_v, device_name) VALUES (?, ?, ?, ?, ?, ?)';
try (Connection conn = new connection().getConnection2();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, pressure.getValue());
pstmt.setString(2, pressure.getValue1());
pstmt.setString(3, pressure.getValue3());
pstmt.setDouble(4, pressure.getValue2());
pstmt.setString(5, pressure.getValue5());
pstmt.setString(6, pressure.getValue4());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
// 简化了 try-with-resources 块,将连接和语句对象合并。 // 移除不必要的 connection.destroy() 方法调用。 // 添加了适当的缩进和间距,提高可读性。
原文地址: https://www.cveoy.top/t/topic/ncrY 著作权归作者所有。请勿转载和采集!