JDBC实现文件、图片的存储实例

  1. /*  
  2. 实现的功能:  
  3.     用数据库存储文本数据,并且读取出来放在当前项目里  
  4. 分析:  
  5.     难度不是很大,关键是掌握文件流,数据库的操作不是很多,但是文件流的读写比较多  
  6.   
  7. 日期:20131003  
  8.   
  9. 作者:烟大阳仔  
  10.   
  11. */  
  12.   
  13. public class Ckb_test {  
  14.   
  15.     public static void main(String[] args) throws SQLException, IOException {  
  16.         // TODO Auto-generated method stub  
  17.         read();  
  18.     }  
  19.     static void create() throws SQLException, IOException  
  20.     {  
  21.         Connection conn=null;  
  22.         PreparedStatement prest=null;  
  23.         ResultSet resultset=null;  
  24.           
  25.         try {  
  26.             //2.建立连接  
  27.             conn=JdbcUtils.getConnection();  
  28.             //单例设计模式  
  29.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  30.             //3.创建语句  
  31.             String sql="insert into ckb_test(text) values(?)";  
  32.             prest=conn.prepareStatement(sql);  
  33.             File file=new File("src/cn/com/JDBC/JdbcUtils.java");  
  34.             Reader reader=new BufferedReader(new FileReader(file));  
  35.             prest.setCharacterStream(1, reader, (int)file.length());  
  36.             //4.执行语句  
  37.               
  38.             int i=prest.executeUpdate();  
  39.             reader.close();  
  40.             System.out.println("i="+i);  
  41.         } finally  
  42.         {  
  43.             JdbcUtils.free(resultset, prest, conn);  
  44.         }  
  45.     }  
  46.     static void read() throws SQLException, IOException  
  47.     {  
  48.         Connection conn=null;  
  49.         Statement st=null;  
  50.         ResultSet resultset=null;  
  51.           
  52.         try {  
  53.             //2.建立连接  
  54.             conn=JdbcUtils.getConnection();  
  55.             //单例设计模式  
  56.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  57.             //3.创建语句  
  58.             st=conn.createStatement();  
  59.             //4.执行语句  
  60.             resultset=st.executeQuery("select text from ckb_test");  
  61.             //5.处理结果  
  62.             while(resultset.next())  
  63.             {  
  64.                 Clob clob=resultset.getClob(1);  
  65.                 Reader reader=clob.getCharacterStream();  
  66.                 //reader=resultset.getCharacterStream(1);  
  67.                 File file=new File("JdbcUtils.java");  
  68.                 Writer writer=new BufferedWriter(new FileWriter(file));  
  69.                 char[] buff=new char[1024];  
  70.                 for(int i=0;(i=reader.read(buff))>0;)  
  71.                 {  
  72.                     writer.write(buff,0,i);  
  73.                 }  
  74.                 writer.close();  
  75.                 reader.close();  
  76.             }  
  77.               
  78.         } finally  
  79.         {  
  80.             JdbcUtils.free(resultset, st, conn);  
  81.         }  
  82.     }  
  83.   
  84. }  
  85.   
  86. ----------------------------------------------------------------------------------------------------------  
  87. /*  
  88. 实现的功能:  
  89.     用数据库存储图片数据,并且读取出来放在当前项目里  
  90. 分析:  
  91.     难度不是很大,关键是掌握字节流,数据库的操作不是很多,但是文件流的读写比较多,注意更改图片的目录  
  92.   
  93. 日期:20131003  
  94.   
  95. 作者:烟大阳仔  
  96.   
  97. */  
  98.   
  99. public class PictureBlob {  
  100.   
  101.     public static void main(String[] args) throws SQLException, IOException {  
  102.         read();  
  103.     }  
  104.     static void create() throws SQLException, IOException  
  105.     {  
  106.         Connection conn=null;  
  107.         PreparedStatement prest=null;  
  108.         ResultSet resultset=null;  
  109.           
  110.         try {  
  111.             //2.建立连接  
  112.             conn=JdbcUtils.getConnection();  
  113.             //单例设计模式  
  114.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  115.             //3.创建语句  
  116.             String sql="insert into blob_test(big_bit) values(?)";  
  117.             prest=conn.prepareStatement(sql);  
  118.             File file=new File("C:\\Documents and Settings\\Administrator\\My Documents\\My Pictures\\cxg.jpg");  
  119.             InputStream in=new BufferedInputStream(new FileInputStream(file));  
  120.             prest.setBinaryStream(1, in, (int)file.length());  
  121.             //4.执行语句  
  122.               
  123.             int i=prest.executeUpdate();  
  124.             in.close();  
  125.             System.out.println("i="+i);  
  126.         } finally  
  127.         {  
  128.             JdbcUtils.free(resultset, prest, conn);  
  129.         }  
  130.           
  131.     }  
  132.       
  133.     static void read() throws SQLException, IOException  
  134.     {  
  135.         Connection conn=null;  
  136.         Statement st=null;  
  137.         ResultSet resultset=null;  
  138.           
  139.         try {  
  140.             //2.建立连接  
  141.             conn=JdbcUtils.getConnection();  
  142.             //单例设计模式  
  143.             conn=JdbcUtilsSingle.getInstance().getConnection();  
  144.             //3.创建语句  
  145.             st=conn.createStatement();  
  146.             //4.执行语句  
  147.             resultset=st.executeQuery("select big_bit from blob_test");  
  148.             //5.处理结果  
  149.             while(resultset.next())  
  150.             {  
  151.                 Blob blob=resultset.getBlob(1);  
  152.                 InputStream in=blob.getBinaryStream();  
  153.                 //reader=resultset.getCharacterStream(1);  
  154.                 File file=new File("1.jpeg");  
  155.                 OutputStream out=new BufferedOutputStream(new FileOutputStream(file));  
  156.                 byte[] buff=new byte[1024];  
  157.                 for(int i=0;(i=in.read(buff))>0;)  
  158.                 {  
  159.                     out.write(buff,0,i);  
  160.                 }  
  161.                 out.close();  
  162.                 in.close();  
  163.             }  
  164.               
  165.         } finally  
  166.         {  
  167.             JdbcUtils.free(resultset, st, conn);  
  168.         }  
  169.     }  
  170. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值