zoukankan      html  css  js  c++  java
  • 使用PreparedStatement向数据表中插入、修改、删除、获取Blob类型的数据

    使用PreparedStatement向数据表中插入、修改、删除、获取Blob类型的数据

    2014-09-07 20:17

    Blob介绍

    BLOB类型的字段用于存储二进制数据

    MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同。

    MySQL的四种BLOB类型
    类型       大小(单位:字节)
    TinyBlob 最大 255
    Blob 最大 65K
    MediumBlob 最大 16M
    LongBlob 最大 4G

    Oracle LOB介绍

        LOB,即Large Objects(大对象),是用来存储大量的二进制和文本数据的一种数据类型(一个LOB字段可存储可多达4GB的数据)。
    LOB 分为两种类型:内部LOB和外部LOB。

    内部LOB将数据以字节流的形式存储在数据库的内部。因而,内部LOB的许多操作都可以参与事务,也可以像处理普通数据一样对其进行备份和恢复操作。Oracle支持三种类型的内部LOB:
    1. BLOB(二进制数据)  
    2. CLOB(单字节字符数据) 
    3. NCLOB(多字节字符数据)。
    CLOB和NCLOB类型适用于存储超长的文本数据,BLOB字段适用于存储大量的二进制数据,如图像、视频、音频,文件等。
    目前只支持一种外部LOB类型,即BFILE类型。在数据库内,该类型仅存储数据在操作系统中的位置信息,而数据的实体以外部文件的形式存在于操作系统的文件系统中。因而,该类型所表示的数据是只读的,不参与事务。该类型可帮助用户管理大量的由外部程序访问的文件。


    程序示例:

    package  tan ;
    import  java .  io .*;
    import  java .  sql .*;
    import  java .  text . SimpleDateFormat ;
    import  org .  junit . Test ;
    public  class  TestJDBC  {
        

        //如何获取数据表中的 blob类型的变量

        @Test
         public  void  testBlob2 ()  {
            Connection conn  = null ;
            PreparedStatement ps  = null ;
            ResultSet rs  = null ;
            InputStream is  = null ;
            FileOutputStream fos  = null ;
              try  {
                conn  = JDBCUtils  . getConnection  ();
                String sql  = "select id,name,email,birth,photo from customers where id=?" ;
                ps  = conn  . prepareStatement  ( sql  );
                ps   .setInt  (1 , 16 );//获取id=16的数据
                rs  = ps  . executeQuery  ();
                  if ( rs . next ()){
                      int  id  = rs  . getInt  ( 1 );
                    String name  = rs  . getString  ( 2 );
                    String email  = rs  . getString  ( 3 );
                    Date birth  = rs  . getDate  ( 4 );
                    Blob photo   =rs  .getBlob  (5 );
                    is  = photo  . getBinaryStream  ();//利用输入流来读取数据库中的二进制文件
                    fos  = new  FileOutputStream  ( new  File ( "girl.png" ));//输出到本地
                    
                      byte  [] buf  = new  byte [  100 ];
                      int  len  = 0 ;
                      while (( len = is . read ( buf ))!=-){
                        fos  . write  ( buf  , 0 ,  len );
                      }
                    Customer cust  = new  Customer  ( id  ,  name  ,  email ,  birth );//将非blob类型封装成对象输出
                    System  . out  . println  ( cust  );
                  }
                
                
              }  catch  ( Exception e  )  {
                
              } finally {
                JDBCUtils  . close  ( rs  ,  ps  ,  conn  );//记得要关闭流
                  if ( fos  !=  null ){
                      try  {
                        fos  . close  ();
                      }  catch  ( IOException e  )  {
                        e  . printStackTrace  ();
                      }
                    
                  }
                  if ( is  !=  null ){
                      try  {
                        is  . close  ();
                      }  catch  ( IOException e  )  {
                          // TODO Auto-generated catch block
                        e  . printStackTrace  ();
                      }
                    
                  }
              }
         }
        

        // 修改数据表包含图片信息的数据

        @Test
         public  void  testBlob1 ()  {
            Connection conn  = null ;
            PreparedStatement ps  = null ;
            FileInputStream fis  = null ;
              try  {
                conn  =  JDBCUtils  . getConnection  ();
                String sql  =  "update customers set photo = ? where id = ?" ;
                ps  =  conn  . prepareStatement  ( sql  );
                  //  填充占位符
                fis  =  new  FileInputStream  ( new  File ( "1.png"  ));
                ps   .setBlob  (1 , fis );
                ps  . setInt  ( 2 ,  16  );
                ps   .execute  ();
                
              }  catch  ( Exception e  )  {
                e  . printStackTrace  ();
              } finally {
                JDBCUtils  . close  ( null ,  ps ,  conn );
                  try  {
                    fis  . close  ();
                  }  catch  ( IOException e  )  {
                    e  . printStackTrace  ();
                  }
              }
            
         }

        // 向数据表中插入一条包含图片信息的数据

        @Test
         public  void  testBlob ()  {
            Connection conn  = null ;
            PreparedStatement ps  = null ;
            FileInputStream fis  = null ;
              try  {
                conn  = JDBCUtils  . getConnection  ();
                String sql  = "insert into customers(name,email,birth,photo)values(?,?,?,?)" ;
                ps  = conn  . prepareStatement  ( sql  );
                ps  . setString  ( 1 ,  "  zhengqiang " );
                ps  . setString  ( 2 ,  "beipiao@123.com"  );
                  // 日期转换
                String date  = "1991-11-13" ;
                SimpleDateFormat sdf  = new  SimpleDateFormat ( "yyyy-MM-dd" );
                java  . util  . Date d  = sdf  . parse  ( date  );

                ps  . setDate  ( 3 ,  new  Date ( d . getTime ()));

                //利用文件输入流写入
                fis  = new  FileInputStream  ( new  File ( "66.jpg"  ));
                ps  . setBlob  ( 4 ,  fis );
                  // 执行预编译语句
                ps   .execute  ();
                
              }  catch  ( Exception e  )  {
                e  . printStackTrace  ();
              } finally {
                JDBCUtils  . close  ( null ,  ps ,  conn );
                 try  {
                    fis  . close  ();
                  }  catch  ( IOException e  )  {
                    e  . printStackTrace  ();
                  }
              }
            
         }
        
    }
  • 相关阅读:
    LeetCode Merge Two Sorted Lists 归并排序
    LeetCode Add Binary 两个二进制数相加
    LeetCode Climbing Stairs 爬楼梯
    034 Search for a Range 搜索范围
    033 Search in Rotated Sorted Array 搜索旋转排序数组
    032 Longest Valid Parentheses 最长有效括号
    031 Next Permutation 下一个排列
    030 Substring with Concatenation of All Words 与所有单词相关联的字串
    029 Divide Two Integers 两数相除
    028 Implement strStr() 实现 strStr()
  • 原文地址:https://www.cnblogs.com/CooderIsCool/p/4777479.html
Copyright © 2011-2022 走看看