`

weblogic访问ORACLE,thin连接方式存取BLOB和CLOB字段(原创)

阅读更多
weblogic服务器中,在通过datasourse获取connection,BLOB字段取出来的就不是oracle.sql.BLOB类型,而是weblogic封装过的OracleThinBlob类型,执行BLOB oBLOB = (BLOB) rs.getBlob(1);所以cast的时候肯定会出错,出现ClassCaseException异常。
 
一,在使用JDBC直接连接的时候(代码引用dev2dev.bea.com.cn)
java 代码
  1. java.sql.Blob myBlob= null;   
  2.   
  3. java.sql.Clob myClob= null;   
  4.   
  5. Connection conn = null;   
  6.   
  7. Properties props = new Properties();   
  8.   
  9. props.put("user",user);   
  10.   
  11. props.put("password", password);   
  12.   
  13. props.put("server",server);   
  14.   
  15. Driver myDriver = (Driver)   
  16.   
  17. Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();   
  18.   
  19. conn = myDriver.connect("jdbc:oracle:thin:" , props);   
  20.   
  21. Statement crstmt = conn.createStatement();   
  22.   
  23. crstmt.execute("create table lobtest (id int, blobcol Blob, clobcol Clob)");//建表   
  24.   
  25. stmt.execute("insert into lobtest values (44,EMPTY_BLOB(),EMPTY_CLOB())");//插入数据   
  26.   
  27. stmt.execute("select * from lobtest where id=44");//取数据   
  28.   
  29. ResultSet rs = stmt.getResultSet();   
  30.   
  31. while ( rs.next() ) {   
  32.   
  33. myBlob = rs.getBlob("blobcol");   
  34.   
  35. }   
  36.   
  37.     
  38.   
  39. // Create a byte array and store some data in it   
  40.   
  41. System.out.println("\nCreating the following byte array:");   
  42.   
  43. int STREAM_SIZE = 10;   
  44.   
  45. byte[] b = new byte[STREAM_SIZE];   
  46.   
  47. for (int i=0; i < STREAM_SIZE; i++) {   
  48.   
  49. b[i] = (byte)(40 + (i%20)); // range 40-60   
  50.   
  51. System.out.println("byte[" + i + "] = " + b[i]);   
  52.   
  53. }   
  54.   
  55.     
  56.   
  57. // Write the byte array to a stream and store it in the Blob column   
  58.   
  59. System.out.println   
  60.   
  61. ("\nWriting the byte array to a stream" +   
  62.   
  63. " and storing it in the table as a blob...");   
  64.   
  65. InputStream is = new ByteArrayInputStream(b);   
  66.   
  67. OutputStream os = ((oracle.sql.BLOB) myBlob).getBinaryOutputStream();   
  68.   
  69. byte[] inBytes = new byte[STREAM_SIZE];   
  70.   
  71. int numBytes = is.read(inBytes);   
  72.   
  73.     
  74.   
  75. // write the input stream to the output stream   
  76.   
  77. while (numBytes > 0) {   
  78.   
  79. os.write(inBytes, 0, numBytes);   
  80.   
  81. numBytes = is.read(inBytes);   
  82.   
  83. }   
  84.   
  85. // The flush() method causes the data to be written to the table   
  86.   
  87. os.flush();   
  88.   
  89.     
  90.   
  91. //read back the blob   
  92.   
  93. System.out.println("\nReading the blob back from the table and displaying:");   
  94.   
  95. Statement readblob = conn.createStatement();   
  96.   
  97. readblob.execute("select * from lobtest where id=44");   
  98.   
  99. ResultSet rsreadblob = readblob.getResultSet();   
  100.   
  101.     
  102.   
  103. // read the blob into a byte array and display   
  104.   
  105. byte[] r = new byte[STREAM_SIZE];   
  106.   
  107. while ( rsreadblob.next() ) {   
  108.   
  109. Blob myReadBlob =rsreadblob.getBlob("blobcol");   
  110.   
  111. java.io.InputStream readis = myReadBlob.getBinaryStream();   
  112.   
  113. for (int i=0 ; i < STREAM_SIZE ; i++) {   
  114.   
  115. r[i] = (byte) readis.read();   
  116.   
  117. System.out.println("output [" + i + "] = " + r[i]);   
  118.   
  119. }   
  120.   
  121. }   
  122.   
  123.     
  124.   
  125.     
  126.   
  127. // create some character data to work with   
  128.   
  129. String ss = "abcdefghijklmnopqrstuvwxyz";   
  130.   
  131. System.out.println("\nCreated the following string to be stored as a clob:\n" +   
  132.   
  133. ss);   
  134.   
  135. // ============== Manipulating the Clob column ======================   
  136.   
  137. // get a reference to the clob column   
  138.   
  139. stmt.execute("select * from lobtest where id=44");   
  140.   
  141. ResultSet crs = stmt.getResultSet();   
  142.   
  143. while ( crs.next() ) {   
  144.   
  145. myClob = crs.getClob("clobcol");   
  146.   
  147. java.io.OutputStream osss =   
  148.   
  149. ((oracle.sql.CLOB) myClob).getAsciiOutputStream();   
  150.   
  151. byte[] bss = ss.getBytes("ASCII");   
  152.   
  153. osss.write(bss);   
  154.   
  155. osss.flush();   
  156.   
  157. }   
  158.   
  159. conn.commit();   
  160.   
  161. // read back the clob   
  162.   
  163. System.out.println("\nReading the clob back from the table and displaying:");   
  164.   
  165. Statement readclob = conn.createStatement();   
  166.   
  167. readclob.execute("select * from lobtest where id=44");   
  168.   
  169. ResultSet rsreadclob = readclob.getResultSet();   
  170.   
  171. // read the clob in as and ASCII stream, write to a character array, and display   
  172.   
  173. while ( rsreadclob.next() ) {   
  174.   
  175. Clob myReadClob =rsreadclob.getClob("clobcol");   
  176.   
  177. java.io.InputStream readClobis = myReadClob.getAsciiStream();   
  178.   
  179. char[] c = new char[26];   
  180.   
  181. for (int i=0 ; i < 26; i++) {   
  182.   
  183. c[i] = (char) readClobis.read();   
  184.   
  185. System.out.println("output [" + i + "] = " + c[i]);   
  186.   
  187. }   
  188.   
  189. }   
  190.   
  191. // Drop the table and clean up connections   
  192.   
  193. System.out.println("\nDropping table...");   
  194.   
  195. Statement dropstmt = conn.createStatement();   
  196.   
  197. dropstmt.execute("drop table lobtest");   
  198.   
  199. System.out.println("Table dropped.");   
  200.   
  201. catch (Exception e) {   
  202.   
  203. System.out.println("Exception was thrown: " + e.getMessage());   
  204.   
  205. throw e;   
  206.   
  207. finally {   
  208.   
  209. try {   
  210.   
  211. if (conn != null)   
  212.   
  213. conn.close();   
  214.   
  215. catch (SQLException sqle) {   
  216.   
  217. System.out.println("SQLException was thrown: " + sqle.getMessage());   
  218.   
  219. throw sqle;   
  220.   
  221. }   
  222.   
  223. }   
  224.   
  225. }   
  226.   

二,使用数据源的情况,并且使用oracle thin驱动方式

为了使代码适应各种应用服务器,可以对代码这样修改

  1. 在weblogic中找到这个jar包加入到工程项目中。weblogic.jar
  2. 在处理BLOB字段的类中添加引用import weblogic.jdbc.vendor.oracle.OracleThinBlob;
  3. 取得BLOB字段的时候原来的处理方式是BLOB oBLOB = (BLOB) rs.getBlob(j + 1);这里的BLOB是 oracle.sql.BLOB。在weblogic服务器下这样使用就会报ClassCaseException,因为在强制类型转换的时候,rs.getBlob(j + 1)得到的是OracleThinBlob类型。所以代码更改为

     

java 代码
  1. if(rs.getBlob(j + 1).getClass().equals(oracle.sql.BLOB.class))     {//except weblogic   
  2.             BLOB oBLOB = (BLOB) rs.getBlob(j + 1);   
  3.   
  4. }else{//for weblogic   
  5.             OracleThinBlob oBLOB = (OracleThinBlob) rs.getBlob(j + 1);   

附录:引用自javaeye JAVA完全控制Oracle中BLOB CLOB说明

----------厚厚发表于 2006年06月27日

网络上很多关于JAVA对Oracle中BLOB、CLOB类型字段的操作说明,有的不够全面,有的不够准确,甚至有的简直就是胡说八道。最近的项目正巧用到了这方面的知识,在这里做个总结。
环境:
Database: Oracle 9i
App Server: BEA Weblogic 8.14
表结构:
CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), BLOBATTR Blob)
CREATE TABLE TESTBLOB (ID Int, NAME Varchar2(20), CLOBATTR Clob)
JAVA可以通过JDBC,也可以通过JNDI访问并操作数据库,这两种方式的具体操作存在着一些差异,由于通过App Server的数据库连接池JNDI获得的数据库连接提供的java.sql.Blob和java.sql.Clob实现类与JDBC方式提供的不同,因此在入库操作的时候需要分别对待;出库操作没有这种差异,因此不用单独对待。

一、BLOB操作
1、入库
(1)JDBC方式
//通过JDBC获得数据库连接
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();
//插入一个空对象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Blob对象后强制转换为oracle.sql.BLOB
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是传入的byte数组,定义:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
(2)JNDI方式
//通过JNDI获得数据库连接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_blob()
st.executeUpdate("insert into TESTBLOB (ID, NAME, BLOBATTR) values (1, "thename", empty_blob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select BLOBATTR from TESTBLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Blob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinBlob(不同的App Server对应的可能会不同)
weblogic.jdbc.vendor.oracle.OracleThinBlob blob = (weblogic.jdbc.vendor.oracle.OracleThinBlob) rs.getBlob("BLOBATTR");
OutputStream outStream = blob.getBinaryOutputStream();
//data是传入的byte数组,定义:byte[] data
outStream.write(data, 0, data.length);
}
outStream.flush();
outStream.close();
con.commit();
con.close();
2、出库
//获得数据库连接
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是读出并需要返回的数据,类型是byte[]
data = new byte[input.available()];
inStream.read(data);
inStream.close();
}
inStream.close();
con.commit();
con.close();
二、CLOB操作
1、入库
(1)JDBC方式
//通过JDBC获得数据库连接
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();
//插入一个空对象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Clob对象后强制转换为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获得数据库连接
Context context = new InitialContext();
ds = (DataSource) context.lookup("ORA_JNDI");
Connection con = ds.getConnection();
con.setAutoCommit(false);
Statement st = con.createStatement();
//插入一个空对象empty_clob()
st.executeUpdate("insert into TESTCLOB (ID, NAME, CLOBATTR) values (1, "thename", empty_clob())");
//锁定数据行进行更新,注意“for update”语句
ResultSet rs = st.executeQuery("select CLOBATTR from TESTCLOB where ID=1 for update");
if (rs.next())
{
//得到java.sql.Clob对象后强制转换为weblogic.jdbc.vendor.oracle.OracleThinClob(不同的App Server对应的可能会不同)
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、出库
//获得数据库连接
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是读出并需要返回的数据,类型是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几种类型的区别
2、java.sql.Clob、oracle.sql.CLOB、weblogic.jdbc.vendor.oracle.OracleThinClob几种类型的区别

0
0
分享到:
评论
2 楼 metallicats 2015-06-23  
非常感谢博主!
1 楼 fshoy 2011-08-16  


太感谢了!  也希望这编好东西有更多人看到!!! 

相关推荐

Global site tag (gtag.js) - Google Analytics