用SQL Data读写数据库自定义类型(一)

2015-01-23 21:53:20 · 作者: · 浏览: 26
如何读写自定义类型?SQLData是个很直观的解决办法
在oracle使用手册上找到了很好的资料

但原文太长,我精炼提取了关于SQLData的关键部分如下(大家只能读英文了),我创建了一个自定义类型MyTime由小时和分组成,比如13点
55分就是13:55
create or replace type MyTime as OBJECT(h int, m int);
Oracle object types provide support for composite data structures in the database.
JDBC materializes Oracle objects as instances of particular Java classes. Two main
steps in using JDBC to access Oracle objects are: 1) creating the Java classes for
the Oracle objects, and 2) populating these classes. You have two options:
1.Let JDBC materialize the object as a STRUCT.
2.Explicitly specify the mappings between Oracle objects and Java classes.you can define
your classes to implement either the JDBC standard java.sql.SQLData interface or the Oracle
extension oracle.sql.ORAData interface
If you want to create custom object classes for your Oracle objects, then you must define
entries in the type map that specify the custom object classes that the drivers will
instantiate for the corresponding Oracle objects.
Map > map = sample.getTypeMap();
/* Use the getTypeMap() method of your OracleConnection object to return the connection's
* type map object.
If the type map in the OracleConnection instance has not been initialized, then the first
call to getTypeMap() returns an empty map.*/
map.put("MYTIME", MyTime.class);
/*Use the type map's put() method to add map entries. The put() method takes two arguments:
* a SQL type name string and an instance of a specified Java class that you want to map to.
myMap.put(sqlTypeName, classObject);
SQL type names in the type map must be all uppercase, because that is how the Oracle
database stores SQL names.
The sqlTypeName is a string that represents the fully qualified name of the SQL type in
the database. The classObject is the Java class object to which you want to map the SQL
type. Get the class object with the Class.forName() method, as follows:
myMap.put(sqlTypeName, Class.forName(className));
*/
sample.setTypeMap(map);
/* When you finish adding entries to the map, use the OracleConnection object's setTypeMap()
* method to overwrite the connection's existing type map. For example:
oraconn.setTypeMap(newMap);
In this example, setTypeMap() overwrites the oraconn connection's original map with newMap.
If you do not provide a type map with an appropriate entry when using a getObject() call,
then the JDBC driver will materialize an Oracle object as an instance of the
oracle.sql.STRUCT class.
*/
String sql = "insert into train values(?, ?, ?, ?, ?, ?)";//第五个参数是MyTime

PreparedStatement pstmt = sample.prepareStatement(sql);
/* Use the setObject() method of the prepared statement to bind your Java datatype
* object to the prepared statement.
pstmt.setObject(1, emp);
Use the getObject() method to retrieve the employee object. The following code
assumes that there is a type map entry to map the Oracle object to Java type Employee:
Employee emp = (Employee)ocs.getObject(1);
*/
pstmt.setObject(5, mt2);
下面是MyTime的类定义
static class MyTime implements SQLData

{
/*The SQLData interface defines methods that translate between SQL and Java for Oracle
* database objects.
* If you create a custom object class that implements SQLData, then you must provide a
* readSQL() method and a writeSQL() method, as specified by the SQLData interface.




The JDBC driver calls your readSQL() method to read a stream of data values from the database
and populate an instance of your custom object class