skip to Main Content

My version table has 8 attributes, all are populated when inserting a Project (One to many relationship, projects – versions). Why do I get this error if I have the complete fields? Or how are values ​​loaded to a table (version) that depends on another (project)?

(codeProject is FK -Cascade-)

Parameter index out of range (8 > number of parameters, which is 7). Error in VersionDAO

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

    public int insertarVer(Version ver, Proyecto pro) {
        String sql = "INSERT INTO version (idVersion, nameVersion, efficVersion, acumCases, acumFail, acumTime, contPruebas, codeProject) VALUES (?,?,?,?,?,?,?,(SELECT code FROM proyecto))";
        try { 
            con = conectar.getConexionSQL();
            ps=con.prepareStatement(sql);
            ps.setInt(1, ver.getIdversion());
            ps.setString(2, ver.getNameVersion());
            ps.setDouble(3, 0.0);
            ps.setInt(4, ver.getAcumCasos());
            ps.setDouble(5, 0.0);
            ps.setDouble(6, 0.0);
            ps.setInt(7, ver.getContPruebas());
            ps.setInt(8, pro.getCod());
            ps.executeUpdate(); 
            return 1;

        } catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }

Table Project (Proyecto in spanish)

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

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

            con = conectar.getConexionSQL();
            ps=con.prepareStatement(sql);
            ps.setInt(1, p.getCod());
            ps.setString(2, "Prueba");
            ps.setInt(3, p.getStatus());
            ps.setString(4, "PHP");
            ps.setInt(5, p.getDuracion());
            ps.setInt(6, p.getAvance());
            ps.setDouble(7, 0.0);
            ps.executeUpdate();

            return 1;

        } catch(Exception e){
            e.printStackTrace();
        }
        return 0;
    }

2

Answers


  1. I see 7 question marks in your upper SQL statement but 8 calls of ps.setXXXX().

    Login or Signup to reply.
  2. for the first code, instead you should get the code of the project out of the request.

    //You should do something like this
    
    public class VersionDAO {
      // all you private attributes
    
      public int insertarVer(Version ver, Proyecto pro) {
    
        // you should avoid to make any calculation or getting value directly when you set values on the PreparedStatement
    
        int idVersion = ver.getIdversion();
        String nameVersion = ver.getNameVersion();
        int acumCasos = ver.getAcumCasos();
        int contPruebas = ver.getContPruebas();
        int code = pro.getCode(); // get the code of you project here
        String query = "INSERT INTO version(idVersion, nameVersion, efficVersion, acumCases, acumFail, acumTime, contPruebas, codeProject) VALUES (?,?,?,?,?,?,?,?)";
    
        try { 
                con = conectar.getConexionSQL();
                ps=con.prepareStatement(sql);
                ps.setInt(1, id);
                ps.setString(2, nameVersion);
                ps.setDouble(3, 0.0);
                ps.setInt(4, acumCasos);
                ps.setDouble(5, 0.0);
                ps.setDouble(6, 0.0);
                ps.setInt(7, contPruebas);
                ps.setInt(8, code);
                ps.executeUpdate(); 
                return 1;
    
            } catch(Exception e){
                e.printStackTrace();
                return 0; // change the return position other way you will always have 0 as retrun value
            }
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search