Java 未找到结果集列“<column_name>”
Java ResultSet Column '<column_name>' not found
我有一个从包含相关列的数据库中提取的结果集。我知道这一点是因为当我遍历记录时,我也会遍历列:
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.
如您所见,在我打印列 company_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.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)
我哪里不明白?
如果您的 company_code 属性不是字符串,请尝试使用不同的方法获取列值。
如果 company_code 是整数类型,试试这个:
rs.getInt(column_name)
或者你可以使用 getObject 方法
rs.getObject(column_name)
这里是 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
Returns:
列值;如果值为 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 子句,否则您将使用提供的标签而不是列名
我有一个从包含相关列的数据库中提取的结果集。我知道这一点是因为当我遍历记录时,我也会遍历列:
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.
如您所见,在我打印列 company_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.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)
我哪里不明白?
如果您的 company_code 属性不是字符串,请尝试使用不同的方法获取列值。
如果 company_code 是整数类型,试试这个:
rs.getInt(column_name)
或者你可以使用 getObject 方法
rs.getObject(column_name)
这里是 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
Returns: 列值;如果值为 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 子句,否则您将使用提供的标签而不是列名