-1
            sqlQuery =
                    "INSERT INTO alumno (matricula, nombre, genero, fecha_nacimiento, telefono)"
                            + "\nVALUES ('" + matriculaAlumno + "','" + nombreAlumno + "','" + generoAlumno + "','" + fechaNacimientoAlumno + "','" + telefonoAlumno + "');"
                            + "\nSET @AlumnoID = LAST_INSERT_ID();"
                            + "\nINSERT INTO tutor(nombre, rfc, telefono)"
                            + "\nVALUES ('" + nombreTutor + "','" + rfcTutor + "','" + telefonoTutor + "');"
                            + "\nSET @TutorID = LAST_INSERT_ID();"
                            + "\nSELECT id_grado INTO  @GradoID FROM grado WHERE grado= '" + grado + "' AND nivel ='" + nivel + "';"
                            + "\nINSERT INTO inscripcion (id_alumno, id_grado, monto, pagado) VALUES (@AlumnoID, @GradoID" + ",'" + montoAPagar + "','" + montoPagado + "');"

This is the method I use to execute the query:


    public void executeQuery(String sqlQuery) {

        try {
            PreparedStatement preparedStatement = registro.prepareStatement(sqlQuery);
            preparedStatement.executeUpdate();
            JOptionPane.showMessageDialog(null, "Operación realizada correctamente.");
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "Fallo durante la ejecución de la operación");
        }

    }

I'm trying to execute this query in Java, but it gives me this error:

INSERT INTO alumno (matricula, nombre, genero, fecha_nacimiento, telefono)
VALUES ('20041221','Test use','1','2000-01-01','6181112233');
SET @AlumnoID = LAST_INSERT_ID();
INSERT INTO tutor(nombre, rfc, telefono)
VALUES ('Test tutor','FDHjh32342','6182223344');
SET @TutorID = LAST_INSERT_ID();
SELECT id_grado INTO  @GradoID FROM grado WHERE grado= '2' AND nivel ='Secundaria';
INSERT INTO inscripcion (id_alumno, id_grado, monto, pagado) VALUES (@AlumnoID, @GradoID,'1000','1000');

java.sql.SQLSyntaxErrorException: 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 'SET @AlumnoID = SCOPE_IDENTITY();
INSERT INTO tutor(nombre, rfc, telefono)
VA...' at line 3

When I execute the exact same query in MySQL (xampp), it runs with no problems. Any ideas?

Droe
  • 1
  • 4
  • How are you executing this in Java? Are you trying to execute multiple SQL statements through a method that expects only one statement? – khelwood May 27 '22 at 23:28
  • @khelwood I added the method in the post. – Droe May 27 '22 at 23:44
  • @BillKarwin Thank you, allowing multiqueries fixed the problem, sorry for the duplicate question. – Droe May 27 '22 at 23:49

0 Answers0