INSERT INTO语句中的语法错误

时间:2014-06-07 08:15:40

标签: java ms-access insert-into

我决定添加"添加"按钮,将员工记录添加到我的Access数据库,员工表。

每当我点击"添加"按钮,出现错误消息" INSERT INTO语句中的语法错误"。有人可以帮忙吗?我的编码有问题吗?因为我确实非常关注这个视频,即使在视频中,这个人成功地成功了#34; Saved"记录到数据库,我不断收到错误。

conn = Connect.ConnectDB();
        String sql = "insert into Employee ("
                +"Employee ID,"
                +"Employee Name,"
                +"Employee NICN,"
                +"Employee Gender,"
                +"Employee Contact Number,"
                +"Employee Department)"
                +"values("+txteid.getText()+ ",'"+txtename.getText()+"','"+txtenicn.getText()+"','"+txtegender.getText()+"','"+txtecnumber.getText()+"','"
                +txtedept.getText()+"')";
        try{
            pst = conn.prepareStatement(sql);
            pst.execute();
            JOptionPane.showMessageDialog(null, "Added");

        }catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }

1 个答案:

答案 0 :(得分:1)

您使用预准备语句,因此您必须使用参数

   conn = getConnection();
   String sql = "insert into Employee ("
                +"Employee ID,"
                +"Employee Name,"
                +"Employee NICN,"
                +"Employee Gender,"
                +"Employee Contact Number,"
                +"Employee Department)"
                +"values(?,?,?,?,?,?)";

然后设置参数:

      pstmt.setInt(1, txteid.getText()); // set input parameter 1
      pstmt.setString(2, txtename.getText()); // set input parameter 2
 ...
      pstmt.executeUpdate(); // execute insert statement