- 浏览: 742529 次
- 性别:
- 来自: 长春
文章分类
最新评论
-
qq591628420:
正解!!!!!!!!!!!!!!!!!!!!!!!!!! ...
ibatis配置log4j输出sql语句等日志信息 -
joy1982109:
我在was下发现一个同名的xml文件,是不是就是这个呢。不过没 ...
让webshere(was)发布程序编译jsp为java文件,java文件位置 -
xiaoqing2305:
...
jbuilder 2006中添加不支持的文件类型,不支持的不显示问题 -
metallicats:
非常感谢博主!
weblogic访问ORACLE,thin连接方式存取BLOB和CLOB字段(原创) -
lailaiping:
谢谢作者了
让webshere(was)发布程序编译jsp为java文件,java文件位置
- java.sql.Blob myBlob= null;
- java.sql.Clob myClob= null;
- Connection conn = null;
- Properties props = new Properties();
- props.put("user",user);
- props.put("password", password);
- props.put("server",server);
- Driver myDriver = (Driver)
- Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
- conn = myDriver.connect("jdbc:oracle:thin:" , props);
- Statement crstmt = conn.createStatement();
- crstmt.execute("create table lobtest (id int, blobcol Blob, clobcol Clob)");//建表
- stmt.execute("insert into lobtest values (44,EMPTY_BLOB(),EMPTY_CLOB())");//插入数据
- stmt.execute("select * from lobtest where id=44");//取数据
- ResultSet rs = stmt.getResultSet();
- while ( rs.next() ) {
- myBlob = rs.getBlob("blobcol");
- }
- // Create a byte array and store some data in it
- System.out.println("\nCreating the following byte array:");
- int STREAM_SIZE = 10;
- byte[] b = new byte[STREAM_SIZE];
- for (int i=0; i < STREAM_SIZE; i++) {
- b[i] = (byte)(40 + (i%20)); // range 40-60
- System.out.println("byte[" + i + "] = " + b[i]);
- }
- // Write the byte array to a stream and store it in the Blob column
- System.out.println
- ("\nWriting the byte array to a stream" +
- " and storing it in the table as a blob...");
- InputStream is = new ByteArrayInputStream(b);
- OutputStream os = ((oracle.sql.BLOB) myBlob).getBinaryOutputStream();
- byte[] inBytes = new byte[STREAM_SIZE];
- int numBytes = is.read(inBytes);
- // write the input stream to the output stream
- while (numBytes > 0) {
- os.write(inBytes, 0, numBytes);
- numBytes = is.read(inBytes);
- }
- // The flush() method causes the data to be written to the table
- os.flush();
- //read back the blob
- System.out.println("\nReading the blob back from the table and displaying:");
- Statement readblob = conn.createStatement();
- readblob.execute("select * from lobtest where id=44");
- ResultSet rsreadblob = readblob.getResultSet();
- // read the blob into a byte array and display
- byte[] r = new byte[STREAM_SIZE];
- while ( rsreadblob.next() ) {
- Blob myReadBlob =rsreadblob.getBlob("blobcol");
- java.io.InputStream readis = myReadBlob.getBinaryStream();
- for (int i=0 ; i < STREAM_SIZE ; i++) {
- r[i] = (byte) readis.read();
- System.out.println("output [" + i + "] = " + r[i]);
- }
- }
- // create some character data to work with
- String ss = "abcdefghijklmnopqrstuvwxyz";
- System.out.println("\nCreated the following string to be stored as a clob:\n" +
- ss);
- // ============== Manipulating the Clob column ======================
- // get a reference to the clob column
- stmt.execute("select * from lobtest where id=44");
- ResultSet crs = stmt.getResultSet();
- while ( crs.next() ) {
- myClob = crs.getClob("clobcol");
- java.io.OutputStream osss =
- ((oracle.sql.CLOB) myClob).getAsciiOutputStream();
- byte[] bss = ss.getBytes("ASCII");
- osss.write(bss);
- osss.flush();
- }
- conn.commit();
- // read back the clob
- System.out.println("\nReading the clob back from the table and displaying:");
- Statement readclob = conn.createStatement();
- readclob.execute("select * from lobtest where id=44");
- ResultSet rsreadclob = readclob.getResultSet();
- // read the clob in as and ASCII stream, write to a character array, and display
- while ( rsreadclob.next() ) {
- Clob myReadClob =rsreadclob.getClob("clobcol");
- java.io.InputStream readClobis = myReadClob.getAsciiStream();
- char[] c = new char[26];
- for (int i=0 ; i < 26; i++) {
- c[i] = (char) readClobis.read();
- System.out.println("output [" + i + "] = " + c[i]);
- }
- }
- // Drop the table and clean up connections
- System.out.println("\nDropping table...");
- Statement dropstmt = conn.createStatement();
- dropstmt.execute("drop table lobtest");
- System.out.println("Table dropped.");
- } catch (Exception e) {
- System.out.println("Exception was thrown: " + e.getMessage());
- throw e;
- } finally {
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException sqle) {
- System.out.println("SQLException was thrown: " + sqle.getMessage());
- throw sqle;
- }
- }
- }
二,使用数据源的情况,并且使用oracle thin驱动方式
为了使代码适应各种应用服务器,可以对代码这样修改
- 在weblogic中找到这个jar包加入到工程项目中。weblogic.jar
- 在处理BLOB字段的类中添加引用import weblogic.jdbc.vendor.oracle.OracleThinBlob;
- 取得BLOB字段的时候原来的处理方式是BLOB oBLOB = (BLOB) rs.getBlob(j + 1);这里的BLOB是 oracle.sql.BLOB。在weblogic服务器下这样使用就会报ClassCaseException,因为在强制类型转换的时候,rs.getBlob(j + 1)得到的是OracleThinBlob类型。所以代码更改为
- if(rs.getBlob(j + 1).getClass().equals(oracle.sql.BLOB.class)) {//except weblogic
- BLOB oBLOB = (BLOB) rs.getBlob(j + 1);
- }else{//for weblogic
- 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几种类型的区别
发表评论
-
talend open studio 连接access时,需要用32位的软件
2019-04-11 11:18 466mdb文件是32位的,建立系统odbc数据源或者用文件直连的 ... -
XML内容中不能有&字符
2017-06-25 17:27 2375&是xml中的转义字符的开头,如果xml内容中有ur ... -
天成公司产品开发部“错题本”
2017-05-12 15:57 5071、页面写死IP地址等信息 造成问题:实施人员不容易维护 ... -
windows和linux下使用命令查找端口对应的进程pid并杀死进程
2017-01-02 09:46 3498windows下命令,8080是要杀死进程的端口,这个命令 ... -
wmware12 共享目录不好使,不能安装wmwarre tools
2016-10-25 12:14 987表现是这样的,按照网上设置了共享目录后,做映射时就是看不到 ... -
java.lang.Exception: XML文档格式不正确:prolog 中不允许有内容
2016-10-09 09:30 1629在进行xml开发的过程中,调用对方webservice,接口 ... -
关于URIEncoding设置规则
2016-03-19 13:34 3673<!--[if !supportLists]--> ... -
js文件压缩成gzjs格式的方法和工具
2015-01-30 10:15 17911、解压 解压可以直接用winrar工具打开gzjs文件 ... -
tomcat的server.xml里面配置URIEncoding的问题,导致post和get访问编码不一致
2014-10-28 16:52 4570原来的发送短信的包都是好用的,post方式和get方式提交数 ... -
windows远程桌面强制登录,当用户满的时候
2014-10-27 13:16 3196windows远程桌面强制登录,如果用户满了,会提示超出最 ... -
oracle数据恢复
2013-11-29 15:51 1032这些恢复都是在数据库启用了undo表空间的情况下才好使的,并 ... -
java HashMap存值巧遇,key不同但是却覆盖了之前的值
2013-09-27 17:16 2646情况是这样的 一个空的HashMap testHM; t ... -
查看windows系统进程的详细信息,任务管理器看不到的
2013-07-12 13:01 9350任务管理器的显示东西太简单了,想看到一个进程的详细路径和启 ... -
oracle 检索数据的时候报ORA-01722: 无效数字
2013-07-11 17:18 4180sql语句如下 select * from yhb_ ... -
java高效代码优化方案
2013-04-10 23:31 2515将类尽可能的设置为Final,更深入点说,如果类是fina ... -
利用winrar给已经压缩好的压缩包加密码,和压缩前添加密码
2013-01-06 10:14 5032其实很简单,但是我用了好久的winrar才知道这个功能,一般都 ... -
nginx 操作,启动、停止、重启
2012-11-01 16:47 44948查看nginx进程看看有没有启动,并可以查看进程ID [r ... -
vnc 常用操作笔记,启动vncserver,指定端口
2012-10-16 15:50 15861linux下启动vnc [root@anhnfts n ... -
eclipse jee 版,项目图标上总有一个红色的叉子
2012-10-08 15:39 12570eclipse 使用的时候项目 ... -
websphere6,7上修改类装载顺序为应用程序优先
2012-09-29 15:28 5906看附件吧,写在word里了, 想弄到这里太费劲了,不支持 ...
相关推荐
weblogic下转换oracle 的clob类型经常报转型错误,本例子通过反射解决weblogic.jdbc.wrapper.Clob_oracle_sql_CLOB转型成oracle.sql.CLOB问题
NULL 博文链接:https://liuzidong.iteye.com/blog/825365
weblogic+Oracle数据库连接池的配置问题
weblogic与oracle数据库通过连接池连接及测试代码 另有myeclipse中weblogic服务器下使用jsf1.2的设置步骤
WebLogic连接Oracle数据库的配置流程图解
NULL 博文链接:https://shikonglaike.iteye.com/blog/543538
我在建站过程中手机的一些资料,分享给大家。Linux+WebLogic+Oracle建站参考资料
Weblogic配置Oracle数据源,掌握Weblogic配置原理
Linux服务器下jdk、weblogic 、oracle的详细安装步骤和方法Linux服务器下jdk、weblogic 、oracle的详细安装步骤和方法Linux服务器下jdk、weblogic 、oracle的详细安装步骤和方法Linux服务器下jdk、weblogic 、oracle...
BEA_Weblogic_配置Oracle连接池 轻松搞定
Weblogic+Oracle10g接続
rt,出现这种情况,可以更换下hibernate核心包 即可解决
Java+Weblogic+Oracle整和开发培训教程 使用JAVA语言 Weblogic容器 Oracle数据库 进行企业级开发的教程
很难找的weblogic支持oracle的blob的驱动包
详细描述在redhat enterprise 6.X环境下,部署weblogic10.3 和oracle11的过程,已处理相关环境下部署不成功的问题解决办法,供各位参考
BEA WebLogic Enterprise Platform和ORACLE RAC解决方案.doc
oracle RAC 和 weblogic集群安装配置和部署详细文档
在WebLogic中建立数据库连接池与数据源及利用JBuilder进行测试.pdf
Oracle 公司的Oracle WebLogic Server 的培训ppt
ubuntu下配置weblogic和oracle.pdf