如何将二进制文件存入Oracle数据库中

如何将二进制文件存入Oracle数据库中,第1张

先把文件读取到内存,再以二进制格式保持到数据库中的大字段中(clob或clob)。

写大对象。

Java code

public static void main(String[] args) {

// TODO Auto-generated method stub

Connection conn = null

Statement stat = null

ResultSet rs = null

OutputStream os = null

FileInputStream fis = null

int bs = 0

try {

Class.forName("oracle.jdbc.driver.OracleDriver")

conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","---")

conn.setAutoCommit(false)

stat = conn.createStatement()

stat.executeUpdate("insert into t_video(id,video) values(1,empty_blob())")

rs = stat.executeQuery("select video from t_video where id = 1")

rs.next()

oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1)

os = blo.getBinaryOutputStream()

bs = blo.getBufferSize()

fis = new FileInputStream("D:\\Temp\\MPlayer-CVS-20040808-K&K\\mplayer.exe")

byte[] buf = new byte[bs]

int length = 0

while(true)

{

length = fis.read(buf)

if(length == -1) break

os.write(buf,0,length)

}

os.close()

os = null

fis.close()

fis = null

conn.commit()

conn.setAutoCommit(true)

conn.close()

} catch(Exception ex) {

ex.printStackTrace()

}

}

读大对象

Java code

InputStream is = null

FileOutputStream fos = null

byte[] buf = null

int bs = 0

try {

Class.forName("oracle.jdbc.driver.OracleDriver")

conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oraDB","bigfou","-")

conn.setAutoCommit(false)

stat = conn.createStatement()

rs = stat.executeQuery("select video from t_video where id = 1")

rs.next()

oracle.sql.BLOB blo = (oracle.sql.BLOB)rs.getBlob(1)

bs = blo.getBufferSize()

buf = new byte[bs]

int length = 0

is = blo.getBinaryStream()

fos = new FileOutputStream("d:\\test.exe")

while(true) {

length = is.read(buf)

if(length == -1) break

fos.write(buf,0,length)

}

fos.close()

fos = null

is.close()

is = null

conn.commit()

conn.setAutoCommit(true)

conn.close()

...

这个很简单的,这要把表单<form enctype="multipart/form-data">这样设置一下,表单里面的数据就是以二进制的形式传到数据库的,至于怎么传到数据库,这个就不用说吧,一个SQL插入语句就行了的,。

//保存文件到SQL Server数据库中

FileInfo fi=new FileInfo(fileName)

FileStream fs=fi.OpenRead()

byte[] bytes=new byte[fs.Length]

fs.Read(bytes,0,Convert.ToInt32(fs.Length))

SqlCommand cm=new SqlCommand()

cm.Connection=cn

cm.CommandType=CommandType.Text

if(cn.State==0) cn.Open()

cm.CommandText="insert into "+tableName+"("+fieldName+") values(@file)"

SqlParameter spFile=new SqlParameter("@file",SqlDbType.Image)

spFile.Value=bytes

cm.Parameters.Add(spFile)

cm.ExecuteNonQuery()

//保存文件到Access数据库中

FileInfo fi=new FileInfo(fileName)

FileStream fs=fi.OpenRead()

byte[] bytes=new byte[fs.Length]

fs.Read(bytes,0,Convert.ToInt32(fs.Length))

OleDbCommand cm=new OleDbCommand()

cm.Connection=cn

cm.CommandType=CommandType.Text

if(cn.State==0) cn.Open()

cm.CommandText="insert into "+tableName+"("+fieldName+") values(@file)"

OleDbParameter spFile=new OleDbParameter("@file",OleDbType.Binary)

spFile.Value=bytes

cm.Parameters.Add(spFile)

cm.ExecuteNonQuery()

代码中的fileName是文件的完整名称,tableName是要 *** 作的表名称,fieldName是要保存文件的字段名称


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10045008.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存