Connection to JDBC is working fine. This is the code which accesses database tables. File name –
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;
}
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;
}
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;
}
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.