【问题标题】:Parameter index out of range in SQL Java use phmyadminSQL Java中的参数索引超出范围使用phpmyadmin
【发布时间】:2020-08-08 11:34:57
【问题描述】:

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

(codeProject 是 FK -Cascade-)

参数索引超出范围(8 > 参数个数,即 7)。 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;
    }

表格项目(西班牙语中的 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】:

    我在上面的 SQL 语句中看到 7 个问号,但 ps.setXXXX() 调用了 8 个。

    【讨论】:

      【解决方案2】:

      对于第一个代码,您应该从请求中获取项目的代码。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-12
        • 1970-01-01
        • 2014-06-06
        • 1970-01-01
        • 2012-01-16
        • 2020-11-12
        • 1970-01-01
        相关资源
        最近更新 更多