Blob使用中遇到不能置值的问题,牛人给指点一下!!
下面是我的代码
File file = new File("文件名");
byte[] fileByte = new byte[ (int)file.length()];
FileInputStream stream = new FileInputStream(file);
stream.read( fileByte);
Blob blob = null;
OutputStream writer = blob.setBinaryStream( fileByte.length);
writer.writer( fileByte,0,fileByte.length);
writer.close();
我看了,论坛上的帖子,blob都是从数据库得到初值,我不想这样,可是现在这样用,在执行到初始一个OutputStream的时候抛了异常,我看是因为blob为空造成的,可是不知道怎么能让这个blob初始化,能给我给个思路吗?
最好能有可以实现的代码!!!谢谢!!!
问题点数:0、回复次数:8Top
1 楼LIGE(静水深流)回复于 2004-12-01 20:45:22 得分 0
我看了好多网上的例子,都是先建立数据库连接,然后给数据库置一个EMPTY_BLOB()后,然后对获取这个单元的cursor,然后才可以对这个单元置值,是不是真的不能先有一个BLOB的对象,然后用这个对象对一个要使用BLOB为参数的方法赋值?????真的不可以吗????大牛给说说呀!!!!!!!!!!!!!!
跪求!!!!!!!!!!!!!!!!!!!!!!!!Top
2 楼LIGE(静水深流)回复于 2004-12-02 12:56:07 得分 0
怎么牛人都回家睡觉去了!!郁闷!Top
3 楼fast_time(fast_time)回复于 2004-12-02 13:32:01 得分 0
必须先要设置空置,才能进行修改Top
4 楼chengys()回复于 2004-12-02 13:38:57 得分 0
File file = new File("文件名");
byte[] fileByte = new byte[ (int)file.length()];
FileInputStream stream = new FileInputStream(file);
stream.read( fileByte);
Blob blob = new Blob();
OutputStream writer = blob.setBinaryStream( fileByte.length);
writer.writer( fileByte,0,fileByte.length);
writer.close();
Top
5 楼tigeryu(吴越小虎)回复于 2004-12-02 13:42:06 得分 0
必须先要设置空置,才能进行修改,这是oracle很特殊的地方,可能就是为了让人不能轻易的移植到别的数据库上吧
说得一点没错,如果你插入的是普通的文本的话,应该按你的做法也没有问题,但如果是其中有其它二进制内容的话就会出错Top
6 楼LIGE(静水深流)回复于 2004-12-02 14:37:22 得分 0
chengys()
你的帖子里面说道的Blob blob = new BLob();
是错误的!BLob是个接口类,是不能这样实例化!所以用new来生成是错的!
tigeryu(吴越小虎)
我其实就是想输入图片!难道就没有办法了吗?Top
7 楼liuchuntao(世寒)回复于 2004-12-10 14:56:42 得分 0
统一 tigeryu(吴越小虎) 的观点
import oracle.sql.*;
import java.sql.*;
import java.io.*;
public class WriteBlob {
public static void main(String[] args) {
try {
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.204:1521:main", "lct", "lct");
conn.setAutoCommit(false);
BLOB blob = null;
//清空
PreparedStatement pstmt = conn.prepareStatement(
"update javatest set content=empty_blob() where name=?");
pstmt.setString(1, "liuchuntao");
pstmt.executeUpdate();
//更新
pstmt = conn.prepareStatement(
"select content from javatest where name= ? for update");
pstmt.setString(1, "liuchuntao");
ResultSet rset = pstmt.executeQuery();
if (rset.next()) {
blob = (BLOB) rset.getBlob(1);
}
String fileName = "e:\\pic\\1.jpg";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
System.out.println("file size = " + fin.available());
pstmt = conn.prepareStatement(
"update javatest set content=? where name=?");
OutputStream out = blob.getBinaryOutputStream();
int count = -1, total = 0;
byte[] data = new byte[ (int) fin.available()];
fin.read(data);
out.write(data);
fin.close();
out.close();
pstmt.setBlob(1, blob);
pstmt.setString(2, "liuchuntao");
pstmt.executeUpdate();
pstmt.close();
conn.commit();
rset.close();
conn.close();
}
catch (SQLException e) {
System.err.println(e.getMessage());
System.out.println(e.getErrorCode());
e.printStackTrace();
}
catch (IOException e) {
System.err.println(e.getMessage());
}
}
}Top
8 楼aixy(不可说)回复于 2004-12-28 17:57:00 得分 0
//清空
PreparedStatement pstmt = conn.prepareStatement(
"update javatest set content=empty_blob() where name=?");
pstmt.setString(1, "liuchuntao");
pstmt.executeUpdate();
//更新
pstmt = conn.prepareStatement(
"select content from javatest where name= ? for update");
pstmt.setString(1, "liuchuntao");
ResultSet rset = pstmt.executeQuery();
if (rset.next()) {
blob = (BLOB) rset.getBlob(1);
}
-------------------------------------------------------
这个有问题吧!如果表中没有一条name="liuchuntao"的纪录,你的清空动作什么也没做;接下来的select也就取不到任何纪录;
即使表中有一条name="liuchuntao"的纪录,你把content置空了,这一句
blob = (BLOB) rset.getBlob(1);
得到的blob的值就将是null,进行转型难道不出错吗?Top




