java blob转string中如何把 varbinary 转换成blob

{"subject":"Re: 如何用java操纵blob字段","content":"发信人: FairyBoy (善才童子), 信区: Database&&&&&&&& 标&&题: Re: 如何用java操纵blob字段
发信站: BBS 水木清华站 (Thu Feb 21 15:38:06 2002) &&&&&& private void fillBlob(Connection conn,oracle.sql.BLOB blob,byte[] btaDat
a) &&&&&&&& throws Exception &&&& { &&&&&&&& OracleCallableStatement cstm=(OracleCallableStatement)conn.prepareCa
ll(\"begin dbms_lob.write(?,?,?,?);\"); &&&&&&&& int nBlock=blob.getChunkSize(); &&&&&&&& for(int i=0;i&=btaData.length/nBi++) &&&&&&&& { &&&&&&&&&&&& byte []btaB &&&&&&&&&&&& if (i==(btaData.length/nBlock)) &&&&&&&&&&&& { &&&&&&&&&&&&&&&& btaBlock=new byte[btaData.length%nBlock]; &&&&&&&&&&&& } &&&&&&&&&&&& else &&&&&&&&&&&& { &&&&&&&&&&&&&&&& btaBlock=new byte[nBlock]; &&&&&&&&&&&& } &&&&&&&&&&&& if ((btaBlock==null) || (btaBlock.length==0)) &&&&&&&&&&&& for(int j=0;j&btaBlock.j++) &&&&&&&&&&&& { &&&&&&&&&&&&&&&& btaBlock[j]=btaData[i*nBlock+j]; &&&&&&&&&&&& } &&&&&&&&&&&& cstm.setBlob(1,blob); &&&&&&&&&&&& cstm.setInt(2,btaBlock.length); &&&&&&&&&&&& cstm.setLong(3,i*nBlock+1); &&&&&&&&&&&& cstm.setBytes(4,btaBlock); &&&&&&&&&&&& cstm.execute(); &&&&&&&& } &&&&&&&& cstm.close(); &&&& } &&&& private byte[] dumpBlob (Connection conn, oracle.sql.BLOB blob) &&&&&&&& throws Exception &&&& { &&&&&&&& OracleCallableStatement cstmt1 =(OracleCallableStatement) conn.prepa
reCall (\"begin ? := dbms_lob.getLength (?);\"); &&&&&&&& OracleCallableStatement cstmt2 =(OracleCallableStatement) conn.prepa
reCall (\"begin dbms_lob.read (?, ?, ?, ?);\"); &&&&&&&& cstmt1.registerOutParameter (1, Types.NUMERIC); &&&&&&&& cstmt1.setBlob (2, blob); &&&&&&&& cstmt1.execute (); &&&&&&&& long length = cstmt1.getLong (1); &&&&&&&& long i = 0; &&&&&&&& int chunk = blob.getChunkSize();; &&&&&&&& byte [] btaRet=new byte[(int)(length)]; &&&&&&&& while (i & length) &&&&&&&& { &&&&&&&&&&&& cstmt2.setBlob (1, blob); &&&&&&&&&&&& cstmt2.setLong (2, chunk); &&&&&&&&&&&& cstmt2.registerOutParameter (2, Types.NUMERIC); &&&&&&&&&&&& cstmt2.setLong (3, i + 1); &&&&&&&&&&&& cstmt2.registerOutParameter (4, Types.VARBINARY); &&&&&&&&&&&& cstmt2.execute (); &&&&&&&&&&&& long read_this_time = cstmt2.getLong (2); &&&&&&&&&&&& byte [] byte_this_time = cstmt2.getBytes (4); &&&&&&&&&&&& //szRetBuffer.append(string_this_time); &&&&&&&&&&&& for(int j=0;j&read_this_j++) &&&&&&&&&&&& { &&&&&&&&&&&&&&&& btaRet[(int)i+j]=byte_this_time[j]; &&&&&&&&&&&& } &&&&&&&&&&&& i += read_this_ &&&&&&&& } &&&&&&&&&&&&&&&&&& cstmt1.close (); &&&&&&&& cstmt2.close (); &&&&&&&& return btaR && }
其中使用fillBlob前,如果是新的记录,先insert一条,再select出来! && 【 在 watertown (迷惘中) 的大作中提到: 】
: 谢谢,能提供源代码吗?
他娘的王校长,
再不把地卖给我,
叫巡抚把他抓起来!
安他个汪洋大盗的罪名!
... ... &&&& ※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.106.98.78]
","ajax_st":1,"ajax_code":"0005","ajax_msg":"操作成功"}已有天涯账号?
这里是所提的问题,您需要登录才能参与回答。
"天涯问答"是天涯社区旗下的问题分享平台。在这里您可以提问,回答感兴趣的问题,分享知识和经历,无论您在何时何地上线都可以访问,此平台完全免费,而且注册非常简单。
怎样将二进制的byte数组转换blob
怎样将二进制的byte数组转换blob
09-11-25 &匿名提问 发布
String 中目前有三个转换成byte数组的方法(还有一个方法已过期),分别如下:getBytes()          使用应用平台默认的字符集进行编码getBytes(Charset charset)         使用指定的Charset进行编码getBytes(String charsetName)   使用指定的字符集进行编码
请登录后再发表评论!各位大虾,请问java如何将sqlserver中的image转换成oracle中的BLOB,谢谢!
我的想法是先将sqlserver中的image存成图片文件,放在文件系统中。然后再用java读取图片文件,最后保存到oracle中。
转—&JDBC读取写入SQLServer的Blob(image)字段数据的代码:import java.sql.*;
import java.io.*;
* JDBC读取写入SQLServer的Blob(image)字段数据的代码.
* @author 老紫竹研究室()
public class Test {
// 注意下面是jdbc2.0的驱动写法,3.0的请自行查找
static String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
static String url = "jdbc:microsoft:sqlserver://192.168.0.202:;DatabaseName=dddd";
static String user = "sa";
static String passwd = "ps";
public static void method1() throws Exception {
Connection conn =
Class.forName(driver);
conn = DriverManager.getConnection(url, user, passwd);
int op = 0;
if (op == 0) {
PreparedStatement ps = conn
.prepareStatement("insert
ps.setString(1, "aaa.exe");
InputStream in = new FileInputStream("d:/aaa.exe");
ps.setBinaryStream(2, in, in.available());
ps.executeUpdate();
ps.close();
PreparedStatement ps = conn
.prepareStatement("select
ps.setString(1, "aaa.exe");
ResultSet rs = ps.executeQuery();
rs.next();
InputStream in = rs.getBinaryStream("filecontent");
System.out.println(in.available());
FileOutputStream out = new FileOutputStream("d:/bbb.exe");
byte[] b = new byte[1024];
int len = 0;
while ((len = in.read(b)) != -1) {
out.write(b, 0, len);
out.flush();
out.close();
in.close();
rs.close();
ps.close();
} catch (Exception ex) {
ex.printStackTrace(System.out);
} finally {
conn.close();
} catch (Exception ex) {
public static void method2() {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/exercise";
// String url=
// "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK";
String user = "root";
String password = "roger";
Class.forName(driver);
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException
Connection conn = DriverManager.getConnection(url, user, password);
PreparedStatement pstmt = conn
.prepareStatement("select
picturename=?");
pstmt.setString(1, "z0011.jpg");
ResultSet res = pstmt.executeQuery();
res.beforeFirst();
while (res.next()) {
Blob blob = res.getBlob(2);
FileOutputStream fout = new FileOutputStream("z0018.jpg");
fout.write(blob.getBytes(1, (int) blob.length()));
System.out.println("z0011.jpg
+ (int) blob.length());
fout.flush();
fout.close();
} catch (IOException e) {
System.out.println("create
conn.close();
} catch (SQLException e) {
System.out.println("SQLException
再加上-通过java往oracle中blob字段写入数据:public class applyPhotoBLOB {
final static String sDBDriver = "oracle.jdbc.driver.OracleDriver";
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection connSDC =
Connection conn =
String sConnStr = "jdbc:oracle:thin:@127.0.0.1:1521:sledu";
String sConnStrSDC = "jdbc:oracle:thin:@10.10.8.12:1521:rac2";
String sDBUid = "test";
String sDBPwd = "test";
String sDBUidSDC = "sdcmanager";
String sDBPwdSdc = "sdcmanager_888";
applyPhotoBLOB apply = new applyPhotoBLOB();
connSDC = apply.getConn(sConnStrSDC,sDBUidSDC,sDBPwdSdc);
if(connSDC!=null)
apply.testBOLB(connSDC);
System.out.println("处理完成!");
catch(Exception e)
System.out.println(e.getMessage());
if(conn!=null) conn.close();
if(connSDC!=null) connSDC.close();
catch(Exception e)
System.out.println(e.getMessage());
public void testBOLB(Connection conn) throws Exception
String strSQL = "Insert Into BKS_XSZPXX(XH,ZPLXM,ZP) Values('','1',empty_blob())";
updateTable1(strSQL,conn);
conn.setAutoCommit(false);
strSQL = "Select ZP from BKS_XSZPXX where XH='' For Update";
Statement stmt =
ResultSet rs =
stmt = conn.createStatement();
rs = stmt.executeQuery(strSQL);
rs.next();
BLOB blob = (BLOB) rs.getBlob("ZP");
OutputStream os = blob.getBinaryOutputStream();// 建立输出流
BufferedOutputStream output = new BufferedOutputStream(os);
BufferedInputStream input = new BufferedInputStream(new File("F:/.jpg").toURL().openStream());
byte[] buff = new byte[2048000];
//用做文件写入的缓冲
int bytesR
while(-1 != (bytesRead = input.read(buff, 0, buff.length)))
output.write(buff, 0, bytesRead);
//System.out.println(bytesRead);
output.close();
input.close();
rs.close();
conn.setAutoCommit(true);
stmt.close();
private int updateTable1(String strSQL,Connection conn) throws Exception
PreparedStatement stmt =
int result = 0;
stmt = conn.prepareStatement(strSQL);
result = stmt.executeUpdate();
catch(Exception e)
throw new Exception(e.getMessage());
stmt.close();
public Connection getConn(String StrConn,String uid,String pwd) throws Exception
Connection conn =
Class.forName(sDBDriver);
conn = DriverManager.getConnection(StrConn,uid,pwd);
catch (Exception e)
throw new Exception(e.getMessage());
把我上面这两个代码结合一下,就能够解决你的问题。
花了2天才搞定java链接数据库啊 谢谢楼主
& 有蛮多要学哦
好文章!学习了
排版不行啊
代码视图排版不太美观啊,基本思路应该是先读取image类型,然后在以二进制存入oracle的blob
&&您还没有登录,点击这里或之后才能回复!
提问时间: 3 年 前
浏览数: 5096
最后更新时间: 2 年 前
更多文档 →
更多资讯 →java String类型转换为Blob类型_百度知道
java String类型转换为Blob类型
java String类型转换为Blob类型怎么转?
我有更好的答案
按默认排序
这个是mysql下存取blob字段的一个很简单的类,跟据自己的需要改改就行了/*** Title:
BlobPros.java* Project:
test* Description:
把图片存入mysql中的blob字段,并取出* Call Module:
mtools数据库中的tmp表
C:\downloads\luozsh.jpg* Copyright:
Copyright (c) * Company:
uniware* Create Date:
* @Author:
FeiFan* @version 1.0 版本*
Revision history*
Description*
-----------*
对图片进行存取** note:
要把数据库中的Blob字段设为longblob
**///package com.import java.io.*;import java.util.*;import java.sql.*;public class BlobPros{
private static final String URL = &jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true&;
private Connection conn =
private PreparedStatement pstmt =
private ResultSet rs =
private File file =
public BlobPros()
* 向数据库中插入一个新的BLOB对象(图片)
* @param infile - 要输入的数据文件
* @throws java.lang.Exception
public void blobInsert(String infile) throws Exception
FileInputStream fis =
Class.forName(&org.gjt.mm.mysql.Driver&).newInstance();
conn = DriverManager.getConnection(URL);
file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement(&insert into tmp(descs,pic) values(?,?)&);
pstmt.setString(1,file.getName());
//把传过来的第一个参数设为文件名
//pstmt.setBinaryStream(2,fis,(int)file.length());
//这种方法原理上会丢数据,因为file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available());
//第二个参数为文件的内容
pstmt.executeUpdate();
catch(Exception ex)
System.out.println(&[blobInsert error : ]& + ex.toString());
//关闭所打开的对像//
pstmt.close();
fis.close();
conn.close();
* 从数据库中读出BLOB对象
* @param outfile - 输出的数据文件
* @param picID - 要取的图片在数据库中的ID
* @throws java.lang.Exception
public void blobRead(String outfile,int picID) throws Exception
FileOutputStream fos =
InputStream is =
byte[] Buffer = new byte[4096];
Class.forName(&org.gjt.mm.mysql.Driver&).newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement(&select pic from tmp where id=?&);
pstmt.setInt(1,picID);
//传入要取的图片的ID
rs = pstmt.executeQuery();
rs.next();
file = new File(outfile);
if(!file.exists())
file.createNewFile();
//如果文件不存在,则创建
fos = new FileOutputStream(file);
is = rs.getBinaryStream(&pic&);
int size = 0;
/* while(size != -1)
size = is.read(Buffer);
//从数据库中一段一段的读出数据
//System.out.println(size);
if(size != -1)
//-1表示读到了文件末
fos.write(Buffer,0,size);
while((size = is.read(Buffer)) != -1)
极埽岗端瞢得哥全工户
//System.out.println(size);
fos.write(Buffer,0,size);
catch(Exception e)
System.out.println(&[OutPutFile error : ]& + e.getMessage());
//关闭用到的资源
fos.close();
rs.close();
pstmt.close();
conn.close();
public static void main(String[] args)
BlobPros blob = new BlobPros();
//blob.blobInsert(&C:\\Downloads\\luozsh1.jpg&);
blob.blobRead(&c:/downloads/luozishang.jpg&,47);
catch(Exception e)
System.out.println(&[Main func error: ]& + e.getMessage());
可以强转。JAVA 是单根继承模式每个类 都有一个父类名叫Objte
你可以强转。。
但是要保证类型的合适String s = &11&;Blob s = (Blob)((Objte)s);
其他类似问题
blob的相关知识
等待您来回答
您可能关注的推广回答者:回答者:
下载知道APP
随时随地咨询
出门在外也不愁}

我要回帖

更多关于 java oracle blob 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信