KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > sqlmap > engine > type > DateTypeHandler


1 /*
2  * Copyright 2004 Clinton Begin
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.ibatis.sqlmap.engine.type;
17
18 import com.ibatis.sqlmap.client.SqlMapException;
19
20 import java.sql.CallableStatement JavaDoc;
21 import java.sql.PreparedStatement JavaDoc;
22 import java.sql.ResultSet JavaDoc;
23 import java.sql.SQLException JavaDoc;
24 import java.text.DateFormat JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.text.SimpleDateFormat JavaDoc;
27 import java.util.Date JavaDoc;
28
29 /**
30  * Date (and time) implementation of TypeHandler
31  */

32 public class DateTypeHandler extends BaseTypeHandler implements TypeHandler {
33
34   private static final String JavaDoc DATE_FORMAT = "yyyy/MM/dd hh:mm:ss";
35   private static final DateFormat JavaDoc format = new SimpleDateFormat JavaDoc(DATE_FORMAT);
36
37   public void setParameter(PreparedStatement JavaDoc ps, int i, Object JavaDoc parameter, String JavaDoc jdbcType)
38       throws SQLException JavaDoc {
39     ps.setTimestamp(i, new java.sql.Timestamp JavaDoc(((Date JavaDoc) parameter).getTime()));
40   }
41
42   public Object JavaDoc getResult(ResultSet JavaDoc rs, String JavaDoc columnName)
43       throws SQLException JavaDoc {
44     java.sql.Timestamp JavaDoc sqlTimestamp = rs.getTimestamp(columnName);
45     if (rs.wasNull()) {
46       return null;
47     } else {
48       return new java.util.Date JavaDoc(sqlTimestamp.getTime());
49     }
50   }
51
52   public Object JavaDoc getResult(ResultSet JavaDoc rs, int columnIndex)
53       throws SQLException JavaDoc {
54     java.sql.Timestamp JavaDoc sqlTimestamp = rs.getTimestamp(columnIndex);
55     if (rs.wasNull()) {
56       return null;
57     } else {
58       return new java.util.Date JavaDoc(sqlTimestamp.getTime());
59     }
60   }
61
62   public Object JavaDoc getResult(CallableStatement JavaDoc cs, int columnIndex)
63       throws SQLException JavaDoc {
64     java.sql.Timestamp JavaDoc sqlTimestamp = cs.getTimestamp(columnIndex);
65     if (cs.wasNull()) {
66       return null;
67     } else {
68       return new java.util.Date JavaDoc(sqlTimestamp.getTime());
69     }
70   }
71
72   public Object JavaDoc valueOf(String JavaDoc s) {
73     try {
74       return format.parse(s);
75     } catch (ParseException JavaDoc e) {
76       throw new SqlMapException("Error parsing default null value date. Format must be '" + DATE_FORMAT + "'. Cause: " + e);
77     }
78   }
79
80 }
81
Popular Tags