利用数据库如何存入BLOB格式图片,并从数据库中取出BLBO格式图片显示出来

来源:互联网 发布:英格蜜儿 知乎 编辑:程序博客网 时间:2024/05/23 15:36

利用数据库如何存入BLOB格式图片,并从数据库中取出BLBO格式图片显示出来

这两天需要在图片存储性能方面做一些实验,无非就是两种方法,一是将图片以BLOB格式存入数据库中,二是将图片路径存入数据库中,然后从数据库中提取出来。

实验数据是从1000张图片中遍历取出100张,先导入1000张图片进入数据库,然后从数据库导出到本地。样本比较小哈。。。。 下面贴出代码
package DmTest;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;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[]) throws IOException, SQLException {DmTest basicApp = new DmTest();// 加载驱动程序basicApp.loadJdbcDriver();basicApp.connect();    //创建表        String sql = "CREATE TABLE blobtest(" +             "b_title int,"+          "b_text Blob"+           ")";      System.out.println(sql);        stmt = conn.createStatement();    stmt.executeUpdate(sql);      System.out.println("创建数据表成功!");            sql="INSERT INTO blobtest(b_title,b_text)VALUES(?,?)";      ps = conn.prepareStatement(sql);         //插入图片   ,将图片先编程byte数组,然后存入blob字段中   for(int i=0;i<=999;i++)   {   String s="c:\\images\\"+i+".jpg";   File file = new File(s);      InputStream inputStream = new FileInputStream(file);            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+"插入成功!");       }    //读取图片出来,保存到本地的磁盘上面   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());     }             }     basicApp.disConnect();      }   }   



数据库我使用的是国产达梦数据库,如果要改其他的话也比较简单。

然后在相应路径下面就生成了100张图片,速度还是比较快的,关于实验数据大家可以去网上下载,或者减少样本,一两张都可以,只是要稍微修改下代码就行。
1 0