SQLException:没有为参数1指定值
我在执行应用程序时遇到以下错误:
java.sql.SQLException:未为参数1指定值
这是什么意思?
我UserGroup在岛上的清单:
public List<UsuariousGrupos> select(Integer var) {    List<UsuariousGrupos> ug= null;
    try {
        conn.Connection();
        stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");
        ResultSet rs = stmt.executeQuery();
        ug = new ArrayList<UsuariousGrupos>();
        while (rs.next()) {
            ug.add(getUserGrupos(rs));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        conn.Disconnected();
    }
    return ug;
}
public UsuariousGrupos getUserGrupos(ResultSet rs) {
    try {
        UsuariousGrupos ug = new UsuariousGrupos(rs.getInt("id_usuario"), rs.getInt("id_grupo"));
        return ug;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}
我在托管bean中获得的用户组列表:
public List<UsuariousGrupos> getListOfUserGroups() {    List<UsuariousGrupos> usuariosGruposList = userGrpDAO.select(var2);
    listOfUserGroups = usuariosGruposList;
    return listOfUserGroups;
}
我的JSF页面:
 <p:dataTable var="usergroups" value="#{usuariousGruposBean.listOfUserGroups}">     <p:column headerText="" style="height: 0" rendered="false">
         <h:outputText value="#{usergroups.id_grupo}"/>
     </p:column>
我的数据表能够显示数据库中的组列表。但是,当我在数据表中选择单个行时,应用程序需要一些时间才能与数据库建立连接以显示所选结果。
另外,奇怪的是,该应用程序能够比其他应用程序更快地显示某些选定的结果。与我一开始指出的异常有什么关系吗?
错误:
DisconnectedConnected!!
java.sql.SQLException: No value specified for parameter 1
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
    at com.mysql.jdbc.PreparedStatement.checkAllParametersSet(PreparedStatement.java:2560)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2536)
    at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2462)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2216)
    at br.dao.UsuariousGruposDAO.select(UsuariousGruposDAO.java:126)
    at br.view.UsuariousGruposBean.getListOfUserGroups(UsuariousGruposBean.java:54)
SEVERE: Error Rendering View[/index.xhtml]
javax.el.ELException: /index.xhtml @61,99 value="#{usuariousGruposBean.listOfUserGroups}": Error reading 'listOfUserGroups' on type br.view.UsuariousGruposBean
    at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
    at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
回答:
目前,因为没有这样的方法Connection()与getPreparedStatement()上java.sql.Connection。
conn.Connection();stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");
conn显然,它是JDBC代码的本地包装。您的特定问题可能是由该getPreparedStatement()方法后面的代码引起的。显然?,在委派给_real_connection.prepareStatement()方法之前,将a附加到SQL字符串。
您可能不想听到这个消息,但是您的JDBC方法完全被打破了。这种设计表明JDBC Connection是作为 
的静态变量或实例变量 。
您需要完全重写它,以便归结为以下正确用法和变量作用域:
public List<UsuariousGrupos> select(Integer idGrupo) throws SQLException {    Connection connection = null;
    PreparedStatement statement = null;
    ResultSet resultSet = null;
    List<UsuariousGrupos> usuariousGrupos = new ArrayList<UsariousGrupos>();
    try {
        connection = database.getConnection();
        statement = connection.prepareStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = ?");
        statement.setInt(1, idGrupo);
        resultSet = statement.executeQuery();
        while (resultSet.next()) {
            usuariousGrupos.add(mapUsuariousGrupos(resultSet));
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
        if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
    }
    return usuariousGrupos;
}
具体问题 ,您还有另一个问题。以下异常
javax.el.ELException:/index.xhtml @ 61,99 value
=“#{usuariousGruposBean.listOfUserGroups}”:读取br.view.UsuariousGruposBean类型的’listOfUserGroups’时出错
表示您正在用 方法而不是(post)constructor或(action)listener方法执行JDBC
。这也是一个非常糟糕的主意,因为在渲染响应期间可以多次调用getter。相应地修复它。
以上是 SQLException:没有为参数1指定值 的全部内容, 来源链接: utcz.com/qa/426223.html




