dedecms织梦内容管理系统    
首页 | java | C/C++ | PHP | 操作系统 | ajax | 脚本编程 | 安全技术 | 本站下载页 | 专题 | QQ群 | 测试中心 | 会员中心 | 积分规则
  当前位置:主页>java>文章内容
非常简单的mysql的BlobTest实例
来源: 作者:
  1. package org.mmc.dao.impl;  
  2.   
  3. import java.io.*;  
  4. import java.sql.*;  
  5. /** 
  6.  *测试操作blob数据 
  7.  */  
  8. public class BlobTest  
  9. {  
  10.     Connection con=null;  
  11.     /** 
  12.      *往数据库中添加BLOB数据 
  13.      */  
  14.     public void addBlob()  
  15.     {  
  16.         try  
  17.         {  
  18.   
  19.             //创建一个PreparedStatement实例  
  20.             PreparedStatement pstmt=con.prepareStatement("insert into blobtest values(1,?)");  
  21.             File file = new File("d:\\other\\apple.gif") ;  
  22.             FileInputStream fis = new FileInputStream(file);              
  23.             //把输入流设置为预处理语句的对象。  
  24.             pstmt.setBinaryStream(1, fis, (int)file.length());        
  25.             //执行更新  
  26.             pstmt.executeUpdate();                    
  27.             pstmt.close();  
  28.             fis.close();  
  29.   
  30.         }  
  31.         catch(Exception e)  
  32.         {  
  33.             e.printStackTrace();  
  34.         }         
  35.     }  
  36.      /** 
  37.       *创建一个表 
  38.       */  
  39.      public void createTable()  
  40.      {  
  41.         try  
  42.         {  
  43.             con.createStatement().execute("create table blobtest (id int ,pic blob,"+  
  44.               "constraint pk_blobtest primary key(id));");  
  45.          }  
  46.          catch(Exception e)  
  47.          {  
  48.             e.printStackTrace();  
  49.          }  
  50.     }  
  51.     /** 
  52.      *从数据库中读取BLOB数据 
  53.      */  
  54.     public void getBlob()  
  55.     {  
  56.           
  57.      try  
  58.        {      
  59.             //创建一个Statement实例  
  60.             Statement stmt=con.createStatement();  
  61.             ResultSet rst=stmt.executeQuery("select * from blobtest where id=1");  
  62.             rst.next();   
  63.             //获得blob数据和它的输入流,然后通过输入流把数据写到文件中。         
  64.             Blob blob = rst.getBlob("pic") ;  
  65.             FileOutputStream out=new FileOutputStream(new File("d:/other/apple1.gif"));  
  66.             InputStream in=blob.getBinaryStream();  
  67.             int i;  
  68.             while((i=in.read())!=-1)  
  69.             out.write(i);  
  70.             //关闭输入、输出流.  
  71.             in.close();  
  72.             out.close();  
  73.   
  74.         }  
  75.         catch(Exception e){  
  76.             e.printStackTrace();  
  77.               
  78.             }  
  79.     }  
  80.     public static void main(String[] args)throws Exception  
  81.     {  
  82.         Class.forName("com.mysql.jdbc.Driver").newInstance();  
  83.         BlobTest test=new BlobTest();  
  84.         test.con=java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/demo","root","12345");  
  85.       test.createTable();  
  86.         test.addBlob();  
  87.         test.getBlob();  
  88.     }  
  89. }  

上一篇:j2se 6 的新特性   下一篇:URL中的特殊字符
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论
  热点文章
·java自学路线图
·JSP入门实例教程
·JUnit单元测试(2)
·JUnit单元测试(1)
·什么是Servlet
·用AJAX+J2EE实现网上会议室系统
·浅谈在Java语言中究竟是传值还是
·Java 5.0 多线程编程实践
·Java的文件 读和写
·构造器内部的多态方法的行为
·JSP常用内置对象使用说明
·对于JAVA基础测试中常见的异常问
  相关文章
·j2se 6 的新特性
·使用二进制方法保存图片
·Java DBJava 6 里的数据库
·mysql操作blob经验谈
·JAVA写的MP3标签读写器
·解析MySQL数据类型 避免数据库设
·如何使用Static的变量和方法
·如何使用java向mysql存取二进制
·JAVA如何调用DOS命令
·JDOM入门
·学习装饰者模式(Decorator)
· 如何通过java或jsp向数据库存取
  相关信息
copy right @ 百家拳软件项目研究室 2007 辽ICP备07011763