Spring JDBCTemplate 给出 InvalidResultSetAccessException 无效的列索引

发布于 2024-11-03 23:32:21 字数 7654 浏览 5 评论 0原文

我使用 Spring 的 SimpleJDBCTemplate 来访问 Oracle DB。这是我的代码。

String sql = "SELECT from_bin_code FROM hbin_import_mapping";
return jt.query(sql, new BeanHbinImportMappingMapper(), (Object) null);

行映射器是:

public class BeanHbinImportMappingMapper
    implements
        RowMapper<BeanHbinImportMapping> {
    public BeanHbinImportMapping mapRow(ResultSet rs, int rowno)
        throws SQLException {
        int fromBinCode = rs.getInt("from_bin_code");
//        char fromBinCodeChar = rs.getString("from_bin_code_char").charAt(0);
//        boolean fromBinCodeAllowed = rs.getString("from_bin_code_allowed")
//            .equals("Y") ? true : false;
//        int oliBinCode = rs.getInt("oli_bin_code");
//        String oliBinQuality = rs.getString("oli_bin_quality");
//        String oliBinGroup = rs.getString("oli_bin_group");
//        String oliBinDesc = rs.getString("oli_bin_desc");
//        boolean olibinRef = rs.getString("oli_bin_ref").equals("Y") ? true
//            : false;

        BeanHbinImportMapping bean = new BeanHbinImportMapping(fromBinCode);
//      BeanHbinImportMapping bean = new BeanHbinImportMapping(1, 'c', true);
        return bean;
    }
}

sql 已测试工作正常。

异常堆栈跟踪:

javax.servlet.ServletException: org.springframework.jdbc.InvalidResultSetAccessException: PreparedStatementCallback; invalid ResultSet access for SQL [SELECT from_bin_code FROM hbin_import_mapping]; nested exception is java.sql.SQLException: Invalid column index
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


root cause 

org.springframework.jdbc.InvalidResultSetAccessException: PreparedStatementCallback; invalid ResultSet access for SQL [SELECT from_bin_code FROM hbin_import_mapping]; nested exception is java.sql.SQLException: Invalid column index
    org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:237)
    org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:200)
    com.st.mas.wmr.persistence.atom.AtomStifOliBinConversion.getLoaderMatrix(AtomStifOliBinConversion.java:52)
    com.st.mas.wmr.persistence.process.ProcessStifOliBinConversion.getLoaderMatrix(ProcessStifOliBinConversion.java:50)
    com.st.mas.wmr.action.StifBinConversionAction.viewLoaderMapping(StifBinConversionAction.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    com.st.mas.wmr.action.AnyDispatchAction.execute(AnyDispatchAction.java:107)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


root cause 

java.sql.SQLException: Invalid column index
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:161)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:226)
    oracle.jdbc.driver.OraclePreparedStatement.setNullInternal(OraclePreparedStatement.java:3661)
    oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:3652)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108)
    org.springframework.jdbc.core.StatementCreatorUtils.setNull(StatementCreatorUtils.java:252)
    org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:213)
    org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
    org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
    org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
    org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:641)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:200)
    com.st.mas.wmr.persistence.atom.AtomStifOliBinConversion.getLoaderMatrix(AtomStifOliBinConversion.java:52)
    com.st.mas.wmr.persistence.process.ProcessStifOliBinConversion.getLoaderMatrix(ProcessStifOliBinConversion.java:50)
    com.st.mas.wmr.action.StifBinConversionAction.viewLoaderMapping(StifBinConversionAction.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    com.st.mas.wmr.action.AnyDispatchAction.execute(AnyDispatchAction.java:107)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

谢谢。

I use Spring's SimpleJDBCTemplate to access Oracle DB. Here is my code.

String sql = "SELECT from_bin_code FROM hbin_import_mapping";
return jt.query(sql, new BeanHbinImportMappingMapper(), (Object) null);

The row mapper is:

public class BeanHbinImportMappingMapper
    implements
        RowMapper<BeanHbinImportMapping> {
    public BeanHbinImportMapping mapRow(ResultSet rs, int rowno)
        throws SQLException {
        int fromBinCode = rs.getInt("from_bin_code");
//        char fromBinCodeChar = rs.getString("from_bin_code_char").charAt(0);
//        boolean fromBinCodeAllowed = rs.getString("from_bin_code_allowed")
//            .equals("Y") ? true : false;
//        int oliBinCode = rs.getInt("oli_bin_code");
//        String oliBinQuality = rs.getString("oli_bin_quality");
//        String oliBinGroup = rs.getString("oli_bin_group");
//        String oliBinDesc = rs.getString("oli_bin_desc");
//        boolean olibinRef = rs.getString("oli_bin_ref").equals("Y") ? true
//            : false;

        BeanHbinImportMapping bean = new BeanHbinImportMapping(fromBinCode);
//      BeanHbinImportMapping bean = new BeanHbinImportMapping(1, 'c', true);
        return bean;
    }
}

The sql is tested working.

Exception stack trace:

javax.servlet.ServletException: org.springframework.jdbc.InvalidResultSetAccessException: PreparedStatementCallback; invalid ResultSet access for SQL [SELECT from_bin_code FROM hbin_import_mapping]; nested exception is java.sql.SQLException: Invalid column index
    org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


root cause 

org.springframework.jdbc.InvalidResultSetAccessException: PreparedStatementCallback; invalid ResultSet access for SQL [SELECT from_bin_code FROM hbin_import_mapping]; nested exception is java.sql.SQLException: Invalid column index
    org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:237)
    org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:200)
    com.st.mas.wmr.persistence.atom.AtomStifOliBinConversion.getLoaderMatrix(AtomStifOliBinConversion.java:52)
    com.st.mas.wmr.persistence.process.ProcessStifOliBinConversion.getLoaderMatrix(ProcessStifOliBinConversion.java:50)
    com.st.mas.wmr.action.StifBinConversionAction.viewLoaderMapping(StifBinConversionAction.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    com.st.mas.wmr.action.AnyDispatchAction.execute(AnyDispatchAction.java:107)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


root cause 

java.sql.SQLException: Invalid column index
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:124)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:161)
    oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:226)
    oracle.jdbc.driver.OraclePreparedStatement.setNullInternal(OraclePreparedStatement.java:3661)
    oracle.jdbc.driver.OraclePreparedStatement.setNull(OraclePreparedStatement.java:3652)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108)
    org.apache.commons.dbcp.DelegatingPreparedStatement.setNull(DelegatingPreparedStatement.java:108)
    org.springframework.jdbc.core.StatementCreatorUtils.setNull(StatementCreatorUtils.java:252)
    org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:213)
    org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:144)
    org.springframework.jdbc.core.ArgPreparedStatementSetter.doSetValue(ArgPreparedStatementSetter.java:65)
    org.springframework.jdbc.core.ArgPreparedStatementSetter.setValues(ArgPreparedStatementSetter.java:46)
    org.springframework.jdbc.core.JdbcTemplate$1.doInPreparedStatement(JdbcTemplate.java:641)
    org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:586)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:636)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:665)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:673)
    org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713)
    org.springframework.jdbc.core.simple.SimpleJdbcTemplate.query(SimpleJdbcTemplate.java:200)
    com.st.mas.wmr.persistence.atom.AtomStifOliBinConversion.getLoaderMatrix(AtomStifOliBinConversion.java:52)
    com.st.mas.wmr.persistence.process.ProcessStifOliBinConversion.getLoaderMatrix(ProcessStifOliBinConversion.java:50)
    com.st.mas.wmr.action.StifBinConversionAction.viewLoaderMapping(StifBinConversionAction.java:35)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:585)
    org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
    com.st.mas.wmr.action.AnyDispatchAction.execute(AnyDispatchAction.java:107)
    org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
    org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
    org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
    org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2024-11-10 23:32:21

您正在传递参数(在本例中为 null),并且查询没有任何查询参数...向查询添加参数或从方法调用中删除参数。

http://forum.springsource.org/showthread.php?t=108788

谢谢马丁·德努姆

You are passing parameters (null in this case) and the query doesn't have any query parameters... Either add a parameter to the query or remove the parameter from the method call.

http://forum.springsource.org/showthread.php?t=108788

Thanks Martin Deinum

不…忘初心 2024-11-10 23:32:21

** RowMapper**

  • 使用此接口的优点是将每一行映射到一个结果对象。 mapRow() 该方法不应在 ResultSet 上调用 next();它只应该映射当前行的值。并返回对象列表。*

applicationContext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/yash" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
<bean id="empdao" class="gmail.daos.EmpDao">
        <property name="jt" ref="jt"/>
</bean>
  • DAO 类 *

    公共类 EmpDao {
        私有 JdbcTemplate jt;
    公共列表列出员工(){
     字符串sql =“选择* FROM emp”;
      列表 emplist = jt.query(sql, new RowMapper() {
    
                    公共 EmpDto mapRow(ResultSet rs, int rowNum) 抛出 SQLException {
                        EmpDto emp = new EmpDto();
    
                        emp.setId(rs.getInt("id"));
                        emp.setName(rs.getString("name"));
                        emp.setAge(rs.getInt("年龄"));
                        emp.setSalary(rs.getInt("工资"));
                        emp.setAddress(rs.getString("地址"));
    
                        System.out.println("***");
    
                        返回雇员;
                    }
    
                });      
                返回雇佣列表;
    }}
    

如果您使用这种方式,它们将不会出现 ReselutSetExecption。

** RowMapper**

  • The advantage of using this Interface is mapping each row to a result object. mapRow() This method should not call next() on the ResultSet; it is only supposed to map values of the current row. and return list of Objects.*

applicationConttext.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/yash" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>

    <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
<bean id="empdao" class="gmail.daos.EmpDao">
        <property name="jt" ref="jt"/>
</bean>
  • DAO Class *

    public class EmpDao {
        private JdbcTemplate jt;
    public List<EmpDto> listEmpoyees(){
     String sql = "select * FROM emp";
      List<EmpDto> emplist = jt.query(sql, new RowMapper() {
    
                    public EmpDto mapRow(ResultSet rs, int rowNum) throws SQLException {
                        EmpDto emp = new EmpDto();
    
                        emp.setId(rs.getInt("id"));
                        emp.setName(rs.getString("name"));
                        emp.setAge(rs.getInt("age"));
                        emp.setSalary(rs.getInt("salary"));
                        emp.setAddress(rs.getString("address"));
    
                        System.out.println("***");
    
                        return emp;
                    }
    
                });      
                return emplist;
    }}
    

If You use this way their will be no ReselutSetExecption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文