Inserting my Cutomized Java Class object in the database
1 posts in topic
Flat View  Flat View

Posted By:   l_p
Posted On:   Friday, November 2, 2001 10:13 PM

I want to insert and retrieve a java class object into the database.Is this possible.Can setObject and getObject methods be used to achieve this and if so, what should be the corressponding datatype in the database

Any help would be appreciated

Thanks !

Re: Inserting my Cutomized Java Class object in the database

Posted By:   Bozidar_Dangubic  
Posted On:   Saturday, November 3, 2001 01:11 PM

use java.sql.SQLData interface. here is what you need to do.

  1. create a java class you want to store to the database. make sure that it implements java.sql.SQLData interface
  2. create a row type in the database that corresponds to the class you created
  3. when you getConnection() from the DriverManager or DataSource get the type map (Connection.getTypeMap()) and set the your java class to map row type you create in the database

    for example,

    Map typeMap = conn.getTypeMap();

    typeMap.put("address_t", Class.forName("com.yourcompany.Address"));

    address_t is the name of the row type you created in the database, com.yourcompany.Address is the java class you want to store in the database.
  4. when you call setObject(address) it will be stored in the database which has the column declared as address_t. when you call getObject("address_column") you need to cast to Address.
    for example,
    Address addr = (Address) resultSet.getObject("address_col");
About | Sitemap | Contact