Java通过ODBC链接数据库并遍历结果的一个问题
时间:2014-05-08 21:52:05
收藏:0
阅读:381
上一篇文章谈到怎么连接Oracle数据库,其实通过ODBC也差不多,只是driver要换成JdbcOdbcDriver。配置文件如下:
driver=sun.jdbc.odbc.JdbcOdbcDriver url=jdbc:odbc:[Your DSN] username=[Your user name] password=[Your password]
需要注意的是,在sql语句里,避免出现重复的列别名,例如:
select o.TIMESTAMP ReceivedTimestamp, o.CL_ORDER_ID OrderId, o.TIMESTAMP ReceivedTimestamp,......
如果sql里面有重复的列别名,那么遍历sql结果时,会出现问题:
ResultSet rs = conn.prepareStatement(properties.fda_sql()).executeQuery() while (rs.next()) { String receivedTime1 = rs.getString("ReceivedTimestamp");
String receivedTime1 = rs.getString("OrderId");
String receivedTime2 = rs.getString("ReceivedTimestamp"); //......
}
遍历开始,第一次遍历时,我们以为receivedTime1和receivedTime2是同一行的数据,事实上receivedTime2取得已经是下一行的数据,从而很可能抛出“java.sql.SQLException: Invalid Descriptor Index”。所以,尽量避免出现重复的列别名。
还需要注意的是,ResultSet里的数据,只能被读取一次。详细请看ResultSet的JavaDoc。
本文完。
评论(0)