0

我编写的程序检查酒店的可用房间。我使用 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 上: 在此处输入图像描述

以下是我得到的错误:

在此处输入图像描述

4

2 回答 2

2

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

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

它应该是这样的

pStmt.setDate(1, sqlDate);
pStmt.setDate(2, sqlDate1);
pStmt.setDate(3, sqlDate1);
pStmt.setDate(4, sqlDate);
于 2016-04-05T13:46:05.330 回答
1

行中缺少空格:

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

只需在字符串末尾添加一个空格

"where (arrival_date >= ? and departure_date < ?) "+
"select * from room as ro " +
于 2016-04-05T13:51:42.910 回答