且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在java中将Blob转换为字符串和将字符串转换为Blob

更新时间:2023-01-06 08:31:40

试试这个(a2是BLOB col)

try this (a2 is BLOB col)

PreparedStatement ps1 = conn.prepareStatement("update t1 set a2=? where id=1");
Blob blob = conn.createBlob();
blob.setBytes(1, str.getBytes());
ps1.setBlob(1, blob);
ps1.executeUpdate();

即使没有BLOB也可以工作,驱动程序会自动转换类型:

it may work even without BLOB, driver will transform types automatically:

   ps1.setBytes(1, str.getBytes);
   ps1.setString(1, str);

此外,如果您使用文本,CLOB 似乎是一种更自然的 col 类型

Besides if you work with text CLOB seems to be a more natural col type