无法发现SQL语法错误

时间:2013-04-24 14:13:11

标签: java mysql sql jdbc syntax-error

每当我尝试插入,删除和更新时,我都会收到以下语法错误。但在关闭异常jdialog并重新启动应用程序后,数据将被插入,更新和删除

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your  mariaDB server for the right syntax to use near 'Customer' at line 1

插入查询

try {

            String sql = "Insert into Customer(ID , FirstName, MiddleName, LastName, DOB, Gander, Address, City, PostCode) "
                    + "values (?, AES_ENCRYPT(?, 'uk112'),AES_ENCRYPT(?, 'uk112'),"
                    + "AES_ENCRYPT(?, 'uk112'),?,AES_ENCRYPT(?, 'uk112'),"
                    + "AES_ENCRYPT(?, 'uk112'),AES_ENCRYPT(?, 'uk112'),"
                    + "AES_ENCRYPT(?, 'uk112'))";

            pst = newconn.prepareStatement(sql);
            pst.setString(1, jTextFieldID.getText());
            pst.setString(2, jTextFieldFirstname.getText());
            pst.setString(3, jTextFieldMiddlename.getText());
            pst.setString(4, jTextFieldLastname.getText());
            pst.setString(5, ((JTextField)txt_Date.getDateEditor().getUiComponent()).getText());
            pst.setString(6, Gander);
            pst.setString(7, jTextFieldAddress.getText());
            pst.setString(8, jTextFieldCity.getText());
            pst.setString(9, jTextFieldPostcode.getText());

            pst.execute();
            newconn.close();
            JOptionPane.showMessageDialog(null, "Saved");
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }

更新查询

try{

    String value1 =  jTextFieldID.getText();
    String value2 = jTextFieldFirstname.getText();
    String value3 = jTextFieldMiddlename.getText();
    String value4 = jTextFieldLastname.getText();
    String value5 = ((JTextField)txt_Date.getDateEditor().getUiComponent()).getText();
    String value6= Gander.toString();
    String value7= jTextFieldAddress.getText();
    String value8 = jTextFieldCity.getText();
    String value9 = jTextFieldPostcode.getText();    
String sql = "update  Customer set FirstName= AES_ENCRYPT(?,'uk112'),"
        + "MiddleName= AES_ENCRYPT(?,'uk112'),"
        + "LastName= AES_ENCRYPT(?,'uk112'),"
        + "DOB=?,Gander= AES_ENCRYPT(?,'uk112'),"
        + "Address= AES_ENCRYPT(?,'uk112'),"
        + "City= AES_ENCRYPT(?,'uk112'),"
        + "PostCode= AES_ENCRYPT(?,'uk112') where ID=?";

    pst = newconn.prepareStatement(sql);
    pst.setString(1, value2);
    pst.setString(2, value3);
    pst.setString(3, value4);
    pst.setString(4, value5);
    pst.setString(5, value6);
    pst.setString(6, value7);
    pst.setString(7, value8);
    pst.setString(8, value9);
    pst.setString(9, value1);
    pst.execute();
    newconn.close();
     JOptionPane.showMessageDialog(null, "Data is updated");

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

删除查询

  String sql = "delete from Customer Where ID =?";
        try{

           pst= newconn.prepareStatement(sql);

          pst.setString(1, jTextFieldID.getText());
           pst.execute();
           JOptionPane.showMessageDialog(null, "Deleted");
       }
       catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
       }

0 个答案:

没有答案