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 short (2 byte) integer 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 DatabaseShortField extends DatabaseField<Short> 043{ 044 045 //########################################################################### 046 // CONSTRUCTORS 047 //########################################################################### 048 049 //--------------------------------------------------------------------------- 050 public DatabaseShortField(DatabaseCol inCol) 051 { 052 this(inCol, (Object) null); 053 } 054 055 //--------------------------------------------------------------------------- 056 public DatabaseShortField(DatabaseCol inCol, Short inValue) 057 { 058 super(inCol, inValue); 059 } 060 061 //--------------------------------------------------------------------------- 062 public DatabaseShortField(DatabaseCol inCol, Object inValue) 063 { 064 super(inCol, convertToShort(inValue)); 065 } 066 067 //--------------------------------------------------------------------------- 068 public DatabaseShortField(DatabaseCol inCol, ResultSet inResultSet) 069 { 070 super(inCol, inResultSet); 071 } 072 073 //--------------------------------------------------------------------------- 074 public DatabaseShortField(XMLTag inXMLTag, DatabaseTable inTable) 075 { 076 super(inXMLTag, inTable); 077 } 078 079 //########################################################################### 080 // PUBLIC METHODS 081 //########################################################################### 082 083 //--------------------------------------------------------------------------- 084 public void setValue(Integer inValue) 085 { 086 setValue(inValue != null ? inValue.shortValue() : null); 087 } 088 089 //--------------------------------------------------------------------------- 090 protected void setValueFromResultSet(ResultSet inResultSet) 091 { 092 // Retrieve the index for the ResultSet column with the matching name. 093 // If no column with the proper name is present in the ResultSet, don't do anything. 094 Integer index = getColIndex(inResultSet); 095 if (index != null) 096 { 097 try 098 { 099 short value = inResultSet.getShort(index); 100 setInitialValue(0 == value && inResultSet.wasNull() ? null : value); 101 } 102 catch (SQLException e) 103 { 104 try 105 { 106 throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e); 107 } 108 catch (SQLException e2) 109 { 110 throw new JDBCException("Problem mapping " + getCol().name() + " value!", e); 111 } 112 } 113 } 114 } 115 116 //--------------------------------------------------------------------------- 117 public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex) 118 { 119 try 120 { 121 if (isNull()) 122 { 123 inPreparedStatement.setNull(inIndex, Types.SMALLINT); 124 } 125 else 126 { 127 inPreparedStatement.setShort(inIndex, getValue()); 128 } 129 } 130 catch (SQLException e) 131 { 132 throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e); 133 } 134 } 135 136 //--------------------------------------------------------------------------- 137 public void setValueFromString(String inValue) 138 { 139 setValue(convertToShort(inValue)); 140 } 141 142 //########################################################################### 143 // PRIVATE METHODS 144 //########################################################################### 145 146 //--------------------------------------------------------------------------- 147 private static Short convertToShort(Object inValue) 148 { 149 Short shortValue = null; 150 if (inValue != null) 151 { 152 if (inValue instanceof Integer) 153 { 154 shortValue = ((Integer) inValue).shortValue(); 155 } 156 else 157 { 158 try 159 { 160 shortValue = Short.parseShort(inValue.toString()); 161 } 162 catch (Exception e) 163 { 164 // Ignore 165 } 166 } 167 } 168 169 return shortValue; 170 } 171}