A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wangshuying 中级黑马   /  2014-11-16 15:20  /  940 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

mysql数据库中,Blob(二进制数据类型)怎么用

2 个回复

倒序浏览
存图片或者附件
回复 使用道具 举报
  1. import java.sql.*;
  2. import java.io.*;
  3. public class binarydata {

  4.         public static void main(String[] args) {
  5.                 // TODO Auto-generated method stub
  6.                 read();
  7.                 //create();
  8.         }
  9.         public static void create()
  10.         {
  11.                 Connection ct=null;
  12.                 PreparedStatement ps=null;
  13.                 ResultSet rs=null;
  14.                 try
  15.                 {
  16.                             ct=JDBCUtils.getConnection();
  17.                               String sql="insert into picture(picture)values(?)";
  18.                             ps=ct.prepareStatement(sql);
  19.                              InputStream in=new BufferedInputStream(new FileInputStream(new File("1.png")));
  20.                             ps.setBinaryStream(1, in);
  21.                             int i=ps.executeUpdate();
  22.                         System.out.println("i="+i);
  23.                             in.close();
  24.                 }catch(Exception e)
  25.                 {
  26.                         e.printStackTrace();
  27.                 }finally
  28.                 {
  29.                         JDBCUtils.free(rs, ps, ct);
  30.                 }
  31.         }
  32.        
  33.         public static void read()
  34.         {
  35.                 Connection ct=null;
  36.                 PreparedStatement ps=null;
  37.                 ResultSet rs=null;
  38.                 try
  39.                 {
  40.                         ct=JDBCUtils.getConnection();
  41.                         String sql="select picture from picture";
  42.                         ps=ct.prepareStatement(sql);
  43.                         rs=ps.executeQuery();
  44.                         int m=0;
  45.                         while(rs.next())
  46.                         {
  47.                                
  48.                                 InputStream in=rs.getBinaryStream("picture");
  49.                                 OutputStream out=new BufferedOutputStream(new FileOutputStream(new File("D://"+(m++)+".png")));
  50.                                 byte[] buf=new byte[1024];
  51.                                 int i=0;
  52.                                 while((i=in.read(buf))!=-1)
  53.                                 {
  54.                                         out.write(buf,0,i);
  55.                                 }
  56.                                 in.close();
  57.                                 out.close();
  58.                         }
  59.                 }catch(Exception e)
  60.                 {
  61.                         e.printStackTrace();
  62.                 }finally
  63.                 {
  64.                         JDBCUtils.free(rs, ps, ct);
  65.                 }
  66.         }

  67. }
























复制代码


工具类:


  1. import java.sql.*;

  2. public class JDBCUtils {
  3.         private static String url="jdbc:mysql://localhost:3306/user";
  4.         private static String user="root";
  5.         private static String password="root";
  6.        
  7.         private JDBCUtils(){}
  8.        
  9.         static
  10.         {
  11.                 try {
  12.                         Class.forName("com.mysql.jdbc.Driver");
  13.                 } catch (ClassNotFoundException e) {
  14.                         // TODO Auto-generated catch block
  15.                         e.printStackTrace();
  16.                 }
  17.         }
  18.        
  19.         public static Connection getConnection() throws SQLException
  20.         {
  21.                 return DriverManager.getConnection(url,user,password);
  22.         }
  23.        
  24.         public static void free(ResultSet rs,Statement st,Connection ct)
  25.         {
  26.                 try
  27.                 {
  28.                         if(rs!=null)
  29.                                 rs.close();
  30.                 }catch(Exception e)
  31.                 {
  32.                         e.printStackTrace();
  33.                 }
  34.                 finally
  35.                 {
  36.                         try
  37.                         {
  38.                                 if(st!=null)
  39.                                         st.close();
  40.                         }catch(Exception e)
  41.                         {
  42.                                 e.printStackTrace();
  43.                         }finally
  44.                         {
  45.                                 if(ct!=null)
  46.                                         try {
  47.                                                 ct.close();
  48.                                         } catch (SQLException e) {
  49.                                                 // TODO Auto-generated catch block
  50.                                                 e.printStackTrace();
  51.                                         }
  52.                         }
  53.                 }
  54.         }

  55. }
复制代码

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马