Java經過ODBC連接數據庫並遍歷結果的一個問題

上一篇文章談到怎麼鏈接Oracle數據庫,其實經過ODBC也差很少,只是driver要換成JdbcOdbcDriver。配置文件以下:html

driver=sun.jdbc.odbc.JdbcOdbcDriver
url=jdbc:odbc:[Your DSN]
username=[Your user name]
password=[Your password]

 

須要注意的是,在sql語句裏,避免出現重複的列別名,例如:java

select 
o.TIMESTAMP ReceivedTimestamp,
o.CL_ORDER_ID OrderId,
o.TIMESTAMP ReceivedTimestamp,......

若是sql裏面有重複的列別名,那麼遍歷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的JavaDocapi

 

本文完。oracle

相關文章
相關標籤/搜索