Question

When debugging this part of my program, it keeps telling my prcProcedimientoAlmacenado (My stored procedure) is null. Here's the following piece of code, which connects the database to my code (It's a GUI interface where I put the values to be added to the database)

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int idFacultad;
    String connectionUrl= "jdbc:mysql://localhost:3306/basespro";
    Connection con = null;

try{
    CallableStatement prcProcedimientoAlmacenado=null;
    Class.forName("com.mysql.jdbc.Driver");
    con=(Connection) DriverManager.getConnection(connectionUrl,"root","bases13");
    //con.setAutoCommit(false);
    prcProcedimientoAlmacenado = con.prepareCall("{call addMateria(?,?,?)}");
    int i=Integer.parseInt(id_Materia.getText());
    System.out.println("   "+i);
    prcProcedimientoAlmacenado.setInt("idMateria",i);
    prcProcedimientoAlmacenado.setInt("idFacultad", 2);
    prcProcedimientoAlmacenado.setString("nombreMateria", nombre_Materia.getText());
    prcProcedimientoAlmacenado.execute();
    con.commit();
}catch(Exception ex){
    System.out.println(ex.getMessage()); 
}

The procedure is the following, but I tested it in MySQL Workbench and it does add. (To be more clear, the procedure takes 2 strings and an integer and adds it to the Table "Materia"

DELIMITER $$

CREATE DEFINER=`root`@`localhost` PROCEDURE `addMateria`(In idMa int, In idFa int, In     nomMa varchar(40))
BEGIN

INSERT INTO Materia(idMateria,idFacultad,nombreMateria) VALUES (idMa,idFa,nomMa);
END
Was it helpful?

Solution

Try to use this syntax:

...
prcProcedimientoAlmacenado.setInt(1,i);
prcProcedimientoAlmacenado.setInt(2, 2);
prcProcedimientoAlmacenado.setString(3, nombre_Materia.getText());
...

To execute query that update, delete or insert any data into the database, you have to use:

executeUpdate(query)

See Using Prepared Statements

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top