Problem I can not insert in java

0

I created two classes a connection for the BD and another Insumo that will have an insert method. I execute the code but nothing happens. I think it's class Conexion I'm not sure you could look at the code to see what the problem is

Click of the button

private void btn_ingresarMouseClicked(java.awt.event.MouseEvent evt){                                          
    Insumo obj= new Insumo();
    obj.setUsuario(txtrut.getText());
    obj.setPass(jPasswordField1.getText());
    try {
        obj.insertar();
    }
    catch(SQLException e){
        System.out.println(e.getMessage());
    }
}  

Class Conexion

public class Conexion {
    public static Connection con = null;

    public Connection Conectarse(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("Registro exitoso");
            Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/ropa_trabajo?"
        + "user=root&password=");
        } catch (Exception e) {
            System.out.println(e.toString()+"dsgdsh");
        }
        return con;
    }

    public void cerrar() throws SQLException {
        if(con!=null){
            con.close();
        }
    }
}

Class Insumo .

The input class I think is where the problem is

public class Insumo extends Conexion {
    private String usuario;
    private String pass;

    public Insumo(){
        super();
        con=super.Conectarse();
    }

    public void insertar() throws SQLException{
        String sql="INSERT INTO usuario(4,?,?)";
        PreparedStatement sentencia=super.Conectarse().prepareStatement(sql);
        sentencia.setString(1,getUsuario());
        sentencia.setString(2,getPass());
        sentencia.executeUpdate();
        super.cerrar();
    }

    /**
     * @return the usuario
     */
    public String getUsuario() {
        return usuario;
    }

    /**
     * @param usuario the usuario to set
     */
    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    /**
     * @return the pass
     */
    public String getPass() {
        return pass;
    }

    /**
     * @param pass the pass to set
     */
    public void setPass(String pass) {
        this.pass = pass;
    }
}
    
asked by jose miguel jara 02.01.2017 в 00:30
source

1 answer

0

In the class Connect your Connection variable with, you have it as static, there you are mixing two concepts, the static variables can not be accessed from methods that are not static.b Remove the declaration of the variable that you have within the try in the Conectarse method, since it is hiding the global variable.

In the insert method you reconnect, it is not necessary to just connect once and then query the variable and disconnect when you need or finish. I say you connect double because you do it in the constructor and then in the insert method of the Input class.

On the other hand, as a recommendation in the input class when you call the method of the parent class you use super, it is not necessary to do it because you automatically have Access when you inherit, as long as it is public or protected.

    
answered by 02.01.2017 / 00:58
source