Blob、Clob、String的对象相关操作

大型对象
BLOB就是使用二进制保存数据。
如:保存位图。
CLOB使用CHAR来保存数据。
如:保存XML文档。

[b]1.读取Blob、Clob对象[/b]

Connection con = null;
try {
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "username","password");
con.setAutoCommit(false);
Statement st = con.createStatement();
PreparedStatement ps = con.prepareStatement("insert into documents(text,photo) values(?,?)");
//向数据库写入Clob字段
File file1 = new File("d:\1.txt");
int filel_length = (int)file1.length();
InputStream f1 = new FileInputStream(file1);
ps.setAsciiStream(1, f1, filel_length);
//向数据库写入Blob字段
File file2 = new File("d:\1.jpg");
int file2_length = (int)file2.length();
InputStream f2 = new FileInputStream(file2);
ps.setBinaryStream(2, f2, file2_length);
ps.execute();
con.commit();

ResultSet rs1 = ps.executeQuery("select text,photo from documents where id='365'");
while(rs1.next()){
// java.sql.Clob clob =rs1.getClob(1); //和提取一般对象一样
InputStream is = rs1.getAsciiStream(1); //得到Clob的流
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = null;
while(null != (line=br.readLine())){
System.out.println(line);
}
is.close();

java.sql.Blob blob = rs1.getBlob(2);
System.out.println(blob.length());
InputStream bis = blob.getBinaryStream(); //得到Blob实例的字节流
OutputStream os = new FileOutputStream("d:\11.jpg");
int i = bis.read();
while(i != -1){
os.write(i);
i = bis.read();
}
os.flush();
os.close();
bis.close();
}
} catch (Exception e) {
e.printStackTrace();
}


[b]2.将blob 转换成 String[/b]

// 将blob 转换成 String
public static String BlobToString(){
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String str = "";
try {
//...获取Connection
st = conn.createStatement();
rs = st.executeQuery("select big_bit from blob_test");
while (rs.next())
Blob blob = rs.getBlob(1);
InputStream in = blob.getBinaryStream();
//一般接下来是把in的字节流写入一个文件中,但这里直接放进字符串
byte[] buff=new byte[(int) blob.length()];
// byte[] buff=new byte[1024];
// byte[] b = new byte[blob.getBufferSize()];
for(int i=0;(i=in.read(buff))>0;){
str=str+new String(buff);
}
in.close();
// blob.close(); //是否需要关闭?
}catch (Exception e) {
e.printStackTrace();
}
return str;
}


[b]3.将clob 转换成 String[/b]

// 将clob 转换成 String
public static String ClobToString(CLOB clob) throws SQLException, IOException {
String reString = "";
Reader is = clob.getCharacterStream();// 得到流
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
StringBuffer sb = new StringBuffer();
while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
}


[b]4.将String类型转换成Blob类型[/b]

String str = "EHOMEvsLGD";
oracle.sql.BLOB b = (oracle.sql.BLOB)(str.getBytes());


[b]5.将String类型转换成Clob类型[/b]

String str = "DKvsWEvsCCM";
java.sql.Clob c = new javax.sql.rowset.serial.SerialClob(str.toCharArray());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值