简体   繁体   English

使用JDBC的SQL语法错误

[英]SQL syntax error using jdbc

I don't know what is the error in this section of could please help 我不知道这部分的错误是什么,请帮忙

here is my code 这是我的代码

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

and here is the error 这是错误

    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

my teamparnter wrote this code for MSSQL but I want now use it under Mysql SGBD I find this problem any suggestion please 我的teamparnter为MSSQL编写了此代码,但我现在想在Mysql SGBD下使用它,我发现此问题有任何建议

You must have an error somewhere in that long SQL statement, or one of the parameters you are passing contains something messing up the statement. 您必须在该长SQL语句中的某处出现错误,或者要传递的参数之一包含使该语句混乱的内容。

You should never use that kind of method for SQL inserts. 对于SQL插入,永远不要使用这种方法。 Use prepared statements: Prepared Statements 使用准备好的语句: 准备好的语句

Prepared statements makes the code way cleaner, and prevents things like SQL injections. 准备好的语句使代码更简洁,并防止SQL注入之类的事情。 Implement that and you should be able to fix your insert statement 实现这一点,您应该能够修复您的插入语句

A SQL statement can only contain ONE statement, your code is trying to execute insert into Employe(post) values (...)insert into Personne(... . SQL语句只能包含ONE语句,您的代码正在尝试执行insert into Employe(post) values (...)insert into Personne(...

This must be divided in two SQL commands, executed separately: insert into Employe(post) values (...) and insert into Personne(... . You can use the same Statement instance, but executeUpdate must be called two times. 这必须分为两个SQL命令,分别执行: insert into Employe(post) values (...)insert into Personne(... insert into Employe(post) values (...) ,您可以使用相同的Statement实例,但是executeUpdate必须调用两次。

And it is indicated to use a PreparedStatement as suggested by Jamal H . 并且指示使用Jamal H建议的PreparedStatement。

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

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