星空网 > 软件开发 > 数据库

使用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  ();
              }

          }
        
     }
    
}








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

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

亚马逊广告工具的意义,它能帮助运营的地方:https://www.kjdsnews.com/a/1660059.html
外贸营销ROI翻倍:视频内容优化的关键点”:https://www.kjdsnews.com/a/1660060.html
速卖通选品建议:看10月热销榜TOP10,黑五爆品其实有迹可循!:https://www.kjdsnews.com/a/1660061.html
外观专利侵权预警第八十八期:屏风(家居好物):https://www.kjdsnews.com/a/1660062.html
Shein扩大外国高管招募,推进领导层全球化:https://www.kjdsnews.com/a/1660063.html
冬季系列丨5款插座暖风机加热器 欧美专利都有!:https://www.kjdsnews.com/a/1660064.html
下周开始,安徽人免费游九华山,带上身份证就:https://www.vstour.cn/a/408234.html
上海滑雪场门票价格?:https://www.vstour.cn/a/408235.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流