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

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