SQLException:没有为参数1指定值 [英] SQLException: No value specified for parameter 1
问题描述
我在执行应用程序时遇到以下错误:
I encountered the following error when I was executing my application:
java.sql.SQLException:没有为参数1指定值
java.sql.SQLException: No value specified for parameter 1
这是什么意思?
我的 UserGroup
我的名单中的列表:
My UserGroup
list in my dao:
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中用户组的获取列表:
My get list of User groups in my managed 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>
我的数据表能够显示数据库中的组列表。但是,当我在数据表中选择单个行时,应用程序与我的数据库建立连接以显示所选结果需要一些时间。
My data table is able to display the list of groups from the database. However, when I select an individual row within my data table, it takes some time for the application to establish connection with my database to display the selected result.
此外,它奇怪的是,应用程序能够比其他应用程序更快地显示某些选定的结果。它与我在开头指出的异常有什么关系吗?
Also, it is weird that the application is able to display certain selected results quicker than others. Does it have anything to do with the Exception I pointed out at the beginning?
错误:
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)
推荐答案
没有 Connection()
和<$ c这样的方法$ c> getPreparedStatement() on java.sql.Connection
。
There is no such method as Connection()
and getPreparedStatement()
on java.sql.Connection
.
conn.Connection();
stmt = conn.getPreparedStatement("select id_usuario, id_grupo from usuarios_grupos where id_grupo ='" + var + "'");
conn
显然是一个本土的包装纸JDBC代码。您的特定问题可能是由 getPreparedStatement()
方法背后的代码引起的。在委派给 real ?附加到SQL字符串中javase / 6 / docs / api / java / sql / Connection.html#prepareStatement%28java.lang.String%29rel =nofollow noreferrer> connection.prepareStatement()
方法。
The conn
is clearly a homegrown wrapper around JDBC code. Your particular problem is likely caused by the code behind the getPreparedStatement()
method. It's apparently appending a ?
to the SQL string before delegating through to the real connection.prepareStatement()
method.
您可能不想听到这个,但您的JDBC方法完全被破坏了。此设计表明JDBC Connection
是作为静态或实例变量保存的,它是 threadunsafe 。
You probably don't want to hear this, but your JDBC approach is totally broken. This design indicates that the JDBC Connection
is hold as a static or instance variable which is threadunsafe.
您需要完全重写它,以便归结为以下正确用法和变量范围:
You need to totally rewrite it so that it boils down to the following proper usage and variable scoping:
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;
}
另请参阅:
- 如何在Java中声明一个全局静态类?
- How to declare a global static class in Java?
- Why JSF calls getters multiple times
See also:
对于具体问题没有相关性,你还有另一个问题。以下异常
Unrelated to the concrete question, you've another problem. The following exception
javax.el.ELException:/index.xhtml @ 61,99 value =#{usuariousGruposBean.listOfUserGroups}:在类型br.view.UsuariousGruposBean上读取'listOfUserGroups'时出错
javax.el.ELException: /index.xhtml @61,99 value="#{usuariousGruposBean.listOfUserGroups}": Error reading 'listOfUserGroups' on type br.view.UsuariousGruposBean
表示您正在 getter中执行JDBC内容方法而不是(post)构造函数或(action)侦听器方法。这也是一个非常糟糕的主意,因为在渲染响应期间可以多次调用getter。相应修复。
indicates that you're doing the JDBC stuff inside a getter method instead of (post)constructor or (action)listener method. This is also a very bad idea because a getter can be called more than once during render response. Fix it accordingly.
这篇关于SQLException:没有为参数1指定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!