I can not get and send the data id of a combobox in JAVA pattern MVC to SQL

0

THIS IS MY JSP "INSERT":

<%  
    ProfesionalBE profesional = new ProfesionalBE();
    profesional.setNombre(request.getParameter("nombre"));
    profesional.setApellido(request.getParameter("apellido")); 
    profesional.setCorreo(request.getParameter("correo"));
    profesional.setId_nacionalidad(request.getParameter("idnacionalidad"));
    int b = new ProfesionalBR().RegistrarProfesional(profesional)    
%>

THIS IS MY DAO CLASS which calls when inserting:

 public int InsertarProfesional(ProfesionalBE e){
        int  r =0;
         Connection con = new Conexion().conectarSQL();
         String sql = "INSERT INTO Tbl_Profesional (Nombre_profesional, Apellidos_profesional, Correo_profesional, id_Nacionalidad)  "
                 + "VALUES ('"+e.getNombre()+"','"+e.getApellido()+"','"+e.getCorreo()+"','"+e.getId_nacionalidad()+"')";

         try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            rs.next();
            rs.close();
            st.close();
            con.close();
            st=null;     
        } catch (SQLException ex) {
            Logger.getLogger(ProfesionalDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
         con=null;
   return r;
}

My problem is that at the moment of inserting the DB I do not insert anything, because it does not read the id as a number, my query is how to get the id of a combobox with data displayed from a sql table to be able to register them in another table,

    
asked by Elert Cordova 27.10.2018 в 01:33
source

1 answer

0

You could use PreparedStatement to define the type of data that you will send to your query:

<%  
    ProfesionalBE profesional = new ProfesionalBE();
    profesional.setNombre(request.getParameter("nombre"));
    profesional.setApellido(request.getParameter("apellido")); 
    profesional.setCorreo(request.getParameter("correo"));
    profesional.setId_nacionalidad(request.getParameter("idnacionalidad"));
    int b = new ProfesionalBR().RegistrarProfesional(profesional)    
%>

public int InsertarProfesional(ProfesionalBE e){
        int  r =0;
         Connection con = new Conexion().conectarSQL();
         String sql = "INSERT INTO Tbl_Profesional (Nombre_profesional, Apellidos_profesional, Correo_profesional, id_Nacionalidad) 
         values (?,?,?,?)";

         PreparedStatement statement=cn.prepareStatement(sql);
         statement.setString(1,e.getNombre());
         statement.setString(2,e.getApellido());
         statement.setString(3,e.getCorreo());
         statement.setInt(4,e.getId_nacionalidad());
         statement.executeUpdate();

         try {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(sql);
            rs.next();
            rs.close();
            st.close();
            con.close();
            st=null;     
        } catch (SQLException ex) {
            Logger.getLogger(ProfesionalDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
         con=null;
   return r;
}

I hope and it works, greetings.

    
answered by 27.10.2018 в 01:57