skip to Main Content

I did several tests, the view values ​​are saved in the model, but do not pass to the database.
The values ​​entered by screen are printed perfectly by console, but at the time of being entered into the database, it is not completed, nor have I been able to determine the error.

DAO class.

public class ProyectoDAO {
ConexionSQL conectar = new ConexionSQL();
Connection con;
PreparedStatement ps;
ResultSet rs;

    public int insert(Proyecto p) {
        String sql = "INSERT INTO proyecto (name, status, duration, advance) VALUES (?,?,?,?)";
        try {

            con = conectar.getConexionSQL();
            ps=con.prepareStatement(sql);
            ps.setString(2, p.getNombre());
            //ps.setString(2, "Test");
            ps.setInt(3, p.getStatus());
            ps.setInt(5, p.getDuracion());
            ps.setInt(6, p.getAvance());
            int registro = ps.executeUpdate();      
            return 1;

        } catch(Exception e){

        }
        return 0;
    }
}

Controller Proyecto

public class Cproyecto extends ConexionSQL {

    Proyecto p = new Proyecto();
    Vproyecto vp = new Vproyecto();
    Version ver = new Version();
    Connection con = getConexionSQL();
    ProyectoDAO pdao = new ProyectoDAO();
    VersionDAO vdao = new VersionDAO();

    public Cproyecto(Proyecto p) {
        super();
        this.vp.addRegistrarListener(new guardarValor()); 
        this.vp.addActualizarListener(new actualizarValor());
        this.vp.addBuscarListener(new buscarValor());
    }

    public void initProyecto() {
        vp.setVisible(true);
        vp.textID.setText(String.valueOf(p.getCod())); //Esto debe aparecer al abrir Registrar
    }

    // Register
        class guardarValor implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                try {

                    if (vp.textNombre.getText().isEmpty() || vp.textVersion.getText().isEmpty() || vp.textAvance.getText().isEmpty())
                        JOptionPane.showMessageDialog(null, "Debe rellenar todos los campos");
                    else {
                        //Save values
                        p.setNombre(vp.textNombre.getText());
                        p.setStatus(vp.comboEstado.getSelectedIndex());
                        p.setDuracion(vp.comboDuracion.getSelectedIndex());
                        p.setAvance(Integer.valueOf(vp.textAvance.getText()));
                        ver.setNameVersion(vp.textVersion.getText());
                        int rp = pdao.insertar(p);
                        int rv = vdao.insertarVer(ver,p);                       

                        } //end else
                } catch (Exception error) {
                    JOptionPane.showMessageDialog(null, "Ha ocurrido un error, consulte a Soporte Técnico");
                    vp.limpiar();
                }
            } //end void
        } //end class   
}

Connection

public class ConexionSQL {
    Statement stm;
    Connection con;


    public Connection getConexionSQL() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://127.0.0.1/analisisqa","root","");
            //Statement stm = con.createStatement();
        }
        catch(ClassNotFoundException exc) {
            exc.printStackTrace();
            JOptionPane.showMessageDialog(null, "No Conectado");
        }
        catch (SQLException ex) {
            Logger.getLogger(ConexionSQL.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "No Conectado");
        }
        return con;

    }
}

My main.


public class Main {

    public static void main(String[] args) {

        ConexionSQL con = new ConexionSQL();
        // TODO Auto-generated method stub
        Proyecto p = new Proyecto();
        Prueba pru = new Prueba();
        Version ver = new Version();

        CPrueba cpru = new CPrueba(pru, ver, p);
        Cproyecto cp = new Cproyecto(p);

        ProyectoDAO pdao = new ProyectoDAO();
        PruebaDAO prudao = new PruebaDAO();
        VersionDAO verdao = new VersionDAO();

        Cprincipal cprincipal = new Cprincipal(p, pru, ver, cpru, cp, con, pdao, prudao, verdao);
        cprincipal.initPrincipal();

    }

}

2

Answers


  1. Your SQL query seems to have 4 parameters:

    String sql = "INSERT INTO proyecto (name, status, duration, advance) VALUES (?,?,?,?)";
    

    But you’re settings something different

    ps.setString(2, p.getNombre());
    //ps.setString(2, "Test");
    ps.setInt(3, p.getStatus());
    ps.setInt(5, p.getDuracion());
    ps.setInt(6, p.getAvance());
    

    And then you ignore error

    } catch(Exception e){
    
    }
    

    Start by printing exception (or let it propagate). It will give you more details.
    Then fix parameter indices:

    ps.setString(1, p.getNombre());
    ps.setInt(2, p.getStatus());
    ps.setInt(3, p.getDuracion());
    ps.setInt(4, p.getAvance());
    
    Login or Signup to reply.
  2. Your Query looks like:

    String sql = "INSERT INTO proyecto (name, status, duration, advance) VALUES (?,?,?,?)";
    

    But you are settings wrong parameters:

     ps.setString(2, p.getNombre());
     //ps.setString(2, "Test");
     ps.setInt(3, p.getStatus());
     ps.setInt(5, p.getDuracion());
     ps.setInt(6, p.getAvance());
    

    Fix parameter indices:

    public class ProyectoDAO {
    ConexionSQL conectar = new ConexionSQL();
    Connection con;
    PreparedStatement ps;
    ResultSet rs;
    
    public int insert(Proyecto p) {
        String sql = "INSERT INTO proyecto (name, status, duration, advance) VALUES (?,?,?,?)";
        try {
    
            con = conectar.getConexionSQL();
            ps=con.prepareStatement(sql);
            ps.setString(1, p.getNombre());
            //ps.setString(2, "Test");
            ps.setInt(2, p.getStatus());
            ps.setInt(3, p.getDuracion());
            ps.setInt(4, p.getAvance());
            int registro = ps.executeUpdate(); 
    
    
            if(registro >= 1){
               return 1;
            }else{
              return 0;
            }
    
        } catch(Exception e){
                e.printStackTrace();
        }
        return 0;
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search