0

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.column3 = column3;
    }
}

EDIT: I finally did it like this:

for(SomeBean s : v) { String column1 = s.getColumn1(); String column2 = s.getColumn2(); String column3 = s.getColumn3(); }

Rodrick
  • 595
  • 10
  • 27
  • http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4/29140403 – Reimeus Feb 08 '16 at 14:16

1 Answers1

1

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

@Override
public String toString(){
 //add your code here
}
Austin
  • 8,018
  • 2
  • 31
  • 37