KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > CharacterType


1 //$Id: CharacterType.java,v 1.2 2004/09/11 11:29:23 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.sql.Types JavaDoc;
9
10 import org.hibernate.MappingException;
11
12 /**
13  * <tt>character</tt>: A type that maps an SQL CHAR(1) to a Java Character.
14  * @author Gavin King
15  */

16 public class CharacterType extends PrimitiveType implements DiscriminatorType {
17
18     public Serializable JavaDoc getDefaultValue() {
19         throw new UnsupportedOperationException JavaDoc("not a valid id type");
20     }
21     
22     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name) throws SQLException JavaDoc {
23         String JavaDoc str = rs.getString(name);
24         if (str==null) {
25             return null;
26         }
27         else {
28             return new Character JavaDoc( str.charAt(0) );
29         }
30     }
31
32     public Class JavaDoc getPrimitiveClass() {
33         return char.class;
34     }
35
36     public Class JavaDoc getReturnedClass() {
37         return Character JavaDoc.class;
38     }
39
40     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index) throws SQLException JavaDoc {
41         st.setString( index, (value).toString() );
42     }
43
44     public int sqlType() {
45         return Types.CHAR;
46     }
47     public String JavaDoc getName() { return "character"; }
48
49     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
50         return '\'' + value.toString() + '\'';
51     }
52
53     public Object JavaDoc stringToObject(String JavaDoc xml) throws Exception JavaDoc {
54         if ( xml.length() != 1 ) throw new MappingException("multiple or zero characters found parsing string");
55         return new Character JavaDoc( xml.charAt(0) );
56     }
57
58     public Object JavaDoc fromStringValue(String JavaDoc xml) {
59         return new Character JavaDoc( xml.charAt(0) );
60     }
61
62 }
63
64
65
66
67
68
Popular Tags