我有一个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.
如您所见,在我打印列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$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)
我不理解什么?
答案 0 :(得分:2)
这是ResultSet.getString方法的Javadoc定义
的getString
String getString(String columnLabel)抛出SQLException
以Java编程语言中String的形式检索此ResultSet对象的当前行中指定列的值。
<强>参数:强>
columnLabel - 使用 SQL AS子句指定的列的标签。 如果未指定SQL AS子句,则标签是列的名称
<强>返回:强> 列值;如果值为SQL NULL,则返回的值为null
<强>抛出:强>
SQLException - 如果columnLabel无效;如果发生数据库访问错误或在关闭的结果集上调用此方法
因此,您可能需要查看SQL请求并确保不使用SQL AS子句,否则使用提供的标签而不是列名
答案 1 :(得分:0)
如果您的company_code属性不是字符串,请尝试使用其他方法获取列值。
如果company_code是整数类型,请尝试:
rs.getInt(column_name)
或者你可以使用getObject方法
rs.getObject(column_name)