database - How can I return ResultSet before closing the connection in Java -
i want write 1 execute method , re-use whenever need without rewrite whole thing. below method return resultset.
public resultset executeselect() { connection con = null; preparedstatement prepared = null; resultset rs = null; try { con = c_db.getconnection(); prepared = con.preparestatement(this.getquery()); if(!this.midquery.equals("")) { int = 1; (string val: this.values) { prepared.setstring(i, val); i++; } } rs = prepared.executequery(); } catch (sqlexception e) { e.printstacktrace(); } { try { if (prepared != null) { prepared.close(); } if (con != null) { con.close(); } } catch(sqlexception e) { e.printstacktrace(); } } return rs; }
the problem after executeselect() executed, connection, preparedstatement , resultset closed before returning data got error java.sql.sqlexception: operation not allowed after resultset closed
the reason want return resultset can re-use executedata method object data. example:
c_query q = new c_query(datasearch,"select * people ", " order id desc"); resultset rs = q.executeselect() observablelist<c_person> peopledata = fxcollections.observablearraylist(); while(rs.next()){ c_person person = new c_person(); person.setfirstname(rs.getstring("firstname")); person.setlastname(rs.getstring("lastname")); peopledata.add(person); }
and
c_query q = new c_query(datasearch,"select * cars", " order id desc"); resultset rs = q.executeselect() observablelist<c_car> cardata = fxcollections.observablearraylist(); while(rs.next()){ c_car car = new c_car(); car.type(rs.getstring("type")); car.brand(rs.getstring("brand")); cardata.add(car); }
how can fix this?
there 'cachedrowset', that: https://docs.oracle.com/javase/6/docs/api/javax/sql/rowset/cachedrowset.html
Comments
Post a Comment