zoukankan      html  css  js  c++  java
  • 处理BLOB

    处理BLOB

    oracle LOB

    LOB(Large Objects大对象),用来存储大量的二进制和文本数据的一种数据类型

    内部LOB

        BLOB(二进制数据)

        CLOB(单字节字母数据)

        NCLOB(多字节字符数据)

    外部LOB

    插入BLOB类型的数据必须使用PreparedStatement。

    因为BLOB类型的数据时无法使用字符串拼写的。

    调用setBlob(int index,InputStream inputStream)

     @Test
        public void test4(){
            Connection conn = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            String sql = "insert into student values(?,?,?,?)";
    
            try {
                conn = Methods.getConnection();
    
              
                preparedStatement = conn.prepareStatement(sql);
    
                preparedStatement.setObject(1,"5");
                preparedStatement.setString(2,"yang5");
                preparedStatement.setString(3,"1235");
                InputStream inputStream = new FileInputStream("boy.png");
                preparedStatement.setBlob(4,inputStream);
                preparedStatement.executeUpdate();
            inputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { Methods.release(preparedStatement,conn,resultSet); } }

      

    读取blob数据:

    1.使用getBlob方法读取Blob对象

    2.调用Blob的getBinaryStream()方法得到输入流,在使用IO操作即可。

    @Test
        public void test5(){
            Connection conn = null;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            String sql = "select picture from student where id =5";
    
            try {
                conn = Methods.getConnection();
    
    
                preparedStatement = conn.prepareStatement(sql);
    
                resultSet = preparedStatement.executeQuery();
    
                if(resultSet.next()){
                    Blob picture = resultSet.getBlob(1);
                    InputStream in = picture.getBinaryStream();
                    OutputStream os = new FileOutputStream("boy1.png");
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while((len= in.read(buffer))!=-1){
                        os.write(buffer,0,len);
                    }
                    os.close();
                    in.close();
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                Methods.release(preparedStatement,conn,resultSet);
            }
        }
    

      

  • 相关阅读:
    Python之路-(js正则表达式、前端页面的模板套用、Django基础)
    Python之路-jQuery
    单例模式实际应用
    hibernate联合主键注解配置
    spring:ContextLoaderListener接口
    Struts2 校验
    Control character in cookie value, consider BASE64 encoding your value-Cookie保存中文出错[转]
    基于Cookie跨域的单点登录问题
    Spring:启动项目时加载数据库数据(总结)
    Oracle表分区[转]
  • 原文地址:https://www.cnblogs.com/yangHS/p/10832890.html
Copyright © 2011-2022 走看看