使用数据库查询结果

时间:2016-02-08 14:11:22

标签: java oracle arraylist

我正在尝试从Oracle DB获取一些数据并将其转换为Arraylist。 但是当我尝试打印Arraylist时,我得到了,我认为它是什么,内存的方向或类似的东西,而不是DB上的值。

这样的事情:
com.packagename.SomeBean@1d251891
com.packagename.SomeBean@48140564
com.packagename.SomeBean@58ceff1

我缺少什么?

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类

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;
    }
}
编辑:我终于这样做了:

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

1 个答案:

答案 0 :(得分:0)

您需要覆盖toString()课程中的SomeBean,例如:

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