java - Print an arraylist with database query result -
this question has answer here:
- what's simplest way print java array? 23 answers
i trying data oracle db , arraylist. when try print arraylist get, think is, direction of memory or that, not value on db.
something this:
com.packagename.somebean@1d251891
com.packagename.somebean@48140564
com.packagename.somebean@58ceff1
what missing?
public static void main(string[] args) { connection con = null; statement stmt = null; resultset rs = null; list<somebean> v = new arraylist<somebean>(); string query = "select * table rownum between 1 , 3"; try { class.forname("oracle.jdbc.driver.oracledriver"); con = drivermanager.getconnection("jdbc:oracle:thin:user/pass@localhost:port:sid"); stmt = con.createstatement(); rs = stmt.executequery(query); while( rs.next() ){ somebean n = new somebean(); n.setcolumn1(rs.getint("column1")); n.setcolumn2(rs.getstring("column2")); n.setcolumn3(rs.getstring("column3")); ... v.add(n); } for(somebean s : v){ system.out.println(s); } } catch (classnotfoundexception e) { e.printstacktrace(); } catch (sqlexception e) { e.printstacktrace(); } { try { stmt.close(); con.close(); } catch (sqlexception e) { e.printstacktrace(); } }
somebean class
public class somebean { protected int column1; protected string column2; protected string column3; public int getcolumn1() { return column1; } public void setcolumn1(int column1) { this.column1= column1; } public string getcolumn2() { return column2; } public void setcolumn2(string column2) { this.column2= column2; } public string getcolumn3() { return column3; } public void setcolumn3(string column3) { this.column3 = column3; } }
edit: did this:
for(somebean s : v) { string column1 = s.getcolumn1(); string column2 = s.getcolumn2(); string column3 = s.getcolumn3(); }
you need override tostring()
in somebean
class, e.g.:
@override public string tostring(){ //add code here }
Comments
Post a Comment