【问题标题】:Checking room availability using JTable and JCalendar throws SQL exception使用 JTable 和 JCalendar 检查房间可用性会引发 SQL 异常
【发布时间】:2016-07-25 11:56:50
【问题描述】:

我编写的程序检查酒店的可用房间。我使用 JCalendar 让用户输入日期,使用 JTable 显示可用房间。 问题是当我点击检查可用性按钮时,它会抛出一个 SQL 异常。

这里是sn-ps的代码:

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个问号,表示它需要4个参数;您只需指定 2 个参数。
  • 我注释掉了"or (departure_date &gt;= ? and arrival_date &lt; ?)"+这一行,但我现在收到MySQLSyntaxErrorException

标签: java mysql jtable jcalendar


【解决方案1】:

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

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

应该是这样的

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

【讨论】:

  • 谢谢,我刚试了一下,但它抛出了MySQLSyntaxErrorException
  • @Diksha,您可以在尝试根据此答案修复它之后使用您拥有的代码编辑您的问题吗?
  • 注意:查询实际上应该是这样才能相应地工作:pStmt.setDate(3, sqlDate); pStmt.setDate(4, sqlDate1);
【解决方案2】:

行中缺少空格:

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

在字符串末尾加一个空格

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

【讨论】:

  • 它仍然抛出MySQLSyntaxErrorException
  • 我编辑了我的答案,还有一行没有空格,最好都检查一下
猜你喜欢
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-23
  • 2019-07-16
  • 2016-08-12
  • 2021-03-18
相关资源
最近更新 更多