我在数据库中的表中插入值时遇到问题

时间:2019-12-18 16:12:49

标签: javascript mysql database

这是我的代码

private void jButtonOKActionPerformed(ActionEvent evt) 
{
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    try {
        Class.forName("java.sql.DriverManager");
       Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/oop", "root", "");
       System.out.println("Database successfully connected! Proof: " +con.toString());

       int column = 0;
       int row = table.getSelectedRow();
       String value = table.getModel().getValueAt(row, column).toString();

       System.out.println(value);

       Statement stmt = con.createStatement();
       String query1 = "select * from package where packageID='"+value+"'";
      ResultSet rs=stmt.executeQuery(query1);

      while(rs.next()) {
          String packageID = rs.getString("packageID");
          String packageName = rs.getString("packageName");
          String location = rs.getString("locationPackage");
          String noPeoplePackage = rs.getString("noPeoplePackage");
          String datePackage = rs.getString("datePackage");
          String pricePackage = rs.getString("pricePackage");

          String query2 = ("insert into booking(locationBooking, noBookingPeople, dateBooking)+ values ('"+location+"','"+noPeoplePackage+"','"+datePackage+"')") ;
          int rss=stmt.executeUpdate(query2);
         //model.addRow(new Object[] {packageID, packageName,location,noPeoplePackage,datePackage,pricePackage});
      }




       // rs.close();
        //stmt.close();
        //con.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
}

应该从字段中获取值以插入数据库中。但是,这是输出 “您的SQL语法有错误,请检查与您的MariaDB相对应的手册,以正确使用'+ values(“ Langkawi Island”,“ 5”,“ 31/12/2019”)'''附近的语法。”

2 个答案:

答案 0 :(得分:0)

在values()之前删除+,并且无需在查询中使用单引号

答案 1 :(得分:-1)

尝试

"insert into booking values ("+location+","+noPeoplePackage+","+datePackage)";