如何浏览MySQL更新查询中的所有记录?

时间:2012-09-23 20:06:56

标签: java mysql sql

我有一个java代码,我需要从文本文件中读取许多行,我需要用读取行更新记录。例如,文本文件包含:aaa,bbb,ccc,.. etc(逗号表示新行),因此,我想更新record1中的col4,其值为aaa,record2的值为bbb等。

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

这是我的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 

2 个答案:

答案 0 :(得分: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){
        }
    }
}

答案 1 :(得分: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 

我已经插入了我做出更改的箭头