【问题标题】:select few rows from resultset mysql java从结果集mysql java中选择几行
【发布时间】:2012-11-07 05:19:55
【问题描述】:

我在我的 java 程序中使用了一个选择命令,并将它的值存储在结果集中。现在在结果集中循环时,我想使用一个选择命令,它将选择结果集的前 5 行并插入到其他表中。第二次,它应该选择接下来的 5 行并插入到表中。第三次,以此类推..

Statement s = connection.createStatement();
s.executeQuery("Select * from table1");
ResultSet res = s.getResultSet();
while(res.next()){
 // here i want to select the first 5 lines of the result set and insert in the second       table
}

【问题讨论】:

    标签: java mysql


    【解决方案1】:
    Statement s = connection.createStatement();
    s.executeQuery("Select * from table1");
    ResultSet res = s.getResultSet();
    
    while(res.next()){
        // here i want to select the first 5 lines of the result set and insert in the second       table
        while(res.next() && (res.getRow()%5) !=0){
            //select from this table
            //call insert method(what selected)
        }
    }
    

    【讨论】:

    • 这将选择 5 行并插入到其他表中,现在下次它会选择接下来的 5 行..???\
    • @Nikki :是的,为什么不呢?你插入 res 对象 getRow() 只是给你行号。我不是在操纵 res 对象的遍历
    • @BhavikShah 它会选择下一个 5 下一个 5 和 .. 它不会在前 5 点之后停止,我对吗?
    • @BhavikShah 所以在第二个选择命令中我必须从表 1 中选择 * ?
    • @BhavikShah 嗨,我试过你的代码,但它选择了备用行,比如第 2、4、6 行等等.....
    【解决方案2】:

    我建议使用 LIMIT 并使用 PreparedStatement 更改您的查询。比如:

    SELECT * FROM table1 LIMIT ?,?
    

    这有几个优点:

    • 您不会一次性获取所有内容 - 如果您的表中有很多行要处理,有时可以提高性能
    • 您可以更改预定义要在每批中提取的元素数量

    所以你的代码看起来像这样:

    PreparedStatement ps = null;
    ResultSet rs = null;
    final int FETCH_LIMIT   = 5; //number of elements to fetch per batch
    final int BATCH_LIMIT   = 3; //number of batches you would want
    
    int currentRows = 0;
    try{
        ps = connection.prepareStatement("SELECT * FROM table1 LIMIT ?,?");
        for(int currentBatch = 0; currentBatch < BATCH_LIMIT; currentBatch++){
            ps.clearParameters();
            ps.setInt(1, currentRows);
            ps.setInt(2, currentRows + FETCH_LIMIT);
    
            try{
                rs = ps.executeQuery();
                while(rs.next()){
                    // do your work
                }
            }catch(Exception exe){
                //manage exception
            }finally{
                //manage resultset
            }
            currentRows += FETCH_LIMIT;
        }
    }catch(Exception exe){
        //Handle your exception
    }
    finally{
        //Manage your resources
    }
    

    【讨论】:

    • @sunleo - 问题被标记为MySQL,因此提出了建议。但是,是的,我同意并非所有 RDBMS 都支持 LIMIT,尽管它们中的大多数都具有某种设置获取限制的功能。
    【解决方案3】:

    请添加一个 falg 并使用它

    int i=0;
    
    while(res.next() && i< 5){
    //select from this table
    //call insert method(what selected)
    i++;
    }
    

    【讨论】:

    • 这是一个合理的解决方案,但不是很整洁!在我看来
    • @BhavikShah 请让我知道您所说的简洁解决方案是什么意思?
    • 简洁的解决方案意味着在您的情况下避免使用不必要的变量,例如“i”。
    • 你认为没有传递那个“i”,你可以得到5个值。如果可以请告诉我,我会相应地编辑。
    【解决方案4】:

    在while循环内动态创建另一个插入查询,并在while循环外执行

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多