001package com.hfg.sql.table.field; 002 003import com.hfg.sql.jdbc.JDBCException; 004import com.hfg.sql.table.DatabaseCol; 005import com.hfg.sql.table.DatabaseTable; 006import com.hfg.xml.XMLTag; 007 008import java.sql.PreparedStatement; 009import java.sql.ResultSet; 010import java.sql.SQLException; 011import java.sql.Types; 012 013//------------------------------------------------------------------------------ 014/** 015 Database field that manages a float value. 016 <div> 017 @author J. Alex Taylor, hairyfatguy.com 018 </div> 019 */ 020//------------------------------------------------------------------------------ 021// com.hfg XML/HTML Coding Library 022// 023// This library is free software; you can redistribute it and/or 024// modify it under the terms of the GNU Lesser General Public 025// License as published by the Free Software Foundation; either 026// version 2.1 of the License, or (at your option) any later version. 027// 028// This library is distributed in the hope that it will be useful, 029// but WITHOUT ANY WARRANTY; without even the implied warranty of 030// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 031// Lesser General Public License for more details. 032// 033// You should have received a copy of the GNU Lesser General Public 034// License along with this library; if not, write to the Free Software 035// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 036// 037// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 038// jataylor@hairyfatguy.com 039//------------------------------------------------------------------------------ 040 041 042public class DatabaseFloatField extends DatabaseField<Float> 043{ 044 045 //########################################################################### 046 // CONSTRUCTORS 047 //########################################################################### 048 049 //--------------------------------------------------------------------------- 050 public DatabaseFloatField(DatabaseCol inCol) 051 { 052 this(inCol, (Object) null); 053 } 054 055 //--------------------------------------------------------------------------- 056 public DatabaseFloatField(DatabaseCol inCol, Float inValue) 057 { 058 super(inCol, inValue); 059 } 060 061 //--------------------------------------------------------------------------- 062 public DatabaseFloatField(DatabaseCol inCol, Object inValue) 063 { 064 super(inCol, convertToFloat(inValue)); 065 } 066 067 //--------------------------------------------------------------------------- 068 public DatabaseFloatField(DatabaseCol inCol, ResultSet inResultSet) 069 { 070 super(inCol, inResultSet); 071 } 072 073 //--------------------------------------------------------------------------- 074 public DatabaseFloatField(XMLTag inXMLTag, DatabaseTable inTable) 075 { 076 super(inXMLTag, inTable); 077 } 078 079 //########################################################################### 080 // PUBLIC METHODS 081 //########################################################################### 082 083 //--------------------------------------------------------------------------- 084 protected void setValueFromResultSet(ResultSet inResultSet) 085 { 086 // Retrieve the index for the ResultSet column with the matching name. 087 // If no column with the proper name is present in the ResultSet, don't do anything. 088 Integer index = getColIndex(inResultSet); 089 if (index != null) 090 { 091 try 092 { 093 float value = inResultSet.getFloat(index); 094 setInitialValue(0 == value && inResultSet.wasNull() ? null : value); 095 } 096 catch (SQLException e) 097 { 098 try 099 { 100 throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); 101 } 102 catch (SQLException e2) 103 { 104 throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); 105 } 106 } 107 } 108 } 109 110 //--------------------------------------------------------------------------- 111 public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex) 112 { 113 try 114 { 115 if (isNull()) 116 { 117 inPreparedStatement.setNull(inIndex, Types.FLOAT); 118 } 119 else 120 { 121 inPreparedStatement.setFloat(inIndex, getValue()); 122 } 123 } 124 catch (SQLException e) 125 { 126 throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e); 127 } 128 } 129 130 //--------------------------------------------------------------------------- 131 public void setValueFromString(String inValue) 132 { 133 setValue(convertToFloat(inValue)); 134 } 135 136 //########################################################################### 137 // PRIVATE METHODS 138 //########################################################################### 139 140 //--------------------------------------------------------------------------- 141 private static Float convertToFloat(Object inValue) 142 { 143 Float floatValue = null; 144 if (inValue != null) 145 { 146 if (inValue instanceof Float) 147 { 148 floatValue = (Float) inValue; 149 } 150 else 151 { 152 try 153 { 154 floatValue = Float.parseFloat(inValue.toString()); 155 } 156 catch (Exception e) 157 { 158 // Ignore 159 } 160 } 161 } 162 163 return floatValue; 164 } 165}