How to Register a Custom Application in Oracle
From Java Example Source Code
Contents |
[edit] Overview - How to Register a Custom Application in Oracle
This Java example program shows how to register a custom application in oracle.
[edit] Java Source Code
- Package: example.jdbc.oraclejdbc
- File: InsertCustomType2_Oracle.java
package example.jdbc.oraclejdbc; import java.io.Serializable; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLData; import java.sql.SQLException; import java.sql.SQLInput; import java.sql.SQLOutput; 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); book.print(); 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); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { try { pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } /** * 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; this.author = 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(); this.author = stream.readString(); this.edition = stream.readInt(); } // writes this object to stream public void writeSQL(SQLOutput stream) throws SQLException { stream.writeString(this.isbn); stream.writeString(this.title); stream.writeString(this.author); stream.writeInt(this.edition); } public void print() { System.out.println("isbn=" + isbn); System.out.println("title=" + title); System.out.println("author=" + author); System.out.println("edition=" + edition); } }
[edit] What Result You Can Get
Coming soon...
[edit] Required External Libraries and/or Files for this Java Example
In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:
- MySQL JDBC driver. You can download it from MySQL Connector/J — for connecting to MySQL from Java
- Oracle JDBC driver. You can download it from JDBC driver for Oracle.
- MSSQL JDBC driver. There are several vendors provide JDBC driver for MSSQL, you should chose one of them:
- jTDS - SQL Server and Sybase JDBC driver. It is an open source JDBC 3.0 Type 4 driver for Microsoft SQL Server (6.5, 7.0, 2000 and 2005) and Sybase. jTDS is the fastest JDBC driver for MS SQL Server and is a complete implementation of the JDBC spec. You can download it from jTDS - SQL Server and Sybase JDBC driver
- MSSQL JDBC driver from Microsoft.
- JDBC driver for MSSQL 2000. You can download it from SQL Server 2000 Driver for JDBC Service Pack 3
- JDBC driver for MSSQL 2005. You can download it from Microsoft SQL Server 2005 JDBC Driver 1.2
- PostgreSQL JDBC driver. You can download it from PostgreSQL JDBC driver. It is a pure Java (Type IV) implementation.
- HSQLDB JDBC Driver. HSQLDB is the leading SQL relational database engine written in Java. It has a JDBC driver and supports a rich subset of ANSI-92 SQL plus SQL 99 and 2003 enhancements. You can download it from hsqldb.
[edit] How to Run this Java Example Program
We recommend running this Java example program with Eclipse.
For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.
It's fairly easy.
[edit] Question & Answer
Any question?
Click edit and post your question or answer here.
