KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > sql > conv > DateTimestampConverter


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.sql.conv;
13
14 import com.versant.core.jdbc.JdbcConverter;
15 import com.versant.core.jdbc.JdbcTypeRegistry;
16 import com.versant.core.jdbc.metadata.JdbcColumn;
17
18 import javax.jdo.JDOFatalDataStoreException; //todo: appears only in throws clause
19
import java.sql.*;
20 import java.util.Date JavaDoc;
21
22 /**
23  * This converts java.util.Date's to and from a column using setTimestamp
24  * and createTimestamp.
25  */

26 public class DateTimestampConverter extends JdbcConverterBase {
27
28     private static final boolean useNanos;
29
30     static {
31         
32         String JavaDoc v = System.getProperty("java.version");
33         useNanos = v.startsWith("1.3");
34
35         
36     }
37
38     public static class Factory extends NoArgJdbcConverterFactory {
39
40         private DateTimestampConverter converter;
41
42         /**
43          * Create a converter for col using props as parameters. Return null if
44          * no converter is required.
45          */

46         public JdbcConverter createJdbcConverter(JdbcColumn col, Object JavaDoc args,
47                 JdbcTypeRegistry jdbcTypeRegistry) {
48             if (converter == null) converter = new DateTimestampConverter();
49             return converter;
50         }
51
52     }
53
54     /**
55      * Get the value of col from rs at position index.
56      *
57      * @throws SQLException on SQL errors
58      * @throws JDOFatalDataStoreException if the ResultSet value is invalid
59      */

60     public Object JavaDoc get(ResultSet rs, int index, JdbcColumn col)
61             throws SQLException, JDOFatalDataStoreException {
62         Timestamp t = rs.getTimestamp(index);
63         if (t == null) return null;
64         if (useNanos) {
65             return new Date JavaDoc(t.getTime() + (t.getNanos() / 1000000));
66         } else {
67             return new Date JavaDoc(t.getTime());
68         }
69     }
70
71     /**
72      * Set parameter index on ps to value (for col).
73      *
74      * @throws SQLException on SQL errors
75      * @throws JDOFatalDataStoreException if value is invalid
76      */

77     public void set(PreparedStatement ps, int index, JdbcColumn col,
78             Object JavaDoc value)
79             throws SQLException, JDOFatalDataStoreException {
80         if (value == null) {
81             ps.setNull(index, col.jdbcType);
82         } else {
83             Date JavaDoc d = (Date JavaDoc)value;
84             if (col.jdbcType == Types.DATE) {
85                 ps.setDate(index, new java.sql.Date JavaDoc(d.getTime()));
86             } else {
87                 ps.setTimestamp(index, new Timestamp(d.getTime()));
88             }
89         }
90     }
91
92     /**
93      * Get the type of our expected value objects (e.g. java.util.Locale
94      * for a converter for Locale's).
95      */

96     public Class JavaDoc getValueType() {
97         return Date JavaDoc.class;
98     }
99 }
100
101
Popular Tags