写大对象。
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()
...
用文件流的方式,把从文件中读出的数据转换成二进制,从数据库中读出就是反方向的:** 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()} 查看更多答案>>数据库中的二进制文件是没有办法直接在数据库中显示的,只可以通过程序进行数据库图片的插入和读取,不知道你使用的是是么开发语言。我以C#代码为例。
一般的思路都是:
现将图片存入数据库。
// 需要保存的图片为image1
// 先将图片保存起来
image1.Save("a.bmp")
// 获得文件流
FileStream fileStream = new FileStream(”a.bmp”, FileMode.Open,FileAccess.Read)
// 将文件流转化成二进制流
BinaryReader binaryReader = new BinaryReader(fileStream)
byte[] img = binaryReader.ReadBytes((int)fileStream.Length)
binaryReader.Close()
fileStream.Close()
// 可以把文件删除
File.Delete(”a.bmp”)
string sql = ”insert into PIC(Pic) values (@img)”
// 获取数据库连接方法getConn()自己写
SqlConnection conn =getConn()
SqlCommand comm = new SqlCommand(sql, conn)
comm.Parameters.Add(”@img”, SqlDbType.Image).Value = img
conn.Open()
comm.ExecuteNonQuery()
// 这样就已经将图片存到了数据库中
conn.Close()
其次就是读取。比较简单。
string sql = ”select Pic from PIC where”+ 你的检索条件
SqlConnection conn = getConn()
SqlCommand comm = new SqlCommand(sql, conn)
conn.Open()
// 获取第一条数据
byte[] dta = (byte[])comm.ExecuteScalar()
conn.Close()
try
{
MemoryStream ms = new MemoryStream(dta)
Image newImage= Image.FromStream(ms)
}
catch
{
MessageBox.Show(”Error”)
}
这样就实现了图片数据的存取功能。
这只是通过C#的方法描述下基本思路,其他语言都是相类似的方法,只需要找到相同功能的API函数就行,祝你成功。赞同8| 评论
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)