Java中可以使用类将byte数组转换成Blob对象。
示例代码如下:
byte[] byteArray= ...;
InputStream inputStream = new ByteArrayInputStream(byteArray);
Blob blob = new SerialBlob(byteArray);
要注意的是在使用SerialBlob时,需要引入这个包。
另外还有一种方法就是使用PreparedStatement来实现:
PreparedStatementstatement = connection.prepareStatement("insert into table_name (column_name) values (?)");
statement.setBlob(1, new ByteArrayInputStream(byteArray));
statement.execute();
建议使用PreparedStatement来设置Blob,因为它更加安全,能够防止SQL注入攻击。