----------厚厚發(fā)表于 2006年06月27日
網(wǎng)絡(luò)上很多關(guān)于JAVA對(duì)Oracle中BLOB、CLOB類型字段的操作說明,有的不夠全面,有的不夠準(zhǔn)確,甚至有的簡(jiǎn)直就是胡說八道。最近的項(xiàng)目正巧用到了這方面的知識(shí),在這里做個(gè)總結(jié)。
環(huán)境:
Database: Oracle 9i
App Server: BEA Weblogic 8.14
表結(jié)構(gòu):
CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), BLOBATTR Blob)
CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), CLOBATTR Clob)
JAVA可以通過JDBC,也可以通過JNDI訪問并操作數(shù)據(jù)庫,這兩種方式的具體操作存在著一些差異,由于通過App Server的數(shù)據(jù)庫連接池JNDI獲得的數(shù)據(jù)庫連接提供的java.sql.Blob和java.sql.Clob實(shí)現(xiàn)類與JDBC方式提供的不同,因此在入庫操作的時(shí)候需要分別對(duì)待;出庫操作沒有這種差異,因此不用單獨(dú)對(duì)待。
一、BLOB操作
1、入庫
(1)JDBC方式
//通過JDBC獲得數(shù)據(jù)庫連接
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是傳入的byte數(shù)組,定義:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
(2)JNDI方式
//通過JNDI獲得數(shù)據(jù)庫連接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Blob對(duì)象后強(qiáng)制轉(zhuǎn)換為weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server對(duì)應(yīng)的可能會(huì)不同)
weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是傳入的byte數(shù)組,定義:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
2、出庫
//獲得數(shù)據(jù)庫連接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1");
if (rs.next())
{
java.sql.Blob blob = rs.getBlob("BLOBATTR");
InputStream inStream = blob.getBinaryStream();
//data是讀出并需要返回的數(shù)據(jù),類型是byte[]
data = new byte[input.available()];
inStream.read(data);
inStream.close();
}
inStream.close();
con.commit();
con.close();
二、CLOB操作
1、入庫
(1)JDBC方式
//通過JDBC獲得數(shù)據(jù)庫連接
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testdb", "test", "test");
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為oracle.sql.CLOB
oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob("CLOBATTR");
Writer outStream = clob.getCharacterOutputStream();
//data是傳入的字符串,定義:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
(2)JNDI方式
//通過JNDI獲得數(shù)據(jù)庫連接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一個(gè)空對(duì)象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//鎖定數(shù)據(jù)行進(jìn)行更新,注意“for update”語句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到j(luò)ava.sql.Clob對(duì)象后強(qiáng)制轉(zhuǎn)換為weblogic.jdbc.vendor.oracle.OracleThinClob(不同的App Server對(duì)應(yīng)的可能會(huì)不同)
weblogic.jdbc.vendor.oracle.OracleThinClob clob = (weblogic.jdbc.vendor.oracle.OracleThinClob) rs.getClob("CLOBATTR");
Writer outStream = clob.getCharacterOutputStream();
//data是傳入的字符串,定義:String data
char[] c = data.toCharArray();
outStream.write(c, 0, c.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
2、出庫
//獲得數(shù)據(jù)庫連接
Connection con = ConnectionFactory.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//不需要“for update”
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1");
if (rs.next())
{
java.sql.Clob clob = rs.getClob("CLOBATTR");
Reader inStream = clob.getCharacterStream();
char[] c = new char[(int) clob.length()];
inStream.read(c);
//data是讀出并需要返回的數(shù)據(jù),類型是String
data = new String(c);
inStream.close();
}
inStream.close();
con.commit();
con.close();
需要注意的地方:
1、java.sql.Blob、oracle.sql.BLOB、weblogic.jdbc.vendor.oracle.OracleThinBlob幾種類型的區(qū)別
2、java.sql.Clob、oracle.sql.CLOB、weblogic.jdbc.vendor.oracle.OracleThinClob幾種類型的區(qū)別