SQLite分隔两个随机数

时间:2018-12-22 20:24:30

标签: java sqlite jdbc

public void Isrinkzaidejapirma(){
    String sql = "SELECT rowid FROM Veiksmai WHERE Pirmas < 4 ORDER BY random() LIMIT 2";

    try (Connection conn = this.connect();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql)){
        while (rs.next()) {
            int zaidejas1id = rs.getInt("rowid");
            //System.out.println(rs.getInt(zaidejas2id));
            System.out.println(zaidejas1id);
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
}

使用此代码,我得到2个随机数。 (例如4 2、1 2等)。 如何将它们分为两个变量?

1 个答案:

答案 0 :(得分:0)

代替while (rs.next())循环,只是使用

Integer value1 = null, value2 = null;
if (rs.next()) {
    value1 = rs.getInt("rowid");
    if (rs.next()) {
        value2 = rs.getInt("rowid");
    }
}

然后,您可以在后续查询中使用这些值之一:

PreparedStatement ps = conn.prepareStatement("SELECT Pirmas FROM Veiksmai WHERE rowid=?");
ps.setInt(1, value1);
rs = ps.executeQuery();