Oracle long raw 類(lèi)型字段讀取問(wèn)題
【問(wèn)題描述】
項(xiàng)目中用到了long raw 類(lèi)型字段用于存放配置文件內(nèi)容,一直相安無(wú)事。突然有一天需要修改設(shè)計(jì),增加了一個(gè)字段"group_name",問(wèn)題來(lái)了,讀取long raw字段總是提是“流已關(guān)閉”,經(jīng)過(guò)一番較量,才算解決,在此和大家分享一下。
系統(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í)行出錯(cuò)的情況】
修改后的表結(jié)構(gòu)(增加了一個(gè)字段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)閉")
分析修改前后,并無(wú)不妥之處,只是增加了一個(gè)字段而已。
【解決辦法】
修改sql語(yǔ)句-〉executeQuery("select id,group_name,name,data,project_id from bt_defination");
執(zhí)行一切正常!
【錯(cuò)誤分析】
原來(lái)是select列表和res的get順序不一致(這種問(wèn)題在不含二進(jìn)制字段的查詢(xún)中不存在)。
得出結(jié)論——對(duì)于包含二進(jìn)制字段的查詢(xún)操作,需要嚴(yán)格按照select列表的順序讀取,對(duì)于select * from ...的情況,默認(rèn)順序時(shí)間表語(yǔ)句的字段順序。
或者更嚴(yán)格的說(shuō),二進(jìn)制字段的數(shù)據(jù)可以提前讀取,但是絕對(duì)不能延后。舉個(gè)例子:
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號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

