Java 当我必须插入100行非常相似的行时,使用PreparedStatement的最佳方法是什么?
我必须Java 当我必须插入100行非常相似的行时,使用PreparedStatement的最佳方法是什么?,java,mysql,jdbc,Java,Mysql,Jdbc,我必须**在数据库中插入100行**,这样所有列的值都完全相同,只有一个除外。例如,考虑一个表 ------------------ |UserId|Timestamp| ------------------ 现在,**每次插入只更改时间戳** 建议按以下方式使用准备好的报表吗 PreparedStatement pstmt = con.prepareStatement("INSERT INTO Pings (UserId,Timestamp) VALUES (?,?)"; pstmt.
**在数据库中插入100行**
,这样所有列的值都完全相同,只有一个除外。例如,考虑一个表
------------------
|UserId|Timestamp|
------------------
现在,**每次插入只更改时间戳**
建议按以下方式使用准备好的报表吗
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Pings (UserId,Timestamp) VALUES (?,?)";
pstmt.setInt(1,001); //setting user is
while(true){
pstmt.setTimestamp(2,getTimestamp());
pstmt.executeUpdate();
}
相比
while(true){
pstmt.setInt(1,001);
pstmt.setTimestamp(2,getTimestamp());
pstmt.executeUpdate();
}
如果我只设置了第一列的值一次,第一种方法有效吗 我建议您与和一起使用配料。那可能看起来像
int count = 0;
while (true) {
pstmt.setInt(1, 001);
pstmt.setTimestamp(2, getTimestamp());
pstmt.addBatch();
if (++count % 50 == 0) { // <-- batch size of 50.
pstmt.executeBatch();
}
}
pstmt.executeBatch();
int count=0;
while(true){
pstmt.setInt(1001);
setTimestamp(2,getTimestamp());
pstmt.addBatch();
如果(++count%50==0){/我建议您将批处理与and一起使用
int count = 0;
while (true) {
pstmt.setInt(1, 001);
pstmt.setTimestamp(2, getTimestamp());
pstmt.addBatch();
if (++count % 50 == 0) { // <-- batch size of 50.
pstmt.executeBatch();
}
}
pstmt.executeBatch();
int count=0;
while(true){
pstmt.setInt(1001);
setTimestamp(2,getTimestamp());
pstmt.addBatch();
如果(++count%50==0){//,谢谢,但还要告诉我pstmt.setInt(1001);是否可以在循环之外。是否真的需要每次都设置它?@aksharprabudesai最佳做法是每次都设置它,从性能角度看,我怀疑它是否会产生任何可测量的差异。另外,为什么在setInt(int,int)中设置前导的0
(s)
?这是setInt(1,1);
谢谢,但也请告诉我pstmt.setInt(1001);是否可以在循环之外。是否真的需要每次都设置它?@aksharprabudesai最佳做法是每次都设置它,从性能角度看,我怀疑它是否会产生任何可测量的差异。另外,为什么您要在setInt中设置leading0
(s)(int,int)
?这是setInt(1,1);