使用jdbc的SQL语法错误

时间:2017-06-26 17:27:50

标签: java mysql sql database

我不知道这部分中的错误可以请求帮助

这是我的代码

public void addEmploye(Employe employe, Service service) throws SQLException{
    int id_service = getServiceId(service);
    if(nbrPersonnes(employe.getCin())!=0)
        System.out.println("Employe deja existant verifier le cin");
    else{
     String SQL = "insert into Employe(post) "
            + "values ("
            + "'"+employe.getPost()+"')"

            + "insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_employe,id_service)"
            + "values('"+employe.getCin()+"',"
            + "'"+employe.getNom()+"',"
            + "'"+employe.getPrenom()+"',"
            + "'"+employe.getAdresse()+"',"
            + "'"+employe.getTel()+"',"
            + "'"+employe.getEmail()+"',"
            + "'"+employe.getPassword()+"',"
            + "0,"
            + " SELECT LAST_INSERT_ID() FROM `Personne`,"
            +id_service+")";
     if(id_service!=0)
         try {
             stmt = con.createStatement();  
             rs = stmt.executeUpdate(SQL);
        } catch (SQLException e) {
            System.out.println("addEmploye "+e.toString());
        }
    }
}

这是错误

    addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1
addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1
addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1
addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1
addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1
addEmploye com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'insert into Personne(cin,nom,prenom,adresse,tel,email,password,id_directeur,id_e' at line 1

我的teamparnter为MSSQL编写了这段代码,但我现在想在Mysql SGBD下使用它我发现这个问题有什么建议请

2 个答案:

答案 0 :(得分:1)

您必须在该长SQL语句中的某处出现错误,或者您传递的参数之一包含弄乱该语句的内容。

您绝不应该将这种方法用于SQL插入。使用准备好的陈述:Prepared Statements

预备语句使代码更清晰,并防止SQL注入之类的事情。实现它,你应该能够修复你的插入语句

答案 1 :(得分:1)

SQL语句只能包含一个语句,您的代码正在尝试执行insert into Employe(post) values (...)insert into Personne(...

这必须分为两个SQL命令,分别执行:insert into Employe(post) values (...)insert into Personne(...。您可以使用相同的Statement实例,但必须调用executeUpdate两次。

并且表示使用Jamal H建议的PreparedStatement。