java - Can't figure out why sqlite query isn't returning the right result -


i have sqlite database can run query "select * barcode barcode='cfms-zufh-wrvy-exaa' , single row return. yet in java application when run pretty same sql, it's returning empty resultset. variables last, email, etc. jtextfields though problem before them. if statement doesn't execute , if run rs.next() before , set if (true) error saying resultset closed.

try {     db.dbopen(config.getdbfolder(),config.getdbname());      // barcode-barcode.replaceall("\\s","");     resultset rs=db.query("select * barcode barcode=?",barcode);      if (rs.next()) {         first.settext(rs.getstring("first_name"));         system.out.println(rs.getstring("first_name"));         last.settext(rs.getstring("last_name"));         email.settext(rs.getstring("email"));         phone.settext(rs.getstring("phone"));         subject.setselecteditem(rs.getstring("subject"));         boolean selected;         if (rs.getstring("baronly").equals("false")) selected=false;         else selected=true;         barcodebox.setselected(selected);            update=true;      }      db.conn.close();  } catch ( sqlexception e) {     e.printstacktrace(); } catch (classnotfoundexception e) {     // todo auto-generated catch block     e.printstacktrace(); } 

here's method query.

public resultset query(string sql, string barcode) throws sqlexception {     // todo auto-generated method stub     preparedstatement prep =conn.preparestatement(sql);     prep.setstring(1, barcode);      resultset rs= prep.executequery();       prep.close();     return rs; } 

in query method, close preparedstatement before return resultset. according javadocs close:

note:when statement object closed, current resultset object, if 1 exists, closed.

don't close preparedstatement in query, close after use resultset. remove query method , place code inline rest of code, still have reference preparedstatement close after you're done resultset.

and @reimeus has mentioned, assign result of replaceall barcode.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -