[字段类型:blob,clob,nclob 说明:三种大型对象(LOB),用来保存较大的图形文件或带格式的文本文件,如Miceosoft Word文档,以及音频、视频等非文本文件� ...]
1.oracle driver的安装
因为操作oracle数据库,所以需要用到jdbc的驱动库,如果安装了oracle可以在oracle安装目下找到classes12.jar,将其拷贝到jdk的安装目录下(对于jdk与tomcat的安装可以在网上查查),以本人的实际安装目录为例:
(1)oracle版本9i:D:/oracle9/ora92/jdbc/lib 在此目录下可以找到classes12.jar
(2)jdk目录:C:/j2sdk1.4.1_04 将(1)中的classes12.jar拷到 C:/j2sdk1.4.1_04/jre/lib/ext目录下面
2.建立数据表
create table FILEINFO
(
FILEID INTEGER PRIMARY KEY,
FILENAME VARCHAR2(100),
FILECONTENT CLOB
)
插入一条测试数据
insert into fileinfo values(1,'测试表',empty_clob());
3.具体实现我用eclipse为开发工具
(1)插入clob数据
import java.sql.*;
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.io.*;
public class putclob {[package com.cicc.dataloader;/** * @author fenglei * */import java.io.BufferedReader;import java.io.CharArrayReader;import java.io.FileInputStream;import java.i
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
final String Webserver="192.168.36.32";//WEB服务器IP地址
final String SID="ora92";//WEB数据库服务器SID
final String UserID="****";//数据库用户名
final String Password="****";//数据库密码
final String sDBDriver = "oracle.jdbc.driver.OracleDriver";
String sConnStr = "jdbc:oracle:thin:@"+Webserver+":1521:"+SID;
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr, UserID, Password);
conn.setAutoCommit (false);
String sql = "select * from fileinfo where fileid=1 for update";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
System.out.println("开始操作clob存入:");
while(rs.next())
{
String str = "开始向表clob字段中存入数据";
putclob g = new putclob();
CLOB clob = (oracle.sql.CLOB) rs.getClob("filecontent");
Writer outstream = clob.getCharacterOutputStream();
outstream.write(str, 0, str.length());
outstream.close();
stmt.execute("commit");
System.out.println("完毕:");
}
conn.setAutoCommit(true);
}catch(Exception e){System.out.println(e.getMessage());}
}
}
(2)读取clob数据
import oracle.jdbc.driver.*;
import oracle.sql.*;
import java.sql.*;
import java.io.*;
public class getclob {
/**
* @param args
*/
public String getClob(ResultSet rs,String colname) throws Exception
{
try{
CLOB clob = (oracle.sql.CLOB) rs.getClob(colname);
Reader instream = clob.getCharacterStream();
char[] buffer = new char[1000];
int length = 0;
String Str="";
while ((length = instream.read(buffer))!=-1)
{
Str=Str + new String(buffer,0,length);
}
instream.close();
return Str;
}catch(Exception e){System.out.println("opendb.getClob():" + e.getMessage());return "";}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
final String Webserver="192.168.36.32";//WEB服务器IP地址
final String SID="ora92";//WEB数据库服务器SID
final String UserID="techline";//数据库用户名
final String Password="techline";//数据库密码
final String sDBDriver = "oracle.jdbc.driver.OracleDriver";
String sConnStr = "jdbc:oracle:thin:@"+Webserver+":1521:"+SID;
Connection conn = null;
Statement stmt=null;
ResultSet rs = null;
try
{
Class.forName(sDBDriver);
conn = DriverManager.getConnection(sConnStr, UserID, Password);
String sql = "select typeid,fileid,filename,filecontent from fileinfo";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next())
{
System.out.println(rs.getString("filename"));
getclob g = new getclob();
System.out.println("测试clob字段值:"+g.getClob(rs,"filecontent"));
}
}catch(Exception e){System.out.println(e.getMessage());}
}
}
总结:在调试过程中出现了"无效的列类型: getCLOB not implemented for class oracle.jdbc.driver.T4CNumberAccessor "错误,经过查证是由于oracle的驱动问题,最开始我用的是10g的classes12.jar,后来用了9i的,此错误消失了.
在网上看到有介绍,在oracle10g中对于clob的操作更方便了(具体未查)
无论是delphi还是java操作clob字段的insert与select都是利用流来操作,这点都一样的(其它工具也如些).[表结构: SQL> desc liuxiaowen.hadoop_job_sql; Name Type Nullable Default Comments --------- ------------- -------- ------- -------- DATA