使用 JTable 和 JCalendar 检查房间可用性抛出 SQL 异常

Checking room availability using JTable and JCalendar throws SQL exception

我编写的程序检查酒店的可用房间。我使用 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();
    }      
  }   
}

GUI 如下所示:

当我在 MySQL 数据库上执行查询(日期与我在 JCalendar 上输入的日期相同)时,这就是显示的内容,应该显示在 JTable 上:

这是我遇到的错误:

我认为你应该为每个'?'设置参数在查询中。有四个 '?'占位符,但你只设置了其中两个

    pStmt.setDate(1, sqlDate);
    pStmt.setDate(2, sqlDate1);

应该是这样的

pStmt.setDate(1, sqlDate);
pStmt.setDate(2, sqlDate1);
pStmt.setDate(3, sqlDate1);
pStmt.setDate(4, sqlDate);

行中缺少 space:

"where (arrival_date >= ? and departure_date < ?)"+
"select * from room as ro" +

在字符串末尾加一个space就可以了

"where (arrival_date >= ? and departure_date < ?) "+
"select * from room as ro " +