无法将结果集的结果存储在变量中

问题描述:

我在将b_id存储在变量bId中时遇到问题,因此我可以进一步使用它。 请帮忙。 它显示'结果开始之前'错误。无法将结果集的结果存储在变量中

Class.forName("com.mysql.jdbc.Driver"); 
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/librarian_db","root","tiger"); 
    PreparedStatement ps2 = con.prepareStatement("select b_id from books where Call_num= ?"); 
    ResultSet rs; 
    ps2.setString(1, callNo); 
    rs=ps2.executeQuery(); 
    bId = rs.getString(1); 
    System.out.println(bId); 
+0

那是什么麻烦?什么是错误? –

+0

java.sql.SQLException:结果集开始之前 这是错误 – Ayushi

你需要使用rs.next()例如:

if(rs.next()){ 
    bId = rs.getString(1); 
    System.out.println(bId); 
} 

如果你想多个结果可以使用在:

while(rs.next()){ 
    bId = rs.getString(1); 
    System.out.println(bId); 
} 
+1

谢谢。 它帮助:) – Ayushi