View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
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   * May jdbc type to java type
29   * @author TiongHiang Lee (thlee@onemindsoft.org)
30   * @version $Id: TypeMapper.java,v 1.2 2004/08/26 12:33:18 thlee Exp $ $Name:  $
31   */
32  public final class TypeMapper
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       * Return the jdbc type given the java type (based on JDBC spec)
71       * @param c the java class
72       * @return the jdbc type
73       */
74      public static int toJdbcType(Class c)
75      {
76          Integer i = (Integer) CLASS_TO_TYPE_MAP.get(c);
77          if (i != null)
78          {
79              return i.intValue();
80          } else
81          {
82              throw new IllegalArgumentException("Unknown class type" + c);
83          }
84      }
85  
86      /***
87       * Return whether a java type is a jdbc type
88       * @param c the class
89       * @return true if it's jdbc type
90       */
91      public static boolean isJdbcType(Class c)
92      {
93          return CLASS_TO_TYPE_MAP.containsKey(c);
94      }
95  
96      /***
97       * Constructor
98       */
99      private TypeMapper()
100     {
101     }
102 }