How to create blob objects in Java?
•
Java
1. How to create blob objects in Java?
I have created a blob object
byte [] fileId=b.toByteArray(); Blob blob=new SerialBlob(fileId);
But it gave me a mistake So ask anyone to help me Thank you in advance
Solution
1) Create blob using connection createBlob
2) Use Preparedstatement Setblob writes blob to DB
3) Use resultset Getblob reads blob from DB
Suppose your table t1 has a 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);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码