在SQL Java中使用phmyadmin的参数索引超出范围。

问题描述 投票:0回答:1

我的版本表有8个属性,在插入一个项目(一对多关系,项目-版本)时,所有属性都被填充。如果我有完整的字段,为什么会出现这个错误?或者说,如何将值加载到依赖于另一个(项目)的表(版本)中?

(codeProject是FK -Cascade-)

参数指数超出范围(8 >参数数量,是7)。版本DAO中出错

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;
    }

表项目(西班牙文Proyecto)。

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;
    }
java sql database phpmyadmin
1个回答
0
投票

我看到你的上层SQL语句中有7个问号,但8次调用ps.setXXXX()。


0
投票

对于第一个代码,而不是你应该从请求中得到项目的代码。

//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
        }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.