Java实现MySQL图片存取操作

来源:互联网 发布:网络机顶盒电视不出来 编辑:程序博客网 时间:2024/05/21 17:21

http://blog.csdn.net/thc1987/article/details/3972201


 2698人阅读 评论(0) 收藏 举报
 分类:

存入操作

[java] view plaincopy
  1. /* 
  2. ---------------表结构------------ 
  3. 表名:student2 
  4.  
  5. +--------+-------------+------+-----+---------+-------+ 
  6. | Field  | Type        | Null | Key | Default | Extra | 
  7. +--------+-------------+------+-----+---------+-------+ 
  8. | id     | int(4)      | NO   | PRI | NULL    |       | 
  9. | name   | varchar(20) | YES  |     | NULL    |       | 
  10. | stupic | blob        | YES  |     | NULL    |       | 
  11. +--------+-------------+------+-----+---------+-------+ 
  12.  
  13. */  
  14. package com.ibm.jdbc;  
  15.   
  16. import java.io.*;  
  17. import java.sql.*;  
  18. public class StoreBLOB {  
  19.     public static void main(String[] args) {  
  20.         //连接MySQl数据库  
  21.         Connection con=DBManager.getConnection();  
  22.         PreparedStatement ps=null;  
  23.         InputStream in=null;   
  24.         try {  
  25.             //从本地硬盘读取一张读片  
  26.             in=new FileInputStream("d:/111.jpg");  
  27.             ps=con.prepareStatement("insert into student2 values(?,?,?)");  
  28.             ps.setInt(1,2);  
  29.             ps.setString(2"Tom");  
  30.             ps.setBinaryStream(3, in, in.available());  
  31.             ps.executeUpdate();  
  32.         } catch (IOException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }catch (SQLException e) {  
  36.                 // TODO Auto-generated catch block  
  37.                 e.printStackTrace();  
  38.             }  
  39.         finally  
  40.         {  
  41.             try {  
  42.                 //关闭流  
  43.                 if(in!=null) in.close();  
  44.             } catch (IOException e) {  
  45.                 // TODO Auto-generated catch block  
  46.                 e.printStackTrace();  
  47.             }  
  48.             //关闭相关连接  
  49.             DBManager.close(ps, con);  
  50.               
  51.         }  
  52.           
  53.     }  
  54. }  

取出操作

 

[java] view plaincopy
  1. package com.ibm.jdbc;  
  2.   
  3. import java.sql.*;  
  4. import java.io.*;  
  5. public class GetBLOB {  
  6.     public static void main(String[] args) {  
  7.         Connection con=DBManager.getConnection();  
  8.         Statement st=null;  
  9.         ResultSet rs=null;  
  10.         InputStream in=null;  
  11.         OutputStream out=null;  
  12.   
  13.         try {  
  14.             st=con.createStatement();  
  15.             rs=st.executeQuery("select stupic from student2 where id=2");  
  16.             rs.next();  //将光标指向第一行  
  17.             //从rs中读取stupic放进InputStream对象中  
  18.             in=rs.getBinaryStream("stupic");  
  19.             //申明byte数组,用来存放图片流  
  20.             byte[] b=new byte[40000];  
  21.             in.read(b); //从InputStream对象中读取数据放进byte数组中  
  22.             //实例化OutputStream对象,在D盘创建一个图片文件  
  23.             out=new FileOutputStream("d:/222.jpg");  
  24.             //将文件输出,内容则为byte数组里面的数据  
  25.             out.write(b);  
  26.             out.flush();  
  27.   
  28.         } catch (SQLException e) {  
  29.             // TODO Auto-generated catch block  
  30.             e.printStackTrace();  
  31.         }  
  32.         catch (IOException e) {  
  33.             // TODO Auto-generated catch block  
  34.             e.printStackTrace();  
  35.         }  
  36.         finally  
  37.         {  
  38.             try {  
  39.                 if(in!=null)  
  40.                     in.close();  
  41.                 if(out!=null)  
  42.                     out.close();  
  43.             } catch (IOException e) {  
  44.                 // TODO Auto-generated catch block  
  45.                 e.printStackTrace();  
  46.             }  
  47.             DBManager.close(rs, st, con);//关闭相关连接  
  48.         }  
  49.     }  
  50. }  

0

0 0