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

示例代码如下: byte[] byteArray= ...; InputStream inputStream = new ByteArrayInputStream(byteArray); Blob blob = new SerialBlob(byteArray); 要注意的是在使用SerialBlob时,需要引入j... String s1="1231dsdgasd的飒飒大"; Clob c = new SerialClob(s1.toCharArray());//String clob Blob b = new SerialBlob(s1.getBytes("GBK"));//String blob //也可以这样不传字符集名称,默认使用系统的 //Blob b = new SerialBlob(s1. 摘要:下文讲述JavaBlob类型同String类型之间相互换的方法说明,如下所示;Blob类型:是一个二进制类型,常用于存储大文本,图像 等等二进制数据,在网页开发中我们经常遇到这样的需求,需将富文本字符串换为Blob数据类型,然后存储到运算服务器上,那么java中如何将富文本换为Blob数据类型呢?下文将一一道来,如下所示:实现思路:字符串Blobnew SerialBlob("字符... BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。 在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型。 BLOB是一个大文件,典型的BLOB是一张图片或一个声音文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传、下载或者存放到一个数据库)。 根据Eric Raymond的说法,处理BLOB的主要思想就是让文件处理器(如数据 展开全部这个是mysql下存取blob字段的一个很简单的类,跟据自己的需要改改就行了/*** Title: BlobPros.java* Project: test* Description: 把图片e69da5e887aa3231313335323631343130323136353331333332623432存入mysql中的blob字段,并取出* Call M... 1,String插入到BLOB类型字段,(这里的字符串以生成的XML为例): String XML = document.asXML(); //使用dom4j写成的xml是String类型,记得string类型不能直接插入到BLOB类型 byte[] xmlbyte = XML.getBytes("GBK"); //这里将String为字符数组。 JdbcTemplate jdbcTem... 最开始不知道存储String类型到Clob类型的时候,感觉特别麻烦,网上翻了很多资料,有参考价值的不多。 数据库存储的字段的类型为Clob类型,要将String类型存储到Clob类型需要一定的换,要先将String类型换成流的方式,然后再将流存储即可,原来也不难 String description = "存储String类型到Clob类型!"; StringReader strRead... import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.sql.Blob;import java.sq... I'm trying to get string from BLOB datatype by usingBlob blob = rs.getBlob(cloumnName[i]);byte[] bdata = blob.getBytes(1, (int) blob.length());String s = new String(bdata);It is working fine but when ... 我试图在一个blob列中编写和更新一个pdf文档,但是我只能更新blob只写入比以前存储的数据更多的数据.如果我尝试用较小的文档数据更新blob列,我只得到一个损坏的pdf.首先使用empty_blob()函数初始化blob列.我在下面写了示例Java类来测试这个行为.我第一次使用’true’作为main方法的第一个参数运行它,所以在第一行存储了大约31kB的文档,在第二行有一个278kB的文档....