什么java类型可以写入BLOB类型中

什么java类型可以写入BLOB类型中,第1张

public interface Blob

SQL BLOB 值在 JavaTM 编程语言中的表示形式(映射关系)。SQL BLOB 是内置类型,它将二进制大对象 (Binary Large Object) 存储为数据库表某一行中的一个列值。默认情况下,驱动程序使用 SQL locator(BLOB) 实现 Blob,这意味着 Blob 对象包含一个指向 SQL BLOB 数据的逻辑指针而不是数据本身。Blob 对象在它被创建的事务处理期间有效。

接口 ResultSet、CallableStatement 和 PreparedStatement 中的方法(如 getBlob 和 setBlob)允许编程人员访问 SQL BLOB 值。Blob 接口提供一些方法来获取 SQL BLOB (Binary Large Object) 值的长度、在客户端实现 BLOB 值以及确定 BLOB 值中某一字节样本的位置。此外,此接口还有更新 BLOB 值的方法。

如果 JDBC 驱动程序支持该数据类型,则必须完全实现 Blob 接口的所有方法。

希望对你又帮助

使用jdk中的方法进行传输。在ResultSet 中有getBlob()方法,在PreparedStatement中有setBlob()方法,所以大多数人都会尝试setBlob。

js没有所谓byte,只有一个ArrayBuffer,而且ArrayBuffer又不能通过>

SLICE方法:

Blob对象的slice方法使用三个参数,均为可选参数,如果三个参数均省略时,相当于把一个Blob对象中的原始二进制数据原样复制到一个新建的Blob对象(即slice方法的返回值)中。

Blob对象的slice方法的第一个参数start的参数值为一个整数值,代表起始复制位置在Blob对象所代表的原始二进制数据中的位置,当start参数值为0时代表从该数据的起始位置(即第一个字节)开始复制数据;如果start参数值为负数值且Blob对象的size属性值+start参数值大于等于0。

则起始复制位置为Blob对象的size属性值+start参数值;如果start参数值为负数值且Blob对象的size属性值+start参数值小于0,则起始复制位置为Blob对象所代表的原始二进制数据的起始位置;如果start参数值为正数值且大于等于Blob对象的size属性值。

这是我以前写的代码,放在action里。在pojo类中对应为byte[]类型,clxxb是一个pojo类,clxxbgetClpic()得到对应的字节数组byte[]。其实输出文件就是输出一个字节流。希望对你有帮助。

InputStream input=clxxbgetClpic()getBinaryStream();

byte[] buffer=new byte[inputavailable()];

ServletOutputStream out=responsegetOutputStream();

int length=0;

while((length=inputread(buffer))!=-1){

outwrite(buffer,0,length);

}

outflush();

outclose();

//配置数据库连接驱动

String sql = xxxxxxxx;//要查询的sql

PreparedStatement ps = connprepareStatement(sql);

String path = xxxxxxx;

ResultSet rs = psexecuteQuery();

while (rsnext()) {

InputStream is = rsgetBlob(x)getBinaryStream();//x为要取的BLOB位置

FileOutputStream os = new FileOutputStream(path + "//"

+ "存放的文件名"+“zip”);

byte[] buff = new byte[1024];

while ((isread(buff)) != -1) {

oswrite(buff);

}

osclose();

isclose();

}

psclose();

connclose();

// 下面为一个完整的例子,如果用framework,需要做一定修改

写入    

public boolean saveWordFile(String filePath) {

        File file = new 

File(filePath);

        Connection conn = getConnection();

        try 

{

            javasqlStatement st = conncreateStatement();

            

connsetAutoCommit(false);

            stexecute("insert into table_name 

values(1,empty_blob())");

            ResultSet rs = 

st

                    executeQuery("select id,word from table_name where 

id=1 for update");

            if (rsnext()) {

                BLOB blob 

= (BLOB) rsgetBlob("word");

                OutputStream outStream = 

blobgetBinaryOutputStream();

                InputStream fin = new 

FileInputStream(file);

                byte[] b = new 

byte[blobgetBufferSize()];

                int len = 0;

                

while ((len = finread(b)) != -1) {

                    outStreamwrite(b, 0, 

len);

                }

                finclose();

                

outStreamflush();

                outStreamclose();

                

conncommit();

                connclose();

            }

        } 

catch (SQLException e) {

            // TODO Auto-generated catch 

block

            eprintStackTrace();

        } catch 

(FileNotFoundException e) {

            // TODO Auto-generated catch 

block

            eprintStackTrace();

        } catch (IOException e) 

{

            // TODO Auto-generated catch block

            

eprintStackTrace();

        }

        return true;

    }

读取

   public void getWordFile(String id) {

        Connection conn = 

getConnection();

        javasqlStatement st;

        try 

{

            st = conncreateStatement();

            ResultSet rs = 

stexecuteQuery("select id,word from table_name where 

id='"+id+"'");

            if (rsnext()) {

                BLOB blob = 

(BLOB) rsgetBlob("word");

                File file = new 

File("c://filenamedoc");

                FileOutputStream output = new 

FileOutputStream(file);

                InputStream input = 

blobgetBinaryStream();

                byte[] buffer = new 

byte[1024];

                int i = 0;

                while ((i = 

inputread(buffer)) != -1) {

                    outputwrite(buffer, 0, 

i);

                }

            }

        } catch (Exception e) 

{

            // TODO Auto-generated catch block

            

eprintStackTrace();

        }

   }

修改

public void updateblob(String id){

  Connection 

conn=getConnection();

  Statement stem=null;

  ResultSet rs=null;

  

try{ 

         connsetAutoCommit(false); 

         

stem=conncreateStatement();

         rs = stemexecuteQuery("select word 

from table_name where id='"+id+"' for update"); 

         

         if 

(rsnext()) { 

             BLOB blob = (BLOB) rsgetBlob("word"); 

             OutputStream outStream = blobgetBinaryOutputStream(); 

             InputStream fin = new FileInputStream("c://2doc"); 

             byte[] b = new byte[blobgetBufferSize()]; 

             int 

len = 0; 

             while ((len = finread(b)) != -1) { 

                 outStreamwrite(b, 0, len); 

             } 

             finclose(); 

             outStreamflush(); 

             outStreamclose(); 

             conncommit(); 

             connclose(); 

         } 

} catch (Exception ex){ 

 try {

  connrollback();

 } catch (SQLException e) {

  // TODO 

Auto-generated catch block

  eprintStackTrace();

 }

  

Systemoutprintln(exgetMessage());

}

 }

呵呵,这个是没有办法接受哦。

requestsetCharacterEncoding("UTF-8");

responsesetContentType("image/jpeg");

responsegetOutputStream()write(bytes);

这几个参数是把这个直接发给浏览器,说白了就直接把当成文件让浏览器下载。

解决办法:

<img border=0 src="localhost:8080/Pic" />

localhost:8080是你配置WEBXML

Pic是你的Servlet类名。

以上就是关于什么java类型可以写入BLOB类型中全部的内容,包括:什么java类型可以写入BLOB类型中、后端如何返回blob对象、java数据库blob字段的下载(读取)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9619865.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-30
下一篇2023-04-30

发表评论

登录后才能评论

评论列表(0条)

    保存