Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
Thread:
Lists: pgsql-hackerspgsql-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

pgsql-hackers by date

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

pgsql-interfaces by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group