如何从选择查询的结果集创建Java对象?

时间:2016-11-25 06:38:40

标签: java sql jdbc

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

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

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

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 ---- 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中也是如此。

感谢您查看此代码。能帮到我吗?谢谢你。

2 个答案:

答案 0 :(得分:4)

您正在返回错误的对象(另请参阅我的内联注释)

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;
}

此外,如果您正确缩进代码,则更容易阅读和调试

答案 1 :(得分:0)

您应该返回flight1对象而不是flight,如下所示:

public static Flight selectFlight() 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();
    if(rs.next()){
        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;
}

另外,请勿将while用于单个记录,而应使用if作为单个记录。