JAVA语言写文件存取,存到ORACLE数据库里怎么写?

 我来答
匿名用户
2013-07-28
展开全部
package com.jspdev.ch13;
import com.jspdev.util.*;
import java.sql.*;
import javax.sql.*;
import java.io.*;
import oracle.jdbc.driver.OracleResultSet;
import oracle.sql.BLOB;
public class BlobBean
{

Connection conn ;
/**
*构造方法,创建Connection对象,并且在数据库中添加一个表。
*/
public BlobBean()throws Exception
{

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection("jdbc:oracle:thin:system@127.0.0.1:1521:hellking", "system", "manager");
// conn.createStatement().execute("create table blobtable(blobvalue blob)");
}

/**
*写入Blob数据到数据库
*/
public void addBlob(String fileName)throws Exception
{

conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
stmt.execute("insert into blobtable values (empty_blob())");
ResultSet rset = stmt.executeQuery("SELECT blobvalue FROM blobtable FOR UPDATE");
BLOB blob = null;
while (rset.next()) {
blob = ((OracleResultSet) rset).getBLOB(1);
System.out.println(blob.length());
}
File binaryFile = new File(fileName);
System.out.println(fileName+"'s length = " + binaryFile.length());
FileInputStream instream = new FileInputStream(binaryFile);
OutputStream outstream = blob.getBinaryOutputStream();
int chunk = blob.getChunkSize();
System.out.println("chunk size = " + chunk);
byte[] buffer = new byte[chunk];
int length = -1;
while ((length = instream.read(buffer)) != -1)
outstream.write(buffer, 0, length);
instream.close();
outstream.close();
conn.commit();

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式