Rodrick February 2016

Print an arraylist with database query result

I am trying to get some data from Oracle DB and get it into an Arraylist. But when I try to print the Arraylist I get, what I think it is, the direction of memory or something like that, not the value that is on DB.

something like this:
com.packagename.SomeBean@1d251891
com.packagename.SomeBean@48140564
com.packagename.SomeBean@58ceff1

What I am 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 * from table where ROWNUM BETWEEN 1 and 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();
} finally {
    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.c        

Answers


Austin D February 2016

You need to override toString() in your SomeBean class, e.g.:

@Override
public String toString(){
 //add your code here
}

Post Status

Asked in February 2016
Viewed 1,509 times
Voted 12
Answered 1 times

Search




Leave an answer