Re: Create java.sql.Blob from byte[]
From: Sebastian Scheid (mynewsgroup_at_web.de)
Date: 10/16/04
- Previous message: Kingsley Idehen: "Re: Unable to load JdbcOdbc library"
- In reply to: Shay: "Create java.sql.Blob from byte[]"
- Next in thread: Chuck Simpson: "Re: Create java.sql.Blob from byte[]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 16 Oct 2004 12:12:43 +0200
"Shay" <shayn@idit.co.il> schrieb im Newsbeitrag
news:b2d1425c.0410140630.94e0b2@posting.google.com...
> Hello All
>
> How can I create Blob from byte array?
> I use jdk1.3.1_06.
> I think that I must create a new class implements the Blob interface
> but I'm not sure.
I found this in comp.databases.ibm-db2:
HOW TO INSERT A BLOB IN A DB USING JAVA
"normal" JDBC
-------------
1.) byte []
byte [] data = ...
String query = "UPDATE tableWithBlob SET blobAtt = ? WHERE idAtt = " +
id ;
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setBinaryStream(1,new ByteArrayInputStream(data),data.length);
pstmt.execute();
pstmt.close();
2.) Object
Object data = ... (Serializable)
String query = "UPDATE tableWithBlob SET blobAtt = ? WHERE idAtt = " +
id ;
PreparedStatement pstmt = connection.prepareStatement(query);
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(b);
out.writeObject(data);
byte [] dataAsByteArray = b.toByteArray();
pstmt.setBinaryStream(1,new
ByteArrayInputStream(dataAsByteArray),dataAsByteArray.length);
pstmt.execute();
pstmt.close();
Instead of
pstmt.setBinaryStream(1,new
ByteArrayInputStream(dataAsByteArray),dataAsByteArray.length);
you can use
pstmt.setBytes(1, dataAsByteArray);
but this method seems to have a limit of 33124 bytes!
Proptietary solution for Oracle: oracle.sql.BLOB needed
-------------------------------------------------------
1.) byte []
byte [] data = ...
Statement stmt = connection.createStatement();
String query = "SELECT blobAtt FROM tableWithBlob WHERE idAtt = " + id +
" FOR UPDATE";
ResultSet rs = stmt.executeQuery(query);
rs.next();
oracle.sql.BLOB blob = (BLOB)rs.getBlob("id");
OutputStream outstream = blob.getBinaryOutputStream();
outstream.write(data);
outstream.flush();
outstream.close();
2.) Object
Object data = ... (Serializable)
BLOB blob = find the blob like above;
OutputStream outstream = blob.getBinaryOutputStream(); //to write the
blob
ObjectOutputStream out = new ObjectOutputStream(outstream); //to
serialize the object
out.writeObject(data);
out.flush();
out.close();
- Previous message: Kingsley Idehen: "Re: Unable to load JdbcOdbc library"
- In reply to: Shay: "Create java.sql.Blob from byte[]"
- Next in thread: Chuck Simpson: "Re: Create java.sql.Blob from byte[]"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]