KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > global > TypeUtil


1 /**
2  * com.mckoi.database.global.TypeUtil 01 Aug 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.global;
26
27 /**
28  * Utility for converting to and from 'Types' objects.
29  *
30  * @author Tobias Downer
31  */

32
33 public class TypeUtil {
34
35   /**
36    * Converts from a Class object to a type as specified in Types.
37    */

38   public static int toDBType(Class JavaDoc clazz) {
39     if (clazz == String JavaDoc.class) {
40       return Types.DB_STRING;
41     }
42     else if (clazz == java.math.BigDecimal JavaDoc.class) {
43       return Types.DB_NUMERIC;
44     }
45     else if (clazz == java.util.Date JavaDoc.class) {
46       return Types.DB_TIME;
47     }
48     else if (clazz == Boolean JavaDoc.class) {
49       return Types.DB_BOOLEAN;
50     }
51     else if (clazz == ByteLongObject.class) {
52       return Types.DB_BLOB;
53     }
54     else {
55       return Types.DB_OBJECT;
56     }
57   }
58
59   /**
60    * Converts from a db type to a Class object.
61    */

62   public static Class JavaDoc toClass(int type) {
63     if (type == Types.DB_STRING) {
64       return String JavaDoc.class;
65     }
66     else if (type == Types.DB_NUMERIC) {
67       return java.math.BigDecimal JavaDoc.class;
68     }
69     else if (type == Types.DB_TIME) {
70       return java.util.Date JavaDoc.class;
71     }
72     else if (type == Types.DB_BOOLEAN) {
73       return Boolean JavaDoc.class;
74     }
75     else if (type == Types.DB_BLOB) {
76       return ByteLongObject.class;
77     }
78     else if (type == Types.DB_OBJECT) {
79       return Object JavaDoc.class;
80     }
81     else {
82       throw new Error JavaDoc("Unknown type.");
83     }
84   }
85
86
87
88
89 }
90
Popular Tags