简体   繁体   English

字节数组到Java中的Blob

[英]Byte array to blob in java

I have a database in mysql. 我在mysql中有一个数据库。 In this I have a table which is storing image in blob format. 在这里,我有一个表,以Blob格式存储图像。 From my Java program I want to insert a new image into the table. 我想从Java程序中将新图像插入表中。 But When I tried to convert a byte[] to blob using 但是当我尝试使用以下方法将byte[]转换为blob时

java.sql.Blob blob=null;
blob.setBytes(1, myByte );

I am getting null pointer exception. 我收到空指针异常。

Again tried by converting: 再次尝试通过转换:

 Blob blob = Hibernate.createBlob(bytes);

But here also got exception wrong format. 但是这里也有异常错误的格式。

if you use this code: 如果使用此代码:

java.sql.Blob blob=null;
blob.setBytes(1, myByte );

blob is null. blob为空。 You have to create a new Blob Object like: 您必须创建一个新的Blob对象,例如:

Blob blob = new SerialBlob(myByte );

and then you can store it. 然后可以存储它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM