Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当我必须插入100行非常相似的行时,使用PreparedStatement的最佳方法是什么?_Java_Mysql_Jdbc - Fatal编程技术网

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中设置leading
0
(s)(int,int)
?这是
setInt(1,1);