problem with Oracle object type [message #399597] |
Thu, 23 April 2009 02:09 |
maciego
Messages: 11 Registered: January 2009
|
Junior Member |
|
|
Hi,
I want to pass Java object into Oracle object type. Problem is with String attributes. They are set to null values on Oracle database with iso-8859-2 encoding (with WE8MSWIN1252 encoding everything works ok).
My sample code:
public class Person implements SQLData, Serializable
{
private String name;
private String street;
private BigDecimal no;
private String sql_type;
public String getSQLTypeName() throws SQLException
{
return getSql_type();
}
public void readSQL( SQLInput stream, String typeName ) throws SQLException
{
setSql_type( typeName );
this.setName( stream.readString() );
this.setStreet( stream.readString() );
this.setNo( stream.readBigDecimal() );
}
public void writeSQL( SQLOutput stream ) throws SQLException
{
stream.writeString( getName() );
stream.writeString( getStreet());
stream.writeBigDecimal( getNo());
}
//getters and setters
}
public class Main
{
public static void main( String args[] ) throws Exception
{
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() );
Connection conn = DriverManager.getConnection( url + login + password);
Statement stmt = conn.createStatement();
try
{
stmt.execute( "drop table people" );
stmt.execute( "drop type PERSON FORCE" );
}
catch( SQLException e )
{
}
stmt.execute( "create type PERSON as object (name VARCHAR (30), street VARCHAR (30), num NUMBER)" );
stmt.execute( "create table people (empno NUMBER, empid PERSON)" );
ResultSet rs = stmt.executeQuery( "select * from people" );
rs.close();
Hashtable map = new Hashtable();
try
{
map.put( "PERSON", Class.forName( "objecttype.Person" ) );
}
catch( ClassNotFoundException ex )
{
}
conn.setTypeMap( map );
Person new_person = new Person();
new_person.setName( "name" );
new_person.setStreet( "street" );
new_person.setNo( new BigDecimal( 5 ) );
new_person.setSql_type( "PERSON" );
PreparedStatement ps = conn.prepareStatement( "insert into people values (?,?)" );
ps.setInt( 1, 102 );
ps.setObject( 2, new_person );
ps.execute();
ps.close();
rs = stmt.executeQuery( "select * from people" );
System.out.println();
System.out.println( " a new row has been added to the people table" );
System.out.println();
rs.close();
stmt.close();
conn.close();
}
|
|
|