简体   繁体   English

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

[英]How can I move through all the records in MySQL update query?

I have a java code where I need to read many lines from a text file and I need to update records with the read lines. 我有一个Java代码,我需要从一个文本文件中读取很多行,并且需要使用读取的行来更新记录。 For example, the text file contains: aaa, bbb, ccc, .. etc (the comma means new line), so, I want to update col4 in record1 with the value aaa, record2 with the value bbb, etc. 例如,文本文件包含:aaa,bbb,ccc,..等(逗号表示换行),因此,我想用值aaa更新record1中的col4,用值bbb更新record2中的col4,等等。

How can I make update statement that makes the update for every record automatically ?? 我怎样才能使每条记录自动更新的更新语句?

This is my Java code: 这是我的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 

Note: As Andreas and Frank noted, your update statement looks a little incorrect. 注意:正如AndreasFrank指出的那样,您的更新语句看起来有点不正确。 You seem to be missing a where clause from your update statement. 您似乎更新语句中缺少where子句。 This is because, you're trying to set only one parameter to your PreparedStatement. 这是因为,您试图为自己的PreparedStatement设置一个参数。 Ideally an update statement looks something like this: 理想情况下,更新语句如下所示:

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

ie you at least need to have one or more columns in your where clause to identify which record or records that needs be updated. 也就是说,您至少需要在where子句中有一个或多个列,以标识需要更新的记录。 If you omit the WHERE clause, all records will be updated ( which is something that you might not want to do ) 如果省略WHERE子句,则所有记录都将被更新( 这是您可能不想执行的操作

Also, as a performance improvement for huge data set, consider updating in batch. 此外,作为对海量数据集的性能改进,请考虑批量更新。 So here's what you would do: 因此,这是您要执行的操作:

  • Create a batch size. 创建一个批量大小。
  • For each line that you read from your file, add it to batch. 对于从文件中读取的每一行,将其添加到批处理中。 This you can do by calling the addBatch() method of PreparedStatement 您可以通过调用PreparedStatementaddBatch()方法来完成此操作
  • Once you reach the batch size, execute the batch by calling executeBatch() . 一旦达到批处理大小,就可以通过调用executeBatch()执行批处理。 You would then clear you batch ( clearBatch() )and continue the process till you're done with reading all the lines from your file. 然后,您将清除批处理( clearBatch() )并继续执行该过程,直到从文件中读取所有行为止

Something like this: 像这样:

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){
        }
    }
}

Since you are using a counter you can use it in where clause 由于您使用的是计数器,因此可以在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 

I have inserted the arrow where I have made changes 我将箭头插入进行更改的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM