作业帮 > 综合 > 作业

java.sql.SQLException: Illegal operation on empty result set

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/13 19:44:50
java.sql.SQLException: Illegal operation on empty result set.
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
public class shujuku {
public static void main(String args[]) {
Connection conn;
conn = Conn.getCon();
try{
Statement stmt = conn.createStatement();
ResultSet sql1=stmt.executeQuery("select * from register where name='cai'");
String iping = sql1.getString("psw");
int porting=sql1.getInt("psw2");
System.out.println(iping);
System.out.println(porting);
}catch(Exception e){
e.printStackTrace();
System.out.println("怎么会有错");
}
}
}
这个怎么错了,麻烦帮我看一下,谢谢啊
java.sql.SQLException: Illegal operation on empty result set
ResultSet 是个结果集.不能这样取记录的sql1.getString("psw");
需要循环历遍的.那怕结果集返回的只有一条记录
改成这样看看.
public static void main(String args[]) {
Connection conn;
conn = Conn.getCon();
try{
Statement stmt = conn.createStatement();
ResultSet sql1=stmt.executeQuery("select * from register where name='cai'");
while(sql1.next()){
String iping = sql1.getString("psw");
int porting=sql1.getInt("psw2");
System.out.println(iping);
System.out.println(porting);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("怎么会有错");
}
}