2

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.

2 Answers 2

4

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

try

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your response. Sorry but I didn't mention, I have to return multiple rows here.
In that case add them to (and return an) ArrayList<Flight>
0

You should return flight1 object not flight as shown below:

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

Also, DO NOT use while for single record, rather use if for single record.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.