pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
SQLData user-defined-types and getObject()
2+ messages / 2 participants
[nested] [flat]

* SQLData user-defined-types and getObject()
@ 2006-01-08 03:28 Assad Jarrahian <jarraa@gmail.com>
  2006-01-08 08:17 ` Re: [JDBC] SQLData user-defined-types and getObject() Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Assad Jarrahian @ 2006-01-08 03:28 UTC (permalink / raw)
  To: pgsql-general@postgresql.org; pgsql-jdbc

Hi All,
  I am quite confused (PLEASE PLEASE Help), I cannot find anything on
the web). I read that you can declare a class that implements SQLData
(in this case I set up a class called Complex from the /src/tutorial
datatype that mimics the user-defined datatype in the db) and then set
the mapping appropriately (see below).
   But it does not work ( I maybe doing something wrong)!!!
The "getObject" code throws:
Exception in thread "main" org.postgresql.util.PSQLException:
Unsupported Types value: 2,000
and the setObject code throws:
Exception in thread "main" java.lang.ClassCastException: java.lang.Class
 (points to the getObject() line

ANY help would be much appreciated!
much thanks in advance.
-assad

Reference:
//Mapping type setup
		java.util.Map map = db.getTypeMap();
		if(map == null) map = new HashMap();
		map.put("complex", Class.forName("Complex"));
		db.setTypeMap(map);
		
		map = db.getTypeMap();

and then I can set and get as follows:

//set info
		Complex test = new Complex();
		test.a = 5.6;
		test.b = 3.4;
		Statement stmt = db.createStatement();
		PreparedStatement temp = db.prepareStatement("INSERT INTO
test_complex VALUES (DEFAULT, ?);");
		temp.setObject(1,test);
	temp.executeUpdate();

//I also  tried with setObject with the type specified to
java.sql.TYPE_JavaObject

//get info

	ResultSet rs = stmt.executeQuery("SELECT * FROM test_complex");
		System.out.println("Got ");
		
		while(rs.next()){
			System.out.println("Got ");
		ResultSetMetaData metaData = rs.getMetaData();
		System.out.println("Type from SQL: " +
		metaData.getColumnTypeName(2));

		Object foo = (Object) rs.getObject(2, map);

		if (foo instanceof Complex) {
		Complex cp = (Complex)foo;
		System.out.println("Got: " + cp + " from DB");



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [JDBC] SQLData user-defined-types and getObject()
  2006-01-08 03:28 SQLData user-defined-types and getObject() Assad Jarrahian <jarraa@gmail.com>
@ 2006-01-08 08:17 ` Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2006-01-08 08:17 UTC (permalink / raw)
  To: Assad Jarrahian <jarraa@gmail.com>; +Cc: pgsql-general@postgresql.org; pgsql-jdbc



On Sat, 7 Jan 2006, Assad Jarrahian wrote:

>  I am quite confused (PLEASE PLEASE Help), I cannot find anything on
> the web). I read that you can declare a class that implements SQLData
> (in this case I set up a class called Complex from the /src/tutorial
> datatype that mimics the user-defined datatype in the db) and then set
> the mapping appropriately (see below).

This is possible according to the JDBC spec, but the postgresql driver 
does not implement it.  At the moment your only option is to have your 
class implement org.postgresql.util.PGobject and register it using a pg 
specific method.  This is fine for a true user defined type, but it 
certainly isn't ideal for a composite type because it requires the type 
creator to handle all of the fields himself.

There isn't much documentation on PGobject implementations either, but the 
javadoc and source code will point you in the right direction.

http://jdbc.postgresql.org/documentation/publicapi/org/postgresql/util/PGobject.html

Kris Jurka



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2006-01-08 08:17 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-01-08 03:28 SQLData user-defined-types and getObject() Assad Jarrahian <jarraa@gmail.com>
2006-01-08 08:17 ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox