简体   繁体   中英

java.sql.SQLException: Illegal operation on empty result set. when query return only 1 row

in this code i am trying to get a row from the id in for lopp variable and printing it into a labels which are created on the basis of count though count is less then the total number of rows in the database the the returns a correct ans when i executed it in mysql. but when i use the rs.getstring("concept name") in the last line to get the contents of "concept name" column i get empty result set error.

Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/niitdb","root","");

for(pos=0; pos<7; pos++){

     SQL = "SELECT `concept name` FROM `concepts " +course+ "` where `concept id` = " + pos+1;   
     Statement stmt = con.createStatement();
     rs = stmt.executeQuery(SQL);
     rs.next();
     Cname[cnt] = new JLabel(rs.getString("concept name")); // error is on this line 
}

The JDBC driver may not handle the column name with blanks correctly. I would try it as

SELECT `concept name` as concept_name FROM ...

and then

if (rs.next())
{
    Cname[cnt] = new JLabel(rs.getString("concept_name"));
}

as you should not call rs.next() without testing the result, and only proceeding to get column values if it was true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM