Click to See Complete Forum and Search --> : How to insert Java object into SQL Server using JDBC statement
anil_001
08-27-2009, 11:35 PM
Hi,
Could any one help me how to insert Java object into SQL Server database using simple JDBC Statement(not using prepared statement).
The database column type is image.
Thanks in advance
Kuriyama
08-28-2009, 09:30 AM
Hi,
Could any one help me how to insert Java object into SQL Server database using simple JDBC Statement(not using prepared statement).
The database column type is image.
Thanks in advance
Why would you NOT want to use prepared statements? They prevent SQL injection attacks and make sure that you don't data type conflicts.
You can write Java objects into SQL, but you'll have to do a couple of things. First make sure that your object implements serializable. I believe what you'll need to do after this is convert your serialized object into binary and write that binary string into SQL.
I would highly recommend that you use prepared statements for this. It takes a lot of the work out of writing objects into a database, namely the conversion into binary.
anil_001
08-31-2009, 12:54 PM
Thank kuriyama for the response.
Currently we are using prep statement, there was issue using prep stmt, i don't what issue was, i was told to replace with simple stmt.
Even prepstmt we are doing all the serializing and converting to byte stream and inserting to sqlserver.
I have inserted bytes, but while retreiving its throwing invalid stream exception i am trying using resultset.getBytes and converting using object using bytearraystream
could you please provide sample code for using stmt.
thanks
Kuriyama
08-31-2009, 05:08 PM
Thank kuriyama for the response.
Currently we are using prep statement, there was issue using prep stmt, i don't what issue was, i was told to replace with simple stmt.
Even prepstmt we are doing all the serializing and converting to byte stream and inserting to sqlserver.
I have inserted bytes, but while retreiving its throwing invalid stream exception i am trying using resultset.getBytes and converting using object using bytearraystream
could you please provide sample code for using stmt.
thanks
Please post some code if possible. Just the bit where you are pulling the data out of SQL.
anil_001
09-01-2009, 12:23 PM
Hi kujiyama,
please the code how i am trying using jdbc stmt
Code for insert object into SQL server
------------------------------------
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject( (UserObject) aUserSession.getObjectStream() );
*
*
byte[]* uBytes = baos.toByteArray();
*
Statement stmt = dbConn.createStatement();
*
// object data type image in SQL server
String sql = "insert into table(id, object) vlaues (1, '"+uBytes+"')";
*
stmt.executeUpdate(sql);
*
*
*
Code for Retreive object from SQL Server
-----------------------------------------
//I have tried below options
//InputStream is = new ByteArrayInputStream((rs.getString("ObjectStream")).getBytes());
//InputStream is = new ByteArrayInputStream(rs.getBytes("ObjectStream"));
*
InputStream is = rs.getBinaryStream( "ObjectStream" );
ObjectInputStream ois = new ObjectInputStream( is );
*
UserObject o = (UserObject) ois.readObject();
*
Kuriyama
09-01-2009, 01:40 PM
Well your code looks correct, so I'm not exactly sure what the problem is. It could be that the insertion of this data isn't working correctly. Try pulling the data out of MSSQL like this.
InputStream is = rs.getBlob("ObjectStream").getBinaryStream();
ObjectInputStream ois = new ObjectInputStream(is);
UserObject o = (UserObject) ois.readObject();
criterion9
09-01-2009, 02:41 PM
Could it be:
vlaues
Probably should be:
values