0

我有一个 java 代码,我需要从文本文件中读取多行,并且需要使用读取的行更新记录。例如,文本文件包含:aaa、bbb、ccc、.. 等(逗号表示换行),所以,我想用值 aaa 更新记录 1 中的 col4,用值 bbb 等更新记录 2。

如何制作自动更新每条记录的更新语句?

这是我的 Java 代码:

counter=0;
        while((fileLine=in.readLine())!=null) 
        { 
            System.out.println("Line read is: "+fileLine);

                //execute db insertion
                try {
                    //insert in the database
                    String Query= "update db.table set col4=?";    //database
                    preparedStmt3 = DBConnection.con.prepareStatement(Query); 
                    preparedStmt3.setString (1, fileLine);
                    preparedStmt3.executeUpdate();
                    System.out.println("Complete update statement for row: "+counter);
                    counter++;
                } catch (Exception e) {
                    System.out.println("DB_Error:_"+ e.toString());
                }

        } //end while loop 
4

2 回答 2

3

注意:如前所述AndreasFrank您的更新语句看起来有点不正确。您似乎缺少更新语句中的where子句。这是因为,您尝试只为 PreparedStatement 设置一个参数。理想情况下,更新语句看起来像这样:

UPDATE table_name
SET column1=?, column2=?,...
WHERE some_column=?

即,您至少需要在where子句中有一个或多个列来确定需要更新的记录。如果您省略 WHERE 子句,所有记录都将被更新(这是您可能不想做的事情

另外,作为海量数据集的性能提升,可以考虑批量更新。所以这就是你要做的:

  • 创建批量大小。
  • 对于从文件中读取的每一行,将其添加到批处理中。这可以通过调用PreparedStatement的addBatch()方法来完成
  • 达到批处理大小后,通过调用executeBatch()执行批处理。然后,您将清除批处理(clearBatch())并继续该过程,直到您完成从文件中读取所有行。

像这样的东西:

PreparedStatement preparedStmt3 = null;
try{
    counter=0;
    int batchCutoff = 1000, currentBatchSize = 0;

    Query= "update db.table set col4=?";    //database
    preparedStmt3 = DBConnection.con.prepareStatement(Query); 

    while((fileLine=in.readLine())!=null) 
    { 
        System.out.println("Line read is: "+fileLine);
        //execute db insertion
        try {
            //insert in the database
            preparedStmt3.setString (1, fileLine);
            preparedStmt3.addBatch();
            preparedStmt3.clearParameters();
            currentBatchSize++;

            if(currentBatchSize >= batchCutoff){
                preparedStmt3.executeBatch();
                preparedStmt3.clearBatch();
                System.out.println("Complete update statement for: "+currentBatchSize+" row(s)");
                currentBatchSize = 0;
            }
            counter++;
        } catch (Exception e) {
            System.out.println("DB_Error:_"+ e.toString());
        }
    } //end while loop 
    //In case the cut-off has not been reached and some statements in the batch are remaining
    try{
        preparedStmt3.executeBatch();
        preparedStmt3.clearBatch();
    }catch (Exception e) {
            System.out.println("DB_Error:_"+ e.toString());
    }finally{
        System.out.println("Total of: "+counter+" row(s) updated");
    }
}finally{
    if(preparedStmt3 != null){
        try{
            preparedStmt3.close();
        }catch(Exception exe){
        }
    }
}
于 2012-09-23T20:13:18.543 回答
0

由于您使用的是计数器,因此您可以在 where 子句中使用它

----->>counter=1;
    while((fileLine=in.readLine())!=null) 
    { 
        System.out.println("Line read is: "+fileLine);

            //execute db insertion
            try {
                //insert in the database
                String Query= "update db.table set col4=? where id=?";    //database
                preparedStmt3 = DBConnection.con.prepareStatement(Query); 
                preparedStmt3.setString (1, fileLine);
       ---->>   preparedStmt3.setInt(2,counter);
                preparedStmt3.executeUpdate();
                System.out.println("Complete update statement for row: "+counter);
                counter++;
            } catch (Exception e) {
                System.out.println("DB_Error:_"+ e.toString());
            }

    } //end while loop 

我已在我进行更改的地方插入了箭头

于 2012-10-16T08:08:50.543 回答