Oracle long raw 類型字段讀取問題
【問題描述】
項目中用到了long raw 類型字段用于存放配置文件內容,一直相安無事。突然有一天需要修改設計,增加了一個字段"group_name",問題來了,讀取long raw字段總是提是“流已關閉”,經過一番較量,才算解決,在此和大家分享一下。
系統環境:windows 2003 enterprise/Oracle 9.2.1.0
原表結構:
create table bt_defination
(
id number(32) not null,
name varchar2(100) not null,
data long raw not null,
project_id number(32) not null,
primary key(id)
)
具體執行步驟如下:
getConnection()....
createStatement()....
executeQuery("select * from bt_defination");
while(res.next())
{
res.getLong("id");
res.getString("name");
res.getBytes("data");
res.getLong("project_id");
}
執行一切正常。
【下面即為修改后執行出錯的情況】
修改后的表結構(增加了一個字段group_name,擴充為主鍵):
create table bt_defination
(
id number(32) not null,
name varchar2(100) not null,
data long raw not null,
group_name varchar(20) not null,
project_id number(32) not null,
primary key(id,name)
)
執行步驟修改為:
具體執行步驟如下:
getConnection()....
createStatement()....
executeQuery("select * from bt_defination");
while(res.next())
{
res.getLong("id");
res.getString("group_name");//new added
res.getString("name");
res.getBytes("data");
res.getLong("project_id");
}
結果顯示:SQLException("流已關閉")
分析修改前后,并無不妥之處,只是增加了一個字段而已。
【解決辦法】
修改sql語句-〉executeQuery("select id,group_name,name,data,project_id from bt_defination");
執行一切正常!
【錯誤分析】
原來是select列表和res的get順序不一致(這種問題在不含二進制字段的查詢中不存在)。
得出結論——對于包含二進制字段的查詢操作,需要嚴格按照select列表的順序讀取,對于select * from ...的情況,默認順序時間表語句的字段順序。
或者更嚴格的說,二進制字段的數據可以提前讀取,但是絕對不能延后。舉個例子:
getConnection()....
createStatement()....
executeQuery("select id,group_name,name,data,project_id from bt_defination");
while(res.next())
{
res.getBytes("data");//up?
res.getLong("id");
res.getString("group_name");//new added
res.getString("name");
res.getLong("project_id");
}
data字段提前讀取也是完全可以的。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
