KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
21 import java.text.DateFormat JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24
25 /**
26  * SQL Date implementation of TypeHandler
27  */

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