Save java objects using JDBC

From: Rob Judd <rjudd(at)mlug(dot)missouri(dot)edu>
To: "pgsql-interfaces(at)postgreSQL(dot)org" <pgsql-interfaces(at)postgresql(dot)org>
Subject: Save java objects using JDBC
Date: 2000-10-17 01:57:02
Message-ID: Pine.LNX.4.21.0010162046450.1933-100000@mlug.missouri.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-interfaces

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() ;
}


}

}

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Philip Warner 2000-10-17 01:59:08 RE: AW: Backup, restore & pg_dump
Previous Message Philip Warner 2000-10-17 01:56:04 Re: AW: Backup, restore & pg_dump

Browse pgsql-interfaces by date

  From Date Subject
Next Message Mark Dzmura 2000-10-17 06:14:07 building pgsql-interfaces...
Previous Message Joseph Shraibman 2000-10-17 00:33:36 Re: 2 computers 1hd 2 postgres daemons. Is it possible?