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等)。 如何将它们分为两个变量?
答案 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();