利用數據庫如何存入BLOB格式圖片,並從數據庫中取出BLBO格式圖片顯示出來

這兩天須要在圖片存儲性能方面作一些實驗,無非就是兩種方法,一是將圖片以BLOB格式存入數據庫中,二是將圖片路徑存入數據庫中,而後從數據庫中提取出來。java

實驗數據是從1000張圖片中遍歷取出100張,樣本比較小哈。。。。
sql

下面貼出代碼數據庫


數據庫我使用的是國產達夢數據庫,若是要改其餘的話也比較簡單。數組


//package lianjie;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.imageio.ImageIO;
public class DmTest {
// 定義 DM JDBC 驅動串
String jdbcString = "dm.jdbc.driver.DmDriver";
// 定義 DM URL 鏈接串
String urlString = "jdbc:dm://localhost:5236/";
// 定義鏈接用戶名
String userName = "SYSDBA";
// 定義鏈接用戶口令
String password = "SYSDBA";
static //定義sql語句
//String sqlString ="create table yujin3(a int,b int,c int);";
String sqlString1="insert into yujin3  values(123,14,1234);";
// 定義鏈接對象
static Connection conn = null;


static Statement stmt = null;   
static PreparedStatement ps = null;   
static ResultSet rs = null;  


//private static String sqlString1;
/* 加載 JDBC 驅動程序
* @throws SQLException 異常 */
public void loadJdbcDriver() throws SQLException {
try {
System.out.println("Loading JDBC Driver...");
// 加載 JDBC 驅動程序
//DriverManager.registerDriver(new dm.jdbc.driver.DmDriver()); 
Class.forName(jdbcString);
} catch (ClassNotFoundException e) {
throw new SQLException("Load JDBC Driver Error1: " + e.getMessage());
} catch (Exception ex) {
throw new SQLException("Load JDBC Driver Error : "
+ ex.getMessage());
}
}
public void connect() throws SQLException {
try {
System.out.println("Connecting to DM Server...");
// 鏈接 DM 數據庫
conn = DriverManager.getConnection(urlString, userName, password);
} catch (SQLException e) {
throw new SQLException("Connect to DM Server Error : "
+ e.getMessage());
}
}
/* 關閉鏈接
* @throws SQLException 異常 */
public void disConnect() throws SQLException {
try {
// 關閉鏈接
conn.close();
System.out.println("close");
} catch (SQLException e) {
throw new SQLException("close connection error : " + e.getMessage());
}
}

public static void main(String args[]) {

	DmTest basicApp = new DmTest();
	// 加載驅動程序
	try {
		basicApp.loadJdbcDriver();
	} catch (SQLException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}
	try {
		basicApp.connect();
	} catch (SQLException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}
	
	 String sql = "DROP TABLE blobtest";   
     //String sql=null;
     try {   
          stmt = conn.createStatement();   
         System.out.println(sql);   
        stmt.executeUpdate(sql);   
         //建立表   
         sql = "CREATE TABLE blobtest(" +      
         "b_title int,"+   
         "b_text Blob"+    
         ")";   
     System.out.println(sql);   
     stmt.executeUpdate(sql);   
     System.out.println("建立數據表成功!");   
        
     sql="INSERT INTO blobtest(b_title,b_text)VALUES(?,?)";   
     ps = conn.prepareStatement(sql);   
     //插入圖片   
     //File file = new File("F:\\image\\yahoo.jpg");   
     for(int i=0;i<=999;i++)
     {
    	 String s="c:\\images\\"+i+".jpg";
     File file = new File(s);   
     InputStream inputStream = new FileInputStream(file);   
    
     try {   
         ps.setInt(1, i);   
         //新建一byte數組   
         byte[] buf=new byte[inputStream.available()];   
         //將文件讀入到byte[]中   
         inputStream.read(buf);   
         ps.setBytes(2, buf);   
         ps.executeUpdate();   
         System.out.println("圖片"+i+"插入成功!");   
     } catch (IOException e1) {   
         System.out.println("保存圖片到數據庫成功!");   
         e1.printStackTrace();   
     }   
        
     }
	 
     
     sql = "SELECT b_title,b_text FROM blobtest";   
     ps = conn.prepareStatement(sql);   
     rs = ps.executeQuery();   
     while(rs.next()){   
    	 if(rs.getInt(1)%10==0){
	         System.out.println("圖片名: "+rs.getInt(1));    
	         Blob blob = rs.getBlob("b_text");   
	         String s1="c:\\imagett\\"+rs.getInt(1)+".jpg";
	         File file2 = new File(s1);   
	         OutputStream outputStream = new FileOutputStream(file2);   
	         try {   
	             outputStream.write(blob.getBytes(1,(int)blob.length()));   
	         } catch (IOException e) {   
	             e.printStackTrace();   
	         }   
	         //打印出來的爲對象   
	         System.out.println("圖片內容: "+ blob.getBinaryStream());  
    	 }
            
     }   
     } catch (SQLException e) {   
         e.printStackTrace();   
     } catch (FileNotFoundException e) {   
         e.printStackTrace();   
     }finally{   
        try {
			basicApp.disConnect();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  
     }   
        
 }   
}

而後在相應路徑下面就生成了100張圖片,速度仍是比較快的,關於實驗數據你們能夠去網上下載,或者減小樣本,一兩張均可以,只是要稍微修改下代碼就行。
性能

相關文章
相關標籤/搜索