如何创建BLOB对象? [英] How to create BLOB object?
本文介绍了如何创建BLOB对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 如何用Java创建BLOB对象?
- 如何从数据库设置BLOB值?
- 如何在数据库中设置BLOB值?
我已经这样创建了BLOB对象:
I have create the BLOB object like this:
byte [] fileId = b.toByteArray();
Blob blob = new SerialBlob(fileId);
但这给我一个错误.
推荐答案
-
要创建BLOB,请使用
Connection.createBlob
使用PreparedStatement.setBlob
要从DB读取BLOB,请使用ResultSet.getBlob
to read BLOB from DB use ResultSet.getBlob
假设您具有BLOB列b1
的表t1
:
Assuming you have table t1
with BLOB column b1
:
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Blob b1 = conn.createBlob();
b1.setBytes(1, new byte[10]); // first position is 1. Otherwise you get: Value of offset/position/start should be in the range [1, len] where len is length of Large Object[LOB]
PreparedStatement ps = conn.prepareStatement("update t1 set c1 = ?");
ps.setBlob(1, b1);
ps.executeUpdate();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select c1 from t1");
Blob b2 = rs.getBlob(1);
这篇关于如何创建BLOB对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文