I'm sorry to bother the list with this question - I know people are
always asking it. I thought I understood how to do this, but my code
doesn't work.
I am trying to save a java object in a database using the setBytes()
method of PreparedStatement - I don't want to use the large object manager
because I want this to be somewhat portable.
The code below gives me the error:
Exception: FastPath protocol error: Z :: FastPath protocol error: Z
As soon as I call setBytes()
I thought this only came when you forget to call "setAutoCommit(false)".
Can anyone please tell me what I'm doing wrong.
Thanks a lot, Rob
-------------------- PSQLTest.java -----------------------
import java.sql.* ;
import java.io.* ;
import java.util.* ;
public class PSQLTest { public PSQLTest() { } public static void main(String[] args) {
Vector vec = new Vector() ;for (int i=0; i<10; ++i) vec.addElement(new Integer(i+5)) ;
Connection conn = null ;try { Class.forName("postgresql.Driver") ; conn = DriverManager.getConnection
("jdbc:postgresql://127.0.0.1:5432/rob","rob", "") ; conn.setAutoCommit(false);
byte[] bytes ; ByteArrayOutputStream out = new ByteArrayOutputStream() ; ObjectOutputStream objOut = new
ObjectOutputStream(out); objOut.writeObject(vec) ; objOut.flush() ; bytes = out.toByteArray() ;
objOut.close();
PreparedStatement ps = conn.prepareStatement ("insert into vectors (name, id) values ( ?, ?)") ;
ps.setString(1, "Vector name") ; ps.setBytes(2, bytes) ; ps.executeUpdate() ; ps.close() ;
conn.commit() ;} catch (Exception e) { System.out.println("Exception: "+e+" :: "+e.getMessage());
e.printStackTrace();}
}
}