簡體   English   中英

如何從選擇查詢的結果集中創建Java對象?

[英]How do I create a Java object from the resultset of a select query?

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

感謝您查看此代碼。 你能幫我嗎 謝謝。

您返回了錯誤的對象(另請參見我的嵌入式注釋)

嘗試

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

另外,如果您正確縮進代碼,則閱讀和調試起來也很容易

您應該返回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用於單條記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM