Java.sql包中的Connection接口的“自定义类型映射”。
发布网友
发布时间:2022-05-18 11:16
我来回答
共1个回答
热心网友
时间:2023-10-15 13:51
Oracle 官方网页上有这段解释,就是说我们如何把一个 SQL 东西翻译成一个 Java 类型的。比如,一个 Date 列翻译成 java.sql.Date,一个 number(1) 翻译成 boolean 而不是 integer。
至于如何使用这个类型映射,你先在调试模式下看一下它原来返回的 getTypeMap() 是什么样的,里面应该已经有默认的翻译,只是不支持自定义的 SQL Type,比如 Java 类型或其它数据库特有的类型。
Parameters:
columnIndex - the first column is 1, the second is 2, ...
map - a java.util.Map object that contains the mapping from SQL type names to classes in the Java programming language
Returns:
an Object in the Java programming language representing the SQL value
另外一段:说 connection.getTypeMap(); 演示我们把一个自定义的类型 mySchemaName.ATHLETES 的字段翻译成 Athletes 的 java 字段。
A user may create a new type map, which is a java.util.Map object, make an entry in it, and pass it to the java.sql methods that can perform custom mapping. In this case, the method will use the given type map instead of the one associated with the connection.
For example, the following code fragment specifies that the SQL type ATHLETES will be mapped to the class Athletes in the Java programming language. The code fragment retrieves the type map for the Connection objectcon, inserts the entry into it, and then sets the type map with the new entry as the connection's type map.
java.util.Map map = con.getTypeMap();
map.put("mySchemaName.ATHLETES", Class.forName("Athletes"));
con.setTypeMap(map);
再说说写一个自己的自定义类型:
http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html#getObject(int,%20java.util.Map)