01: /*
02: * Copyright 2004 Clinton Begin
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 java.sql.CallableStatement;
19: import java.sql.PreparedStatement;
20: import java.sql.ResultSet;
21: import java.sql.SQLException;
22: import java.util.Date;
23:
24: /**
25: * Date (and time) implementation of TypeHandler
26: */
27: public class DateTypeHandler extends BaseTypeHandler implements
28: TypeHandler {
29:
30: private static final String DATE_FORMAT = "yyyy/MM/dd hh:mm:ss";
31:
32: public void setParameter(PreparedStatement ps, int i,
33: Object parameter, String jdbcType) throws SQLException {
34: ps.setTimestamp(i, new java.sql.Timestamp(((Date) parameter)
35: .getTime()));
36: }
37:
38: public Object getResult(ResultSet rs, String columnName)
39: throws SQLException {
40: java.sql.Timestamp sqlTimestamp = rs.getTimestamp(columnName);
41: if (rs.wasNull()) {
42: return null;
43: } else {
44: return new java.util.Date(sqlTimestamp.getTime());
45: }
46: }
47:
48: public Object getResult(ResultSet rs, int columnIndex)
49: throws SQLException {
50: java.sql.Timestamp sqlTimestamp = rs.getTimestamp(columnIndex);
51: if (rs.wasNull()) {
52: return null;
53: } else {
54: return new java.util.Date(sqlTimestamp.getTime());
55: }
56: }
57:
58: public Object getResult(CallableStatement cs, int columnIndex)
59: throws SQLException {
60: java.sql.Timestamp sqlTimestamp = cs.getTimestamp(columnIndex);
61: if (cs.wasNull()) {
62: return null;
63: } else {
64: return new java.util.Date(sqlTimestamp.getTime());
65: }
66: }
67:
68: public Object valueOf(String s) {
69: return SimpleDateFormatter.format(DATE_FORMAT, s);
70: }
71:
72: }
|