AbstractJdbc2Connection.java getObject(String type, String value)

From: "Berkes Alexander" <Alexander(dot)Berkes(at)oevp(dot)at>
To: <pgsql-jdbc(at)postgresql(dot)org>
Subject: AbstractJdbc2Connection.java getObject(String type, String value)
Date: 2007-09-18 12:33:55
Message-ID: 0E77F407253745429A06B25C5657FBD801887B58@moriz.lfg.oevp.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hello,

I am getting this Exception when trying to use ResultSet.getObject(int i):

--------------------------
java.lang.ClassCastException: java.lang.Class
at org.postgresql.jdbc2.AbstractJdbc2Connection.getObject(AbstractJdbc2Connection.java:366)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getObject(AbstractJdbc2ResultSet.java:2347)
at testing.UDTTest.<init>(UDTTest.java:57)
at testing.UDTTest.main(UDTTest.java:108)
--------------------------



I am using a table (tablename=person) with columns firstname and lastname.
As postgresql always creates a composite type when you create a table, I thought
about just writing a Person class witch implements the SQLData interface.



my Person.class:

-------------------------
package testing;

import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;

public class Person implements SQLData {
private String myType = "testing.Person";
private String firstname;
private String lastname;

public static Person parse(String input, String typeName) throws SQLException
{
try
{
StreamTokenizer tz = new StreamTokenizer(new StringReader(input));
if(tz.nextToken() == '('
&& tz.nextToken() == StreamTokenizer.TT_WORD)
{
String firstname = tz.sval;
if(tz.nextToken() == ','
&& tz.nextToken() == StreamTokenizer.TT_WORD)
{
String lastname = tz.sval;
if(tz.nextToken() == ')')
{
return new Person(firstname, lastname);
}
}
}
throw new SQLException("Unable to parse person from string \"" + input + '"');
}
catch(IOException e)
{
throw new SQLException(e.getMessage());
}
}

public Person (String firstname, String lastname){
this.firstname = firstname;
this.lastname = lastname;
}


public String getSQLTypeName() throws SQLException {
return myType;
}

public void readSQL(SQLInput stream, String typeName) throws SQLException {
this.myType = typeName;
firstname = stream.readString();
lastname = stream.readString();
}

public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(firstname);
stream.writeString(lastname);
}

public String toString(){
StringBuffer sb = new StringBuffer();
sb.append('(');
sb.append(firstname);
sb.append(',');
sb.append(lastname);
sb.append(')');

return sb.toString();
}

public String getFirstname(){ return this.firstname; }
public String getLastname(){ return this.lastname; }
}
---------------------------------------



In the main class I set the new type in the typemap:

--------------------------------
Connection con = .....
.
.
.
.

Map<String, Class<?>> typeMap = con.getTypeMap();

if(typeMap == null){
typeMap = new HashMap<String, Class<?>>();
System.out.println("Created new typeMap");
}

typeMap.put("person", Class.forName("testing.Person"));
con.setTypeMap(typeMap);
.
.
.
ResultSet rs = con.executeQuery("blah");

while(rs.next()){
Person p = (Person) rs.getObject(1);
}

------------------------------



After I played a while I downloaded the postgresql-jdbc source code to look at the class AbstractJdbc2Connection.
I discovered the part where the error accures (look at the bold line):

-------------------------
public Object getObject(String type, String value) throws SQLException
{
if (typemap != null)
{
SQLData d = (SQLData) typemap.get(type);
if (d != null)
{
// Handle the type (requires SQLInput & SQLOutput classes to be implemented)
if (Driver.logDebug)
Driver.debug("getObject(String,String) with custom typemap");
throw org.postgresql.Driver.notImplemented(this.getClass(), "getObject(String,String)");
}
}
.
.
.
.
-------------------------



As we can see the cast to SQLData is wrong, because the typeMap is of form: <String, Class<?>>
Threrefor this line should look like: SQLData d = (SQLData) ((Class)typemap.get(type)).newInstance();
I than recognized, that this feature is not implemented yet in the sourcecode because an Error is thrown
when the Object received is != null.



Could it be possible, that pgjdbc had this implemented correctly in a prior version?
I am not sure about that.....

Another question:
I would like to help developing the driver but was not able to integrate the pgjdbc source code into eclipse.
What tools/plugins etc. do I need to handle the Driver.java.in
classe correctly?


best regards,

________________________________
Alexander Berkes
Service & Mobilisierung
EDV und PDV

ÖVP Bundespartei
Lichtenfelsgasse 7, 1010 Wien

Tel.: +43 (1) 40126 512
Fax: +43 (1) 40126 519
mailto:Alexander(dot)Berkes(at)oevp(dot)at
________________________________
Mach' mit und sei dabei:
http://www.perspektiven2010.at
<http://www.perspektiven2010.at/>

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Eric Faulhaber 2007-09-18 15:44:55 Re: Batch INSERT failing with error 22P02
Previous Message Stephane Bailliez 2007-09-18 11:03:08 Re: Exception while doing ResultSetMetadata.getColumnName() after select setval()