java - SQL异常 : No value specified for parameter 1

标签 java jsf jdbc

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

java.sql.SQLException: No value specified for parameter 1

这是什么意思?

我的 dao 中的 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>

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

此外,奇怪的是,该应用程序能够比其他应用程序更快地显示某些选定的结果。跟我一开始说的Exception有关系吗?

错误:

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)

最佳答案

java.sql.Connection 上没有Connection()getPreparedStatement() 这样的方法.

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

conn 显然是 JDBC 代码的本地包装器。您的特定问题可能是由 getPreparedStatement() 方法背后的代码引起的。在委托(delegate)给真实 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;
}

另见:


与具体问题无关,您还有另一个问题。以下异常

javax.el.ELException: /index.xhtml @61,99 value="#{usuariousGruposBean.listOfUserGroups}": Error reading 'listOfUserGroups' on type br.view.UsuariousGruposBean

表示您正在 getter 方法而不是 (post)constructor 或 (action)listener 方法中执行 JDBC 操作。这也是一个非常糟糕的主意,因为在渲染响应期间可以多次调用 getter。相应地修复它。

另见:

关于java - SQL异常 : No value specified for parameter 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8182880/

相关文章:

java - SQLException 是否发生取决于我们检索的值

java - 对给定范围 [a,b] 中的奇数求和?

java - 如何按值设置选定的索引JComboBox

java流通过终端操作改变数据

java - 将 JSP Web 应用程序迁移到 JSF

jsf - 选择子项时未选择 Primefaces 树顶父节点

css - 将 styleClass 应用于父 panelGrid 元素而不是子元素

java - 带有规范/谓词的JPA findAll上的NPE

mysql - RxJava-Jdbc 0.5.7 : How to obtain last insert id?

java - JDBC中如何只设置一些参数就可以调用存储过程