首页 文章

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

提问于
浏览
0

我有一个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 回答

  • 3

    Note: 注意到 AndreasFrank ,您的更新语句看起来有点不正确 . 您似乎缺少update语句中的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){
            }
        }
    }
    
  • 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
    

    我插入了箭头,我已经做了更改

相关问题