Register custome type to Oracle : Oracle JDBC « Database SQL JDBC « Java

Register custome type to Oracle

import java.util.*;
import java.sql.*;

public class InsertCustomType2_Oracle {
  public static Connection getConnection() throws Exception {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
    String username = "name";
    String password = "pass";
    Class.forName(driver); // load Oracle driver
    return DriverManager.getConnection(url, username, password);

  public static void main(String[] args) {
    String id = "001";
    String isbn = "1234567890";
    String title = "Java Oracle";
    String author = "java2s";
    int edition = 1;

    // create the Book object
    Book book = new Book(isbn, title, author, edition);

    Connection conn = null;
    PreparedStatement pstmt = null;
    try {
      conn = getConnection();
      // create type map
      java.util.Map map = conn.getTypeMap();
      System.out.println("map=" + map);
      map.put("BOOK", Class.forName("Book"));
      System.out.println("map=" + map);

      String insert = "insert into book_table(ID, BOOK) values(?, ?)";
      pstmt = conn.prepareStatement(insert);
      pstmt.setString(1, id);
      pstmt.setObject(2, book);
    } catch (Exception e) {
    } finally {
      try {
      } catch (SQLException e) {

 * A class to hold a copy of "BOOK" data type
class Book implements SQLData, Serializable {

  public static final String SQL_TYPE_NAME = "BOOK";

  public String isbn;

  public String title;

  public String author;

  public int edition;

  public Book() {

  public Book(String isbn, String title, String author, int edition) {
    this.isbn = isbn;
    this.title = title; = author;
    this.edition = edition;

  // retrieves the fully qualified name of the SQL
  // user-defined type that this object represents.
  public String getSQLTypeName() {
    return SQL_TYPE_NAME;

  // populates this object with data it reads from stream
  public void readSQL(SQLInput stream, String sqlType) throws SQLException {
    this.isbn = stream.readString();
    this.title = stream.readString(); = stream.readString();
    this.edition = stream.readInt();

  // writes this object to stream
  public void writeSQL(SQLOutput stream) throws SQLException {

  public void print() {
    System.out.println("isbn=" + isbn);
    System.out.println("title=" + title);
    System.out.println("author=" + author);
    System.out.println("edition=" + edition);

Related examples in the same category

1.Create a table in database
2.Get Object From Oracle Database Using STRUCT
3.Insert BLOG(Picture or Photo) Data Type Into Oracle Database
4.Serialized And Deserialize Object Oracle
5.Get Oracle Table Names
6.Get Parameter MetaData From Oracle JDBC Driver
7.Test Oracle JDBC Driver Installation
8.Create Employee Table Oracle
9.Count row in Oracle
10.Get Column Names From ResultSet for Oracle
11.Demo ResultSet Oracle
12.All data types for Oracle
13.Get Column Privileges Oracle
14.Test OCINet 8 App
15.Test SSL
16.Test Data Encryption Integrity
17.Test DataSource LookUp
18.OracleDataSource Demo
19.Insert custom type to Oracle
20.Check JDBC Installation for Oracle
21.Creating an OBJECT Type in an Oracle Database
22.Inserting an OBJECT Value into an Oracle Table
23.Connect to an Oracle database with JDBC
24.Creating an Oracle Table to Store Java Types