找不到Java ResultSet列”

我有一个ResultSet,我从包含相关列的数据库中提取.我知道这一点,因为当我遍历记录时,我也循环遍历列:

ResultSetMetaData rsmd = rs.getMetaData();
int numColumns = rsmd.getColumnCount();

try {

    while(rs.next()) {      

        JSONObject obj = new JSONObject();                          
        for (int i=1; i<numColumns+1; i++) {                    
            String column_name = rsmd.getColumnName(i);
            System.out.println(Time.formatDate(System.currentTimeMillis(),  true)+" - [ResultSetConverter.convert] column_name ["+column_name+"]");

            ...

            // line where exception is thrown
            obj.put(column_name, rs.getString(column_name)+"");

         }

    }

} catch (Exception e) {
    System.out.println(Time.formatDate(System.currentTimeMillis(),  true)+" - [ResultSetConverter.convert] Exception: "+e.getMessage());            
}

这样做,我打印到控制台的列名称来验证是否存在:

4/1/2015 01:18 PM - [ResultSetConverter.convert] column_name [_id]
4/1/2015 01:18 PM - [ResultSetConverter.convert] column_name [image]
4/1/2015 01:18 PM - [ResultSetConverter.convert] column_name [company_code]
4/1/2015 01:18 PM - [ResultSetConverter.convert] Exception: Column 'company_code' not found.

正如您所看到的,在我打印group_code列之后会遇到异常!

数据来自MySQL,字段定义为:

`company_code` varchar(45) DEFAULT NULL,

堆栈跟踪:

java.sql.SQLException: Column 'company_code' not found.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
    at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1163)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5729)
    at com.sequon.core.ResultSetConverter.convert(ResultSetConverter.java:96)
    at com.sequon.core.Database.getDevicesForMap(Database.java:1360)
    at com.sequon.api.Data.getDevices(Data.java:751)
    at com.sequon.api.Data.getData(Data.java:354)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:414)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

我不明白的是什么?

最佳答案 这是ResultSet.getString方法的Javadoc定义

getString

String getString(String columnLabel) throws SQLException

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

参数:

columnLabel – the label for the column specified with the SQL AS clause. If the SQL AS clause was not specified, then the label is the name of the column

返回:
列值;如果值为SQL NULL,则返回的值为null

抛出:

SQLException – if the columnLabel is not valid; if a database access error occurs or this method is called on a closed result set

因此,您可能需要查看SQL请求并确保不使用SQL AS子句,否则使用提供的标签而不是列名称

点赞