1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.onemind.commons.java.sql;
22
23 import java.math.BigDecimal;
24 import java.sql.*;
25 import java.util.*;
26 import java.util.Date;
27 /***
28 * JDBC utilities
29 * @author TiongHiang Lee (thlee@onemindsoft.org)
30 * @version $Id: JdbcUtils.java,v 1.3 2004/10/23 15:25:44 thlee Exp $ $Name: $
31 */
32 public final class JdbcUtils
33 {
34
35 /*** map the java classes to jdbc type int * */
36 public static final Map CLASS_TO_TYPE_MAP;
37 static
38 {
39 Map m = new HashMap();
40 m.put(String.class, new Integer(Types.CHAR));
41 m.put(String.class, new Integer(Types.VARCHAR));
42 m.put(String.class, new Integer(Types.LONGVARCHAR));
43 m.put(BigDecimal.class, new Integer(Types.NUMERIC));
44 m.put(Boolean.class, new Integer(Types.BIT));
45 m.put(Boolean.TYPE, new Integer(Types.BIT));
46 m.put(Integer.class, new Integer(Types.INTEGER));
47 m.put(Integer.TYPE, new Integer(Types.INTEGER));
48 m.put(Long.class, new Integer(Types.BIGINT));
49 m.put(Long.TYPE, new Integer(Types.BIGINT));
50 m.put(Float.class, new Integer(Types.REAL));
51 m.put(Float.TYPE, new Integer(Types.REAL));
52 m.put(Double.class, new Integer(Types.DOUBLE));
53 m.put(Double.TYPE, new Integer(Types.DOUBLE));
54 m.put(byte[].class, new Integer(Types.BINARY));
55 m.put(byte[].class, new Integer(Types.VARBINARY));
56 m.put(byte[].class, new Integer(Types.LONGVARBINARY));
57 m.put(Date.class, new Integer(Types.DATE));
58 m.put(Time.class, new Integer(Types.TIME));
59 m.put(Timestamp.class, new Integer(Types.TIMESTAMP));
60 m.put(Clob.class, new Integer(Types.CLOB));
61 m.put(Blob.class, new Integer(Types.BLOB));
62 m.put(Array.class, new Integer(Types.ARRAY));
63 m.put(Struct.class, new Integer(Types.STRUCT));
64 m.put(Ref.class, new Integer(Types.REF));
65 m.put(Class.class, new Integer(Types.JAVA_OBJECT));
66 CLASS_TO_TYPE_MAP = Collections.unmodifiableMap(m);
67 }
68
69 /***
70 * {@inheritDoc}
71 */
72 private JdbcUtils()
73 {
74 }
75
76 /***
77 * Get the MetaData from the resultset
78 * @param rst the result set
79 * @param name the name of metadata to create
80 * @return the metadata
81 * @throws SQLException if there's database problem
82 */
83 public static MetaData getMetaData(ResultSet rst, String name) throws SQLException
84 {
85 return getMetaData(rst.getMetaData(), name);
86 }
87
88 /***
89 * return the MetaData
90 * @param meta the resultset metadata
91 * @param name the name
92 * @return the meta data
93 * @throws SQLException if there's database problem
94 */
95 public static MetaData getMetaData(ResultSetMetaData meta, String name) throws SQLException
96 {
97 MetaData metaData = new MetaData(name);
98 int n = meta.getColumnCount();
99 for (int i = 1; i <= n; i++)
100 {
101 String cname = meta.getColumnName(i);
102 int ctype = meta.getColumnType(i);
103 metaData.addField(new Field(cname, ctype));
104 }
105 return metaData;
106 }
107
108 /***
109 * Return the jdbc type given the java type (based on JDBC spec)
110 * @param c the java class
111 * @return the jdbc type
112 */
113 public static int toJdbcType(Class c)
114 {
115 Integer i = (Integer) CLASS_TO_TYPE_MAP.get(c);
116 if (i != null)
117 {
118 return i.intValue();
119 } else
120 {
121 throw new IllegalArgumentException("Unknown class type" + c);
122 }
123 }
124
125 /***
126 * Return whether a java type is a jdbc type
127 * @param c the class
128 * @return true if it's jdbc type
129 */
130 public static boolean isJdbcType(Class c)
131 {
132 return CLASS_TO_TYPE_MAP.containsKey(c);
133 }
134 }