java代码实现String转Blob,并存入数据库Blob字段

前提回顾:

由于业务需求,需要把一些数据保存为blob数据,例如报文、文本、图片等。


思路敲门:

Question:
1、可以通过sql语句直接把string类型转换blob存入数据库吗?
2、可以通过直接读取文件,把文件里面的内容存入blob字段吗?
3、可以通过java代码,把string类型转换blob存入数据库吗?

解决方法:

1、可以通过rawtohex(’’)函数,但是不能插入数据超度过长的数据,不推荐

用法:

insert into act_ge_bytearray values('58',1,'ewwe','56',rawtohex(''),0);

2、可以通过匿名函数,例如备份数据库脚本时,可以使用

用法

--如系统是windows
--把act_blob解压,比如windows系统E盘,
--创建文件存储目录,需与数据库服务器同一系统
create or replace directory IMAGES as 'E:\act_blob';

--如系统是 linux系统
--把act_blob解压,比如/opt/pic,
--创建文件存储目录,需与数据库服务器同一系统
create or replace directory IMAGES as '/opt/pic';

--在以下目录名必须大写
declare
	l_bfile bfile;
	l_blob blob;
begin
	update act_ge_bytearray set bytes_=empty_blob() where ID_='58'
	return bytes_ into l_blob;
	--读取图片文件对象
	l_bfile := bfilename('IMAGES','58.xml');  --目录必须大写
  --打开图片文件对象
	dbms_lob.open(l_bfile,dbms_lob.file_readonly);
  --把图片文件对象写入Blob数据中
	dbms_lob.loadfromfile(l_blob,l_bfile,dbms_lob.getlength(l_bfile));
	dbms_lob.close(l_bfile);
	commit;
end;
/


3、使用java代码
package com.test;

import com.sinosoft.batch.db.DBconn;
import oracle.sql.BLOB;

import java.io.OutputStream;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @program: XXXX
 * @Date: 2019/9/17 16:34
 * @Author: Mr.SU
 * @Description:
 */
public class TestStringToBlob {
    
    

    public static void main(String[] args) {
    
    

        // blob内存放的是字节数组
        // String 的getBytes方法获得该字符串的字节数组(注意编码),然后存入blob即可
        String infoStr = "blob";
        byte[] bytes = null;
        try {
    
    
            bytes = infoStr.getBytes("utf-8");
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }

        instertData(bytes);

    }

    public static void instertData(byte[] bytes) {
    
    
        // TODO Auto-generated method stub
        try {
    
    
            java.util.Date dateTim = new Date();
            SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
            String file_content = df1.format(dateTim);//日期转换为字符串 用于生成序列号

            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@localhost:1521:orcl";
            String username = "user";
            String password = "user";
            //数据库连接
//            Connection con = DBconn.getConnectionByDBname("jdbc_opension");
//            con.setAutoCommit(false);
            Connection con = DriverManager.getConnection(url, username,
                    password);
            con.setAutoCommit(false);
            String sql1 = "insert into csip_filecontent(pk_filecontent,filecontent) values("+"'"+file_content+"'"+",empty_blob())";
            Statement st = con.createStatement();
            boolean b2 = st.execute(sql1);

            String sql2 = "select filecontent from csip_filecontent where pk_filecontent = "+"'"+file_content+"'"+" for update";
            PreparedStatement ps = con.prepareStatement(sql2);
            ResultSet rs = ps.executeQuery();
            OutputStream outputStream = null;
            if(rs.next()){
    
    
                BLOB blob = (BLOB)rs.getBlob(1);
                outputStream = blob.getBinaryOutputStream();
                outputStream.write(bytes,0,bytes.length);
            }
            outputStream.flush();
            outputStream.close();
            con.commit();
            con.close();

        } catch (Exception e) {
    
    
            System.out.println(e.getCause());
        }
    }
}


author:su1573

猜你喜欢

转载自blog.csdn.net/su1573/article/details/100930267
今日推荐