View Javadoc

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;
24  import java.sql.SQLException;
25  import java.sql.Timestamp;
26  import java.text.ParseException;
27  import java.text.SimpleDateFormat;
28  import java.util.Calendar;
29  import java.util.Date;
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 DEFAULT_FORMAT = "MM/dd/yyyy";
44  
45     protected SimpleDateFormat formatter = new SimpleDateFormat(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 query, int index, Object value) throws SQLException, ParseException
51     {
52  
53        if (value == null || value instanceof Timestamp)
54        {
55           Timestamp timestamp = (Timestamp) value;
56           query.setTimestamp(index++, timestamp);
57        }
58        else if (value instanceof Date)
59        {
60           Date date = (Date) value;
61           Timestamp timestamp = new Timestamp(date.getTime());
62           query.setTimestamp(index++, timestamp);
63        }
64        else if (value instanceof Calendar)
65        {
66           Calendar calendar = (Calendar) value;
67           Timestamp timestamp = new Timestamp(calendar.getTimeInMillis());
68           query.setTimestamp(index++, timestamp);
69        }
70        else if (value instanceof String)
71        {
72           Date date = formatter.parse((String) value);
73           Timestamp timestamp = new Timestamp(date.getTime());
74           query.setTimestamp(index++, timestamp);
75        }
76        else
77        {
78           throw new IllegalArgumentException("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 format)
89     {
90        formatter = new SimpleDateFormat(format);
91     }
92  }