query in Sql with JDBC
In my table, I have ta开发者_高级运维ken capacity in this form. `capacity DECL(3,0) UNSIGNED
Is it correct??I am not sure if the above method is correct to do it in this way? if I have to do it with PreparedStatement, how would it work?
First of all your SQL query is incorrect.
SELECT max(f.capacity)-COALESCE(SUM(r.num_seats),0) AS seats_avail
FROM flights f,
reservations r
WHERE f.flight_no="CSC585"
AND r.flight_no=f.flight_no
AND r.departure_date="2010-05-31";
Java code will looks like
String sql = " SELECT max(f.capacity)-COALESCE(SUM(r.num_seats),0) AS seats_avail
FROM flights f,
reservations r
WHERE f.flight_no=?
AND r.flight_no=f.flight_no
AND r.departure_date=?";
final PreparedStatement stmt = connectionHolder.prepareStatement(sql);
stmt.setString(1, "CSC585");
stmt.setString(2, "2010-05-31");
ResulSet rs = stmt.executeQuery();
while ( rs.next() ) {
String seats = rs.getString("seats_avail");
System.out.println(seats );
}
精彩评论