6

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

6
  • You have no space between these two statements. + "'"+employe.getPost()+"')" + "insert into Personne( Commented Jun 26, 2017 at 17:32
  • I have always same error even with no space Commented Jun 26, 2017 at 17:37
  • 1
    Can you have 2 inserts in the same block? Don't you at least need a semi-colon? Commented Jun 26, 2017 at 17:39
  • 1
    Try printing out the statement and see if it works directly in MySQL. Commented Jun 26, 2017 at 17:39
  • @TedTrippin no and no - semi-colon is not valid in SQL statements (some drivers don't allow it, other just ignore it). Commented Jun 26, 2017 at 18:02

2 Answers 2

1

A SQL statement can only contain ONE statement, your code is trying to execute 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.

And it is indicated to use a PreparedStatement as suggested by Jamal H.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

You should never use that kind of method for SQL inserts. Use prepared statements: Prepared Statements

Prepared statements makes the code way cleaner, and prevents things like SQL injections. Implement that and you should be able to fix your insert statement

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.