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.math.BigDecimal;
24  import java.math.BigInteger;
25  import java.sql.PreparedStatement;
26  import java.sql.SQLException;
27  import java.sql.Types;
28  import java.text.ParseException;
29  
30  /*** Sets a <code>java.sql.Types.DECIMAL</code> on a query using <code>PreparedStatement.setBigDecimal()</code>.
31   * Conversion is provided from <code>java.math.BigDecimal</code>, <code>java.math.BigInteger</code>,
32   * <code>java.lang.Double</code>, <code> java.lang.Long</code>. Conversion from a string is provided
33   * using <code>Long.parseLong()</code> method.
34   * 
35   * @author Matthew L. Wilson
36   * @version $Revision: 1.2 $ $Date: 2005/12/19 10:58:01 $
37   */
38  public class DecimalSetter extends AbstractSetter
39  {
40     /***
41      * @see net.mlw.vlh.adapter.jdbc.util.Setter#set(java.sql.PreparedStatement, int, java.lang.Object)
42      */
43     public int set(PreparedStatement query, int index, Object value) throws SQLException, ParseException
44     {
45        if (value instanceof BigDecimal)
46        {
47           query.setBigDecimal(index++, (BigDecimal) value);
48        }
49        else if (value instanceof BigInteger)
50        {
51           BigDecimal decimal = new BigDecimal((BigInteger) value);
52           query.setBigDecimal(index++, decimal);
53        }
54        else if (value instanceof Long)
55        {
56           BigDecimal decimal = BigDecimal.valueOf(((Long) value).longValue());
57           query.setBigDecimal(index++, decimal);
58        }
59        else if (value instanceof Double)
60        {
61           BigDecimal decimal = new BigDecimal(((Double) value).doubleValue());
62           query.setBigDecimal(index++, decimal);
63        }
64        else if (value instanceof String)
65        {
66           BigDecimal decimal = new BigDecimal((String) value);
67           query.setBigDecimal(index++, decimal);
68        }
69        else if (value == null)
70        {
71           query.setNull(index++, Types.DECIMAL);
72        }
73        else
74        {
75           throw new IllegalArgumentException("Cannot convert value of class " + value.getClass().getName() + " to decimal at position "
76                 + index);
77        }
78        return index;
79     }
80  }