简体   繁体   English

Java SQL查询无法返回有效的ResultSet

[英]Java SQL query failing to return valid ResultSet

This is driving me mad because I cannot make any sense of it. 这让我很生气,因为我无法理解它。 I am executing the following code: 我正在执行以下代码:

nameString = jcbClientList.getItemAt(jcbClientList.getSelectedIndex());
System.out.println(" Name String = " + nameString );
sql = "SELECT * FROM clients WHERE Name = \'" + nameString + "\'";
System.out.println(sql);

try {
    Statement st = con.createStatement();
    ResultSet rs = st.executeQuery(sql);
    while( rs.next()) {
        clientID = rs.getInt(1);
    }
}
catch(SQLException se) {
    msg = "Problem getting client ID from DB \n" + se.getLocalizedMessage();
    System.out.println(msg);
    JOptionPane.showMessageDialog(null, msg);
}

The SQL string be built is correct. 构建的SQL字符串是正确的。 I have checked this by taking the System.out.println(sql) output of the string and pasting it into other code and it work perfectly. 我通过获取字符串的System.out.println(sql)输出并将其粘贴到其他代码中来检查这一点,它完美地工作。 However, in this context I am getting an exception: 但是,在这种情况下,我得到一个例外:

Invalid cursor state - no current row. 游标状态无效 - 没有当前行。

Even if I change the sql to be 'SELECT * FROM clients' which should return 20 rows and does elsewhere in the application, it still gives the same error. 即使我将sql更改为'SELECT * FROM clients',它应该返回20行并且在应用程序的其他地方执行,它仍然会给出相同的错误。 The database being addressed is an embedded Derby DB. 正在寻址的数据库是嵌入式Derby DB。

I seem to recall having run into specific JDBC drivers that did not properly implement the part that says " A ResultSet cursor is initially positioned before the first row ". 我似乎记得遇到过没有正确实现“ResultSet游标最初位于第一行之前”的部分的特定JDBC驱动程序。 I got around it by first doing a first() (or beforeFirst()) call and only then start invoking next(). 我首先执行first()(或beforeFirst())调用,然后才开始调用next()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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