KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > mlw > vlh > adapter > jdbc > util > setter > TimestampSetter


1 /**
2  * Copyright (c) 2003 held jointly by the individual authors.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; with out even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * > http://www.gnu.org/copyleft/lesser.html
19  * > http://www.opensource.org/licenses/lgpl-license.php
20  */

21 package net.mlw.vlh.adapter.jdbc.util.setter;
22
23 import java.sql.PreparedStatement JavaDoc;
24 import java.sql.SQLException JavaDoc;
25 import java.sql.Timestamp JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Calendar JavaDoc;
29 import java.util.Date JavaDoc;
30
31 /**
32  * Sets a <code>java.sql.Types.TIMESTAMP</code> on a query using <code>PreparedStatement.setTimestamp()</code>.
33  * Conversion is provided for types <code>java.sql.Timestamp</code>, <code>java.util.Date</code>, <code>java.util.Calendar</code>.
34  * Conversion from a string is provided using a formater - the default format is "MM/dd/yyyy".
35  *
36  * @see PreparedStatement#setTimestamp(int, java.sql.Timestamp)
37  *
38  * @author Matthew L. Wilson
39  * @version $Revision: 1.6 $ $Date: 2005/12/19 10:56:41 $
40  */

41 public class TimestampSetter extends AbstractSetter
42 {
43    public static final String JavaDoc DEFAULT_FORMAT = "MM/dd/yyyy";
44
45    protected SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc(DEFAULT_FORMAT);
46
47    /**
48     * @see net.mlw.vlh.adapter.jdbc.util.Setter#set(java.sql.PreparedStatement, int, java.lang.Object)
49     */

50    public int set(PreparedStatement JavaDoc query, int index, Object JavaDoc value) throws SQLException JavaDoc, ParseException JavaDoc
51    {
52
53       if (value == null || value instanceof Timestamp JavaDoc)
54       {
55          Timestamp JavaDoc timestamp = (Timestamp JavaDoc) value;
56          query.setTimestamp(index++, timestamp);
57       }
58       else if (value instanceof Date JavaDoc)
59       {
60          Date JavaDoc date = (Date JavaDoc) value;
61          Timestamp JavaDoc timestamp = new Timestamp JavaDoc(date.getTime());
62          query.setTimestamp(index++, timestamp);
63       }
64       else if (value instanceof Calendar JavaDoc)
65       {
66          Calendar JavaDoc calendar = (Calendar JavaDoc) value;
67          Timestamp JavaDoc timestamp = new Timestamp JavaDoc(calendar.getTimeInMillis());
68          query.setTimestamp(index++, timestamp);
69       }
70       else if (value instanceof String JavaDoc)
71       {
72          Date JavaDoc date = formatter.parse((String JavaDoc) value);
73          Timestamp JavaDoc timestamp = new Timestamp JavaDoc(date.getTime());
74          query.setTimestamp(index++, timestamp);
75       }
76       else
77       {
78          throw new IllegalArgumentException JavaDoc("Cannot convert object of type " + value.getClass().getName() + " to timestamp at position "
79                + index);
80       }
81
82       return index;
83    }
84
85    /**
86     * @param format The format to set.
87     */

88    public void setFormat(String JavaDoc format)
89    {
90       formatter = new SimpleDateFormat JavaDoc(format);
91    }
92 }
Popular Tags