用JAVA读取ORACLE BLOB字段的问题。(急)

The_east_key 2002-03-20 04:14:31
加精
现在我已经成功的将一个大小为1.5k的图片放入数据库字段名为:comment格式为blob中,如何通过JAVA将其取出并保存到一个文件中,和显示在applet上。请帮忙解决一下,
谢谢大家
...全文
278 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
AD1816 2002-04-18
  • 打赏
  • 举报
回复
朋友,我看了你的程序,基本上解决了我得问题,但我想问一下,如果要是用的文件是word文档,既有文本又有图形的呢,我解决不了。出现错误:
异常: Connection reset by peer: JVM_recv in socket input stream read
云智软件 2002-03-28
  • 打赏
  • 举报
回复
只要注意一点就行了
用 ResultSet 取一个 InputStream来然后读取,其它的方式都会有读取不全完的情况.
另外,在表中要保存好图片的类型以便于日后调用,
俺在Oracle 中放过好几M 的东西.没出过问题.
The_east_key 2002-03-26
  • 打赏
  • 举报
回复
我的存入数据库的实际文件的长度是1975字节,但是通过getChunkSize得到的块长度是8000多字节,怎么回事?请指教!
awk 2002-03-25
  • 打赏
  • 举报
回复
将 // int chunk=blob.getChunkSize();
int chunk=id;
改为
int chunk=blob.getChunkSize();
获取正确的块长度。
另外一种方式是:
.......
PreparedStatement pst=
conn.prepareStatement("SELECT * FROM my_blob");
ResultSet rs=(OracleResultSet)pst.executeQuery();
rs.next();
InputStream gif_data = rs.getBinaryStream (2);
FileOutputStream file = null;
try
{

file = new FileOutputStream ("leslie.gif");
int chunk;
while ((chunk = gif_data.read()) != -1)
file.write(chunk);
}
catch (Exception e){
String err = e.toString();
System.out.println(err);
}
......
The_east_key 2002-03-22
  • 打赏
  • 举报
回复
无论如何,首先谢谢河马兄和冷血狐兄的帮助,对于路人甲兄提供信息表示感谢,但是,连接不能用,很遗憾。
------------------------------------------------------------------
对于河马兄的回答有如下疑问,请指教:

BLOB blob;
cmd = "SELECT * FROM my_blob_table WHERE X='row1'";
ResultSet rest = stmt.executeQuery(cmd);

BLOB blob = ((OracleResultSet)rset).getBLOB(2);//得到blob数据
//上面这行代码,为什么要将rset强制转换成OracleResultSet类型的?

File binaryFile = new File("john.gif");

System.out.println("john.gif length = " + binaryFile.length());
//上面这行代码有和意义?无论怎么打印,长度均为0

FileInputStream instream = new FileInputStream(binaryFile);
//以上这行代码我不是很理解。因为我现在需要向文件写,而不是从文件读呀,如果binaryFile文件不存在的时候,运行出错。

OutputStream outstream = blob.getBinaryOutputStream();

int chunk = blob.getChunkSize();
//getChunkSize()是什么意思?

byte[] buffer = new byte[chunk];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);//写入文件
//此时,outstream仅仅是一个输出流而已,怎么写到文件里面呢?
instream.close();
outstream.close();
对于以上问题,请指教,谢谢!
---------------------------------------------------------------
---------------------------------------------------------------

对于冷血狐兄的回答,有一些问题,也想请教。
首先是Getting BLOB and CLOB Locators from a Result Set
// Select LOB locator into standard result set.
ResultSet rs =stmt.executeQuery ("SELECT blob_col, clob_col FROM lob_table");
//以上代码我在运行时,出错,错误信息是:找不到字段:blob_col,clob_col,当然,我是用我的字段,但是,也不行,用*可以,为什么?好像不能用字段名似的。


while (rs.next())
{// Get LOB locators into Java wrapper classes.
oracle.jdbc2.Blob blob = (oracle.jdbc2.Blob)rs.getObject(1);
oracle.jdbc2.Clob clob = (oracle.jdbc2.Clob)rs.getObject(2);
[...process...]
}
然后是Read BLOB data from BLOB locator.
InputStream byte_stream = my_blob.getBinaryStream();
byte [] byte_array = new byte [10];
int bytes_read = byte_stream.read(byte_array);
和Writing BLOB Data
java.io.OutputStream outstream;
// read data into a byte array
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// write the array of binary data to a BLOB
outstream = ((BLOB)my_blob).getBinaryOutputStream();
outstream.write(data);
还有Passing a BLOB Locator to a Prepared Statement
OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
"INSERT INTO blob_table VALUES(?)");
ops.setBLOB(1, my_blob);
ops.execute();
----------------------------------------------------------
----------------------------------------------------------
以下是我这两天的结果,有不对的地方,请指教。谢谢!
//以下是需要用到的类
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.sql.*;
import java.io.*;
import oracle.sql.BLOB;
import oracle.jdbc.*;

//此程序的作用:向test(字段为:id number;comment blob)库,插入一个文件(vc.doc),再将其从库中读出,写到save.DOC中

void button1_actionPerformed(ActionEvent e) {
try
{
//首先是将文件输入到数据库。
Class.forName("oracle.jdbc.driver.OracleDriver");//注册数据库引擎。
Connection conn= DriverManager.getConnection("jdbc:oracle:oci8:@sxm", "sunxiaoming", "sunxiaoming");//建立连接串
conn.setAutoCommit(false);//关闭自动提交,以提高性能。
Statement stmt=conn.createStatement();//建立会话

File file=new File("VC.DOC");
InputStream is=new FileInputStream(file);//创建输入流,将外部文件输入到InputStream 中。
System.out.print(file.length());
PreparedStatement pstmt=conn.prepareStatement("INSERT INTO TEST VALUES (?,?)");
pstmt.setInt(1,(int)file.length());
pstmt.setBinaryStream(2,is,(int)file.length());
pstmt.executeUpdate();//将文件流插入到数据库中。
pstmt.close();

//以下是从库中读取文件。
ResultSet rset=stmt.executeQuery("SELECT * from test");
while (rset.next())
{// Get LOB locators into Java wrapper classes.
int id=rset.getInt(1);//获取文件长度。
BLOB blob=((OracleResultSet)rset).getBLOB(2);//获取文件字段。
// int chunk=blob.getChunkSize();
int chunk=id;
byte [] buffer=new byte[chunk];
System.out.println(chunk);

File binaryFile=new File("save.DOC");
FileOutputStream fileoutstream=new FileOutputStream(binaryFile);//创建文件输出流。
InputStream instream=blob.getBinaryStream();//建立输入流,并将字段comment的值以流的形式,放入instream变量。
instream.read(buffer,0,chunk);//将文件流存入变量buffer,以buffer为中转
fileoutstream.write(buffer,0,chunk);//将buffer写入文件输出流。
System.out.println("write ok!");
instream.close();//关闭流
fileoutstream.close();
}
rset.close();
stmt.close();
conn.close();
System.out.print("ok");
}
catch(Exception ee)
{
System.out.println(ee.getMessage());
}
}
//以上是我的程序,请指教。
The_east_key 2002-03-22
  • 打赏
  • 举报
回复
请问你们的开发环境是什么?
skyyoung 2002-03-21
  • 打赏
  • 举报
回复
http://www.csdn.net/Expert/TopicView.asp?id=83096&datebasetype=200101
coldbloodfox 2002-03-21
  • 打赏
  • 举报
回复
这个例子我记得已经发到你上个帖子上了难道读不出来吗?
有什么问题沟通
(三)用jdbc处理lob
exmple 4.
首先是Getting BLOB and CLOB Locators from a Result Set
// Select LOB locator into standard result set.
ResultSet rs =stmt.executeQuery ("SELECT blob_col, clob_col FROM lob_table");
while (rs.next())
{// Get LOB locators into Java wrapper classes.
oracle.jdbc2.Blob blob = (oracle.jdbc2.Blob)rs.getObject(1);
oracle.jdbc2.Clob clob = (oracle.jdbc2.Clob)rs.getObject(2);
[...process...]
}
然后是Read BLOB data from BLOB locator.
InputStream byte_stream = my_blob.getBinaryStream();
byte [] byte_array = new byte [10];
int bytes_read = byte_stream.read(byte_array);
和Writing BLOB Data
java.io.OutputStream outstream;
// read data into a byte array
byte[] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// write the array of binary data to a BLOB
outstream = ((BLOB)my_blob).getBinaryOutputStream();
outstream.write(data);
还有Passing a BLOB Locator to a Prepared Statement
OraclePreparedStatement ops = (OraclePreparedStatement)conn.prepareStatement
"INSERT INTO blob_table VALUES(?)");
ops.setBLOB(1, my_blob);
ops.execute();
AndrewT 2002-03-20
  • 打赏
  • 举报
回复
BLOB blob;
cmd = "SELECT * FROM my_blob_table WHERE X='row1'";
ResultSet rest = stmt.executeQuery(cmd);
BLOB blob = ((OracleResultSet)rset).getBLOB(2);//得到blob数据
File binaryFile = new File("john.gif");
System.out.println("john.gif length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
byte[] buffer = new byte[chunk];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);//写入文件
instream.close();
outstream.close();

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧