如何将二进制文件存入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()

...

方法一:菜单 *** 作方式,推荐使用:“文件”=》另存为=》保存类型选择“EXCEL二进制工作簿(*.xlsb)”

方法二:采用VBA方式处理二进制格式存储文件,打开亦然。只是过程复杂,一般只在需要定制要求时才考虑。

用文件流的方式,把从文件中读出的数据转换成二进制,从数据库中读出就是反方向的:** void button1_Click(object sender, EventArgs e){byte[] bufferbuffer = File.ReadAllBytes(\"readme.doc\")//读取文件内容//创建连接SqlConnection connect = new SqlConnection(@\"Integrated Security=SSPIPersist Security Info=FalseInitial Catalog=BSPlatform2008Data Source=.\\SqlExpress\")SqlCommand cmd = connect.CreateCommand()cmd.CommandText = \"INSERT INTO Tmp (FileContent) VALUES (@FileContent)\"//FileContent字段是Image类型cmd.Parameters.Add(\"@FileContent\", SqlDbType.Image)cmd.Parameters[\"@FileContent\"].Value = buffer//接受byte[]类型的值connect.Open()cmd.ExecuteNonQuery()connect.Close()} 查看更多答案>>


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存