當前位置:首頁 » 操作系統 » 資料庫blob欄位

資料庫blob欄位

發布時間: 2023-03-07 14:52:19

A. 請問sql server2008資料庫如何讀取BLOB欄位的值

@Override
public String getFormViewById(final String id) {
return (String)getHibernateTemplate().execute(new HibernateCallback<Object>(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
String sql = "select formView from sys_forms where id='" + id + "'";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String content = "";
try {
conn = session.connection();
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){
Blob blob = (Blob) rs.getBlob("formView");
int bolblen = (int) blob.length();
byte[] data = blob.getBytes(1, bolblen);
content = new String(data,"utf-8");
}
}finally{
conn.close();
stmt.close();
}

return content;
}
});
}

B. 使用java語言操作,如何來實現MySQL中Blob欄位的存取

/**
* Title: BlobPros.java
* Project: test
* Description: 把圖片存入mysql中的blob欄位,並取出
* Call Mole: mtools資料庫中的tmp表
* File: C:downloadsluozsh.jpg
* Copyright: Copyright (c) 2003-2003
* Company: uniware
* Create Date: 2002.12.5
* @Author: ChenQH
* @version 1.0 版本*
*
* Revision history
* Name Date Description
* ---- ---- -----------
* Chenqh 2003.12.5 對圖片進行存取
*
* note: 要把資料庫中的Blob欄位設為longblob
*
*/

//package com.uniware;

import java.io.*;
import java.util.*;
import java.sql.*;

public class BlobPros
{
private static final String URL = "jdbc:mysql://10.144.123.63:3306/mtools?user=wind&password=123&useUnicode=true";
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
private File file = null;

public BlobPros()
{
}

/**
* 向資料庫中插入一個新的BLOB對象(圖片)
* @param infile 要輸入的數據文件
* @throws java.lang.Exception
*/
public void blobInsert(String infile) throws Exception
{
FileInputStream fis = null;
try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);

file = new File(infile);
fis = new FileInputStream(file);
//InputStream fis = new FileInputStream(infile);
pstmt = conn.prepareStatement("insert into tmp(descs,pic) values(?,?)");
pstmt.setString(1,file.getName()); //把傳過來的第一個參數設為文件名
//pstmt.setBinaryStream(2,fis,(int)file.length()); //這種方法原理上會丟數據,因為file.length()返回的是long型
pstmt.setBinaryStream(2,fis,fis.available()); //第二個參數為文件的內容
pstmt.executeUpdate();
}
catch(Exception ex)
{
System.out.println("[blobInsert error : ]" + ex.toString());
}
finally
{
//關閉所打開的對像//
pstmt.close();
fis.close();
conn.close();
}
}

/**
* 從資料庫中讀出BLOB對象
* @param outfile 輸出的數據文件
* @param picID 要取的圖片在資料庫中的ID
* @throws java.lang.Exception
*/

public void blobRead(String outfile,int picID) throws Exception
{
FileOutputStream fos = null;
InputStream is = null;
byte[] Buffer = new byte[4096];

try
{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection(URL);
pstmt = conn.prepareStatement("select pic from tmp where id=?");
pstmt.setInt(1,picID); //傳入要取的圖片的ID
rs = pstmt.executeQuery();
rs.next();

file = new File(outfile);
if(!file.exists())
{
file.createNewFile(); //如果文件不存在,則創建
}
fos = new FileOutputStream(file);
is = rs.getBinaryStream("pic");
int size = 0;
/* while(size != -1)
{
size = is.read(Buffer); //從資料庫中一段一段的讀出數據
//System.out.println(size);
if(size != -1) //-1表示讀到了文件末
fos.write(Buffer,0,size);
} */
while((size = is.read(Buffer)) != -1)
{
//System.out.println(size);
fos.write(Buffer,0,size);
}

}
catch(Exception e)
{
System.out.println("[OutPutFile error : ]" + e.getMessage());
}
finally
{
//關閉用到的資源
fos.close();
rs.close();
pstmt.close();
conn.close();
}
}

public static void main(String[] args)
{
try
{

BlobPros blob = new BlobPros();
//blob.blobInsert("C:Downloadsluozsh1.jpg");
blob.blobRead("c:/downloads/1.jpg",47);
}
catch(Exception e)
{
System.out.println("[Main func error: ]" + e.getMessage());
}
}
}

熱點內容
促銷升級源碼 發布:2025-08-21 13:20:39 瀏覽:67
各大資料庫 發布:2025-08-21 13:13:53 瀏覽:810
設計圖庫加密 發布:2025-08-21 13:06:38 瀏覽:515
索引存儲是數據的存儲方式么 發布:2025-08-21 13:05:31 瀏覽:506
我的世界大型伺服器ip大全 發布:2025-08-21 12:54:23 瀏覽:578
htcandroidl 發布:2025-08-21 12:50:59 瀏覽:963
編程拳皇 發布:2025-08-21 12:50:57 瀏覽:330
sqlserver2005作業 發布:2025-08-21 12:15:25 瀏覽:325
安卓手機怎麼設置側邊任務欄 發布:2025-08-21 12:11:10 瀏覽:774
二級c能編譯嗎 發布:2025-08-21 11:58:53 瀏覽:973