SQLException:已用尽的结果集

我的JDBC代码有问题。 这是相关代码:

/** method for checking password into the Oracle database */ public String CheckUserDB(String userToCheck) throws SQLException { String storedPassword; if (ds == null) throw new SQLException("No data source"); Connection conn = ds.getConnection(); if (conn == null) throw new SQLException("No connection"); try { conn.setAutoCommit(false); boolean committed = false; try { PreparedStatement passwordQuery = conn.prepareStatement( "SELECT passwd from USERS WHERE userz = ?"); passwordQuery.setString(1, userToCheck); ResultSet result = passwordQuery.executeQuery(); result.next(); storedPassword = result.getString("passwd"); conn.commit(); committed = true; } finally { if (!committed) conn.rollback(); } } finally { conn.close(); } return storedPassword; } 

这是例外:

 java.sql.SQLException: Exhausted Resultset oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:1270) oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:494) org.jboss.jca.adapters.jdbc.WrappedResultSet.getString(WrappedResultSet.java:1359) com.dx.sr_57.user_check.CheckUserDB(user_check.java:100) com.dx.sr_57.user_check.user_compare(user_check.java:123) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:601) org.apache.el.parser.AstValue.invoke(AstValue.java:196) org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276) com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) javax.faces.component.UICommand.broadcast(UICommand.java:315) javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) 

这是怎么造成的,如何解决?

 result.next(); storedPassword = result.getString("passwd"); 

您没有检查下一个的返回值。 如果你没有行,你会遇到麻烦……

 if(result.next()){ storedPassword = result.getString("passwd"); } 

问题可能不在于代码,而是可能是数据库。 仔细检查TABLE是否为空。 如果表为空,则会出现此错误。 请记住,像Oracle这样的数据库需要在所有insert, update, alter statements之后commit在数据库之外可能看不到您的更改,直到您对数据库运行提交,我遇到了这个问题很长一段时间。 我继续使用select语句检查表,但我的oracle db的问题是我没有对我的数据库发出提交。

在oracle数据库中使用varchar数据类型而不是char

如果使用char,则将数据库空白的实际大小与查询参数进行比较

并且查询不会被执行,结果变为false。

因此,请在oracle数据库中使用varchar数据类型。