Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java ResultSet Column '<column_name>' not found

I have a ResultSet I am pulling from a database that contains the column in question. I know this because when I loop through the records, I also loop through the columns:

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());            
}

In doing so, I am printing into the console the column names to verify existence:

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.

As you can see, right after I print the column company_code the exception is encountered!

The data is from MySQL and the field definition is:

`company_code` varchar(45) DEFAULT NULL,

Stack Trace:

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)

What am I not understanding?

like image 219
Roy Hinkley Avatar asked Oct 20 '22 15:10

Roy Hinkley


1 Answers

Here is the Javadoc definition for ResultSet.getString method

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.

Parameters:

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

Returns: the column value; if the value is SQL NULL, the value returned is null

Throws:

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

So you may need to see on your SQL request and be sure that you did not use a SQL AS clause otherwise you use the supplied label instead of the column name

like image 135
alainlompo Avatar answered Nov 15 '22 03:11

alainlompo