简体   繁体   English

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

[英]i have problem to insert value in a table in database

this is my code这是我的代码

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());
    }
}

it is supposed to take the value from the field to insert in the database.它应该从字段中获取值以插入到数据库中。 however, this is the output "You have an error in your SQL syntax, check the manual that corresponds to your MariaDB for the right syntaax use near '+values("Langkawi Island","5", "31/12/2019")'"但是,这是输出“您的 SQL 语法有错误,请检查与您的 MariaDB 对应的手册,以了解在 '+values("Langkawi Island","5", "31/12/2019" 附近使用正确的语法)'"

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

尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM