mysql - get last added id in first table and insert it in another table using java -


i trying insert last added id of chapter table lesson table.

here code:

string sql = "insert lesson"             +"(title, chapter_id, source)"             +"values(?,?,?)"; string getid = "select max(id) id chapter"; rs = pst.executequery(getid); int lastid = rs.getint("id"); rs.close(); pst=conn.preparestatement(sql); pst.setstring(2,txt_lessontitle.gettext()); pst.setint(3,lastid); pst.setstring(4,txt_lessoncontent.gettext());  pst.executeupdate(); 

i got error:

before start of result set

you need check whether there results

rs = pst.executequery(getid); if (rs.next()) {         int lastid = rs.getint("id");         rs.close();         pst=conn.preparestatement(sql);         pst.setstring(2,txt_lessontitle.gettext());         pst.setint(3,lastid);         pst.setstring(4,txt_lessoncontent.gettext());          pst.executeupdate(); } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -