java - Resultset can not be traversed -
java - Resultset can not be traversed -
i want utilize while(rs.next())
traverse userinfo
table. there 7 records in userinfo
table, got first record. after had joined codes of rs.getrow();
, got result want.
try{ statement stmt=conn.createstatement(); string querystr="select * userinfo"; resultset rs=stmt.executequery(querystr); while(rs.next()) { string strname="name "+rs.getstring(3)+";"; string intage="age "+rs.getint(5)+";"; string strcompany="company "+rs.getstring(4)+".<br>"; //int rownum=rs.getrow(); out.println(strname+intage+strcompany); } out.println("succeed in visiting userinfo table."); }catch(sqlexception e){ out.println("failed query."); }
i don't want utilize rs.getrow();
. how should deal problem?
your code appear fine except string part.if using string hold record, wouldn't able display 7 records.
thus seek using collection object arraylist. , of course of study should close connections in block.
try{ statement stmt=conn.createstatement(); string querystr="select * userinfo"; resultset rs=stmt.executequery(querystr); list<string> list = new arraylist<string>(); while(rs.next()){ list.add(rs.getstring(3)); } system.out.println("rows "+list.size()); }catch(sqlexception e){ out.println("exception "+e.getmessage()); } finally{ rs.close(); stmt.close(); conn.close(); }
this illustration of how should retrieving values database using collection object. http://theopentutorials.com/tutorials/java/jdbc/how-to-retrieve-all-rows-from-mysql-table-using-jdbc/
java resultset
Comments
Post a Comment