SQLException:没有为参数1指定值

Sha*_*unK 8 java jsf jdbc

我在执行应用程序时遇到以下错误:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的托管bean中的用户组列表:

public List<UsuariousGrupos> getListOfUserGroups() {
    List<UsuariousGrupos> usuariosGruposList = userGrpDAO.select(var2);
    listOfUserGroups = usuariosGruposList;
    return listOfUserGroups;
}
Run Code Online (Sandbox Code Playgroud)

我的JSF页面:

 <p:dataTable var="usergroups" value="#{usuariousGruposBean.listOfUserGroups}">
     <p:column headerText="" style="height: 0" rendered="false">
         <h:outputText value="#{usergroups.id_grupo}"/>
     </p:column>
Run Code Online (Sandbox Code Playgroud)

我的数据表能够显示数据库中的组列表.但是,当我在数据表中选择单个行时,应用程序与我的数据库建立连接以显示所选结果需要一些时间.

此外,应用程序能够比其他应用程序更快地显示某些选定的结果,这很奇怪.它与我在开头指出的异常有什么关系吗?

错误:

Disconnected
Connected!!
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)
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 13

目前,因为没有这样的方法Connection()getPreparedStatement()java.sql.Connection.

conn.Connection();
stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");
Run Code Online (Sandbox Code Playgroud)

conn显然是围绕JDBC代码的本土包装器.您的特定问题可能是由该getPreparedStatement()方法背后的代码引起的.它显然?在委托给真正的 connection.prepareStatement()方法之前附加了一个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;
}
Run Code Online (Sandbox Code Playgroud)

也可以看看:


具体问题无关,你还有另一个问题.以下例外

javax.el.E​​LException:/index.xhtml @ 61,99 value ="#{usuariousGruposBean.listOfUserGroups}":在类型br.view.UsuariousGruposBean上读取'listOfUserGroups'时出错

表示您在getter方法中执行JDBC内容而不是(post)构造函数或(action)侦听器方法.这也是一个非常糟糕的主意,因为在渲染响应期间可以多次调用getter.相应地修复它.

也可以看看:

  • 发现它的+1实际上不是JDBC而是包装器 (3认同)