oracle数据库表中,存在一个blog类型的字段,怎么用Java取出

这个字段是二进制存入数据库的,要实现上传下载,怎么做?
哪里有参考,没做过,好迷茫

package DBtest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Blob;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class getDataFrmDB {
public getDataFrmDB() {
}
public static void main(String args[]) {
getDataFrmDB test = new getDataFrmDB();
if (test.getDate()) {
System.out.print("操作成功!");
}
else {
System.out.print("操作异常!");
}
}
public boolean getDate() {
Connection conn = null;
Statement sql = null;
ResultSet rs = null;
try {
try {
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String sourceURL = "jdbc:odbc:ORDB";
Class.forName("oracle.jdbc.driver.OracleDriver");
String sourceURL = "jdbc:oracle:thin:@127.0.0.1:1521:ORDB";
String user = "thamsdt";
String password = "thamsdt";
conn = DriverManager.getConnection(sourceURL, user, password);
sql = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//注:“ini”字段为BLOB字段
String sqlstr = "Select username,ini from S_USER where usercode='ROOT'";
rs = sql.executeQuery(sqlstr);
while (rs.next()) {
String name = rs.getString("username");
//如下使用JdbcOdbcDriver则报错:Hit uncaught exception java.lang.UnsupportedOperationException
//java.sql.Blob blob = rs.getBlob("ini");
//注意这里的写法:使用的是OracleDriver
oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob("ini");
String filepath = "C:/" + name + ".ini";
System.out.println("输出文件路径为:" + filepath);
try {
InputStream in = blob.getBinaryStream(); // 建立输出流
FileOutputStream file = new FileOutputStream(filepath);
int len = (int) blob.length();
byte[] buffer = new byte[len]; // 建立缓冲区
while ( (len = in.read(buffer)) != -1) {
file.write(buffer, 0, len);
}
file.close();
in.close();
}
catch (Exception e) {
System.out.println("I/O Exception.");
return false;
}
}
}
finally {
rs.close();
sql.close();
conn.close();
}
}
catch (SQLException e) {
System.out.println("SQLException.");
return false;
}
catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException.");
return false;
}
return true;
}
}追问

是要把文件存入数据库中不是保存文件,用二进制的形式存入数据库中!!这样好像是存了文件啊

追答

写入看这个
数据库中提供了两种字段类型 Blob 和 Clob 用于存储大型字符串或二进制数据(如图片)。

Blob 采用单字节存储,适合保存二进制数据,如图片文件。
Clob 采用多字节存储,适合保存大型文本数据。

Oracle中处理BLOB/CLOB字段的方式比较特别,所以需要特别注意下面两点:

1. 在Oracle JDBC中采用流机制对 BLOB/CLOB 进行读写操作,所以要注意不能在批处理中读写 BLOB/CLOB字段,否则将出现
Stream type cannot be used in batching 异常。

2. Oracle BLOB/CLOB 字段本身拥有一个游标(cursor),JDBC通过游标对Blob/Clob字段进行操作,在Blob/Clob字段创建之前,无法获取其游标句柄,会出现
Connection reset by peer: socket write error 异常。

正确的做法是:首先创建一个空 Blob/Clob 字段,再从这个空 Blob/Clob字段获取游标,例如下面的代码:

PreparedStatement ps = conn.prepareStatement( " insert into PICTURE(image,resume) values(?,?) " );
// 通过oralce.sql.BLOB/CLOB.empty_lob()构造空Blob/Clob对象
ps.setBlob( 1 ,oracle.sql.BLOB.empty_lob());
ps.setClob( 2 ,oracle.sql.CLOB.empty_lob());

ps.excuteUpdate();
ps.close();

// 再次对读出Blob/Clob句柄
ps = conn.prepareStatement( " select image,resume from PICTURE where id=? for update " );
ps.setInt( 1 , 100 );

ResultSet rs = ps.executeQuery();
rs.next();

oracle.sql.BLOB imgBlob = (oracle.sql.BLOB)rs.getBlob( 1 );
oracle.sql.CLOB resClob = (oracle.sql.CLOB)rs.getClob( 2 );

// 将二进制数据写入Blob
FileInputStream inStream = new FileInputStream( " c://image.jpg " );
OutputStream outStream = imgBlob.getBinaryOutputStream();

byte [] buf = new byte [ 10240 ];
int len;
while (len = inStream.read(buf) > 0 ) {
outStream.write(buf, 0 ,len);
}
inStream.close();
outStream.cloese();

// 将字符串写入Clob
resClob.putString( 1 , " this is a clob " );

// 再将Blob/Clob字段更新到数据库
ps = conn.prepareStatement( " update PICTURE set image=? and resume=? where id=? " );
ps.setBlob( 1 ,imgBlob);
ps.setClob( 2 ,resClob);
ps.setInt( 3 , 100 );

ps.executeUpdate();
ps.close();

温馨提示:答案为网友推荐,仅供参考
相似回答