使用JTable和JCalendar检查房间可用性会引发SQL异常

时间:2016-04-05 13:41:47

标签: java mysql jtable jcalendar

我写的程序检查了酒店的房间。我使用JCalendar为用户输入日期,使用JTable显示可用的房间。 问题是,当我点击Check Availability按钮时,它会抛出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的外观: enter image description here

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

以下是我遇到的错误:

enter image description here

2 个答案:

答案 0 :(得分:2)

我认为你应该为每个&#39;?&#39;设置参数。在查询中。有四个&#39;?&#39;占位符但你只设置其中两个

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

它应该是这样的

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

答案 1 :(得分:1)

行中缺少空格:

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

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

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