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

RE: Save java objects using JDBC

From: Peter Mount <petermount(at)maidstone(dot)gov(dot)uk>
To: "'Rob Judd'" <rjudd(at)mlug(dot)missouri(dot)edu>, "pgsql-interfaces(at)postgreSQL(dot)org" <pgsql-interfaces(at)postgresql(dot)org>
Subject: RE: Save java objects using JDBC
Date: 2000-10-17 09:55:43
Message-ID: 1B3D5E532D18D311861A00600865478CF1B436@exchange1.nt.maidstone.gov.uk (view raw or flat)
Thread:
Lists: pgsql-interfaces
No, when you forget to setAutoCommit(false) you get a different exception.

This is a protocol error. What version of both the Backend, JDBC Driver and
JDK are you using?

Peter

-- 
Peter Mount
Enterprise Support Officer, Maidstone Borough Council
Email: petermount(at)maidstone(dot)gov(dot)uk
WWW: http://www.maidstone.gov.uk
All views expressed within this email are not the views of Maidstone Borough
Council


-----Original Message-----
From: Rob Judd [mailto:rjudd(at)mlug(dot)missouri(dot)edu]
Sent: Tuesday, October 17, 2000 2:57 AM
To: pgsql-interfaces(at)postgreSQL(dot)org
Subject: [INTERFACES] Save java objects using JDBC


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

	

	
    }
    
}

Responses

pgsql-interfaces by date

Next:From: Peter MountDate: 2000-10-17 10:05:08
Subject: RE: building pgsql-interfaces...
Previous:From: Peter MountDate: 2000-10-17 09:53:23
Subject: RE: problem with driver

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