SQLException:没有为参数1指定值

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

java.sql.SQLException:没有为参数1指定值

这是什么意思?

我的用户组列表在我的dao中:

 public List select(Integer var) { List 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(); 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 getListOfUserGroups() { List usuariosGruposList = userGrpDAO.select(var2); listOfUserGroups = usuariosGruposList; return listOfUserGroups; } 

我的JSF页面:

  

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

此外,应用程序能够比其他应用程序更快地显示某些选定的结果,这很奇怪。 它与我在开头指出的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()方法背后的代码引起的。 它显然是附加一个? 在委托到真正的 connection.prepareStatement()方法之前到SQL字符串。

您可能不想听到这个,但您的JDBC方法完全被破坏了。 此设计表明JDBC Connection是作为线程安全的静态或实例变量保存的

您需要完全重写它,以便归结为以下正确用法和变量范围:

 public List select(Integer idGrupo) throws SQLException { Connection connection = null; PreparedStatement statement = null; ResultSet resultSet = null; List usuariousGrupos = new ArrayList(); 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; } 

也可以看看:

  • 如何在Java中声明全局静态类?

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

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

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

也可以看看:

  • 为什么JSF多次调用getter

通常在使用预准备语句时会出现此类错误,并且忘记使用索引1设置参数。

在这种情况下,您正在使用预准备语句,但没有任何准备,您可以手动创建查询字符串。

此外,您可能会遇到其他问题,因为您在连字符之间连接了一个Integer 。 没有它们的数字值。

所以,它应该是这样的:

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

但实际上你应该使用像getStatement()这样的东西或正确使用getPreparedStatement()(在var和setInteger()的位置放置一个?来放置它。

所以最终的解决方案是:

 stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = ?;"); stmt.setInt(1, var); 

如果你使用

 stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo = ?); 

你必须

 stmt.setInt(1, var); 

之前

 ResultSet rs = stmt.executeQuery(); 

为第一个参数赋值(即参数1)。 如果不是,您的例外将会发生。