Save parameters of a method in a variable

0

I have the following code that is part of a class that handles the events of a frame :

public void actionPerformed(ActionEvent ae) {
if(ae.getSource()== NuevoEstudiante.btnGuardar){

    String nombre= nuevoEstudiante.txtNombre.getText();
    String materia= nuevoEstudiante.txtMatricula.getText();
    String nota= nuevoEstudiante.txtNota.getText();

    String datos= DAO.guardar(new Estudiante(nombre, materia, Integer.parseInt(nota)));

    if(datos!= null){
        JOptionPane.showMessageDialog(null, datos);
    }else{
        JOptionPane.showMessageDialog(null, "Registro Erroneo.");                
    }
  }
}

When I try to save the parameters received in the data variable

String datos= DAO.guardar(new Estudiante(nombre, materia, Integer.parseInt(nota)));

Netbeans gives me an error saying: incompatible types: void can not be converted to String , can you tell me how I can solve this error or if I have to redo my code in another way, I'll thank you for an example.

The save method used is part of a DAO as follows:

public void guardar(Estudiante e) {
  Connection con = null;
  PreparedStatement pstmnt = null;

try {
    con = dbcon.conectar();

    String sql = "INSERT INTO notas(nombre, matricula, nota) "
            + "VALUES(?, ?, ?)";

    pstmnt = con.prepareStatement(sql);

    pstmnt.setString(1, e.getNombre());
    pstmnt.setString(2, e.getMatricula());
    pstmnt.setInt(3, e.getNota());

    pstmnt.execute();

} catch (SQLException ex) {
    //System.out.println("Error al conectar a la BD");
    JOptionPane.showMessageDialog(null, "Error al conectar a la BD");
}finally {
    dbcon.desconectar(con);
 }
}
    
asked by David Calderon 07.09.2016 в 21:00
source

1 answer

2

The error is in how you call the save method, since it is an empty method (Void) what you can do is simply call the save method without creating a String. you have this.

    String datos= DAO.guardar(new Estudiante(nombre, materia, Integer.parseInt(nota)));

You would leave it like that.

   DAO.guardar(new Estudiante(nombre, materia, Integer.parseInt(nota)));

Another tip, the save method you should return it boolean, so that you return true or false, depending on whether or not you could insert the student.

    
answered by 07.09.2016 в 21:14