• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

mysql 搞代码 4年前 (2022-01-09) 18次浏览 已收录 0个评论

这两天需要在图片存储性能方面做一些实验,无非就是两种方法,一是将图片以BLOB式存入数据库中,二是将图片路径存入数据库中,然后从数据库中提取出来。 实验数据是从1000张图片中遍历取出100张,样本比较小哈。。。。 下面贴出代码 数据库我使用的是国产达

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

实验数据是从1000张图片中遍历取出100张,样本比较小哈。。。。

下面贴出代码


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

//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<b>本文来源gao@!dai!ma.com搞$$代^@码5网@</b>.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张图片,速度还是比较快的,关于实验数据大家可以去网上下载,或者减少样本,一两张都可以,只是要稍微修改下代码就行。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:利用数据库如何存入BLOB格式图片,并从数据库中取出BLBO格式图片
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址