简体   繁体   中英

java.sql.SQLException: Before start of result set

I don't understand why this isn't working, I keep getting this java.sql.SQLException: Before start of result set error, ive looked online but because ive been staring at a computer screen for hours my brain is frazzling so I'm not able to find any soloutions. please can you help me?! thanks

String from = "";
String subject= "";
String to= "";
String message= "";
try
{
    while(retrieveMessages().next())
    {
         to = retrieveMessages().getString("tblEmail.to");
         from = retrieveMessages().getString("tblEmail.from");
         subject = retrieveMessages().getString("tblEmail.subject");
         message = retrieveMessages().getString("tblEmail.message");
         MailBox.add(new Email(to, from, subject, message));
    }
}
catch (SQLException e)
{

    e.printStackTrace();
}

You're calling retrieveMessages() multiple times. It doesn't help that you haven't shown us what that does, but I suspect you want to call it once and store the result in a local variable. At the moment, I suspect you're calling next() on one result set, then getString() on a different result set. For example:

try (ResultSet results = retrieveMessages()) {
    while (results.next()) {
        String to = results.getString("tblEmail.to");
        String from = results.getString("tblEmail.from");
        String subject = results.getString("tblEmail.subject");
        String message = results.getString("tblEmail.message");
        MailBox.add(new Email(to, from, subject, message));
    }
}

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