我编写的程序检查酒店的可用房间。我使用 JCalendar 让用户输入日期,使用 JTable 显示可用房间。问题是当我点击检查可用性按钮时,它会抛出一个 SQL 异常。
以下是代码片段:
public class Room_Availability extends JFrame {
//Note: Didn't add codes about instantiating panels, setting layouts and all
JLabel arr_date= new JLabel("Arrival Date:");
JLabel dep_date= new JLabel("Departure Date:");
JButton chk_btn= new JButton("Check Availability");
JDateChooser arrival = new JDateChooser();
JDateChooser departure = new JDateChooser();
JTable CheckAvail= new JTable();
Room_Availability(){
chk_btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
CheckAvailActionPerformed(evt);
}
});
}
private void CheckAvailActionPerformed(java.awt.event.ActionEvent evt) {
ResultSet rs = null;
Connection conn = null;
Statement stmt = null;
try {
// new com.mysql.jdbc.Driver();
Class.forName("com.mysql.jdbc.Driver").newInstance();
String connectionUrl = "jdbc:mysql://localhost:3306/testing?autoReconnect=true&useSSL=false";
String connectionUser = "root";
String connectionPassword = "admin";
conn = DriverManager.getConnection(connectionUrl, connectionUser,
connectionPassword);
stmt = conn.createStatement();
String query= "select * from room as ro" +
"where ro.room_id not in "+
" ("+
"select re.room_no"+
" from testing.booking as re "+
"where (arrival_date >= ? and departure_date < ?)"+
"or (departure_date >= ? and arrival_date < ?)"+
" )";
PreparedStatement pStmt = (PreparedStatement) conn.prepareStatement(query);
java.util.Date utilDate = arrival.getDate();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
java.util.Date utilDate1 = departure.getDate();
java.sql.Date sqlDate1 = new java.sql.Date(utilDate1.getTime());
pStmt.setDate(1, sqlDate);
pStmt.setDate(2, sqlDate1);
pStmt.setDate(3, sqlDate);
pStmt.setDate(4, sqlDate1);
rs = pStmt.executeQuery();
CheckAvail.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我在 MySQL 数据库上执行查询(与我在 JCalendar 上输入的日期相同)时,这就是显示的内容,应该显示在 JTable 上:
以下是我得到的错误: