如何从选择查询的结果集中创建Java对象? [英] How do I create a Java object from the resultset of a select query?

查看:109
本文介绍了如何从选择查询的结果集中创建Java对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与JDBC的连接工作正常.这是访问数据库表的代码.文件名–

Connection to JDBC is working fine. This is the code which accesses database tables. File name –

FlightDB.java 架构– Flights1(flno int,时间时间戳)

FlightDB.java Schema – Flights1(flno int, time timestamp)

public static Flight selectFlight(Flight flight) throws SQLException{
    PreparedStatement ps = null;
    ResultSet rs = null;
    String q1 = "Select * from Flights1 f order by f.time";
    Flight flight1 = null;
    try{
        ps = connection.prepareStatement(q1);
        rs = ps.executeQuery();
        while(rs.next()){
            Flight flight1 = new Flight();
            flight1 = new Flight();
            flight1.setflno(rs.getInt(1));
            flight1.settime(rs.getTimestamp(2));
            // System.out.println("new flight:" +flight1.getflno()); Correct output printed here
        }
    }
    finally{
        closeResultSet(rs);
        closePreparedStatement(ps);
    }
    return flight;
}

这是顶级代码的一部分------------文件名:Display.java

And this is a part of top-level code------------ File name : Display.java

static Flight showFlights(ResultSet rs) throws SQLException {
    Flight flight1 = new Flight();
    AirDB.selectFlight(flight1);
    // flight1.setflno(rs.getInt(1));
    // flight1.settime(rs.getTimestamp(2));
    System.out.println("New flight " + flight1.getflno());//OP: New flight 0
    return flight1;
}

这是我的班机---- Flight.java

And this is my class Flight ---- Flight.java

public Flight() {
    flno = 0;
    time = null;
}

public Flight(int flno ,Timestamp time)
{
    this.flno = flno;
    this.time = time;
}

public int getflno(){
    return flno;
}

public void setflno(int flno){
    this.flno = flno;
}

public Timestamp gettime(){
    return time;
}

public void settime(Timestamp time){
    this.time = time;
}

我得到0(默认值)作为我的输出,这是不正确的.我想从顶级java文件中打印输出.我尝试使用flight1 = AssignFlights1.showFlights(rs);在FlightDB.java中也是如此.

I get 0(default value) as my output which is not correct. I want to print the output from the top-level java file. I tried using flight1 = AssignFlights1.showFlights(rs); in FlightDB.java too.

感谢您查看此代码.你能帮我这个忙吗?谢谢.

Thank you for looking at this code. Can you please help me in this. Thanks you.

推荐答案

您返回了错误的对象(另请参见我的内联注释)

You are returning the wrong Object (also see my inline comments)

尝试

public static Flight selectFlight() throws SQLException{  // no param needed
  PreparedStatement ps = null;
  ResultSet rs = null;

  // I guess that this will not be the query you want in the end
  String q1 = "Select * from Flights1 f order by f.time";        
  Flight flight1 = null;
  try{
    ps = connection.prepareStatement(q1);
    rs = ps.executeQuery();
    if (rs.next()){  // only returning one object so no needed for while
      flight1 = new Flight();
      flight1.setflno(rs.getInt(1));
      flight1.settime(rs.getTimestamp(2));
      System.out.println("new flight:" +flight1.getflno()); Correct output printed here
    }
  }
  finally{
    closeResultSet(rs);
    closePreparedStatement(ps);
  }
  return flight1;
}

如果您正确地缩进代码,那么阅读和调试也将更加容易

Also if you correctly indent your code it is alot easier to read and debug

这篇关于如何从选择查询的结果集中创建Java对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆