1 32 33 package com.knowgate.dataobjs; 34 35 import java.sql.DatabaseMetaData ; 36 import java.sql.Types ; 37 38 import java.text.ParseException ; 39 import java.text.SimpleDateFormat ; 40 import java.math.BigDecimal ; 41 42 47 48 public final class DBColumn { 49 50 public DBColumn() { } 51 52 public DBColumn(String sTable, String sColName, 53 short iColType, String sColType, 54 int iPrecision, int iDecDigits, 55 int iNullable, int iColPos) 56 57 { 58 sName = sColName; 59 iPosition = iColPos; 60 sTableName = sTable; 61 iSQLType = iColType; 62 sSQLTypeName = sColType; 63 iMaxSize = iPrecision; 64 iDecimalDigits = iDecDigits; 65 bNullable = (iNullable==DatabaseMetaData.columnNullable); 66 } 67 68 70 74 public String getName() { return sName; } 75 76 80 public void setName(String sColName) { sName=sColName; } 81 82 86 public int getPosition() { return iPosition; } 87 88 92 public void setPosition(int iPos) { iPosition=iPos; } 93 94 98 public String getTableName() { return sTableName; } 99 100 105 106 public short getSqlType() { return iSQLType; } 107 108 112 public void setSqlType(short iType) { 113 iSQLType=iType; 114 sSQLTypeName=DBColumn.typeName(iSQLType); 115 } 116 117 public void setSqlType(int iType) { 118 iSQLType=(short) iType; 119 sSQLTypeName=DBColumn.typeName(iSQLType); 120 } 121 122 126 public String getSqlTypeName() { return sSQLTypeName; } 127 128 132 133 public int getPrecision() { return iMaxSize; } 134 135 139 140 public int getDecimalDigits() { return iDecimalDigits; } 141 142 146 147 public boolean isNullable() { return bNullable; } 148 149 151 public void setDateFormat(String sFmt) throws IllegalArgumentException { 152 oDtFmt = new SimpleDateFormat (sFmt); 153 } 154 155 157 public SimpleDateFormat getDateFormat() { 158 return oDtFmt; 159 } 160 161 163 168 public static String typeName(int iSQLtype) { 169 switch (iSQLtype) { 170 case Types.BIGINT: 171 return "BIGINT"; 172 case Types.BINARY: 173 return "BINARY"; 174 case Types.BIT: 175 return "BIT"; 176 case Types.BLOB: 177 return "BLOB"; 178 case Types.BOOLEAN: 179 return "BOOLEAN"; 180 case Types.CHAR: 181 return "CHAR"; 182 case Types.CLOB: 183 return "CLOB"; 184 case Types.DATE: 185 return "DATE"; 186 case Types.DECIMAL: 187 return "DECIMAL"; 188 case Types.DOUBLE: 189 return "DOUBLE"; 190 case Types.FLOAT: 191 return "FLOAT"; 192 case Types.INTEGER: 193 return "INTEGER"; 194 case Types.LONGVARBINARY: 195 return "LONGVARBINARY"; 196 case Types.LONGVARCHAR: 197 return "LONGVARCHAR"; 198 case Types.NULL: 199 return "NULL"; 200 case Types.NUMERIC: 201 return "NUMERIC"; 202 case Types.REAL: 203 return "REAL"; 204 case Types.SMALLINT: 205 return "SMALLINT"; 206 case Types.TIME: 207 return "TIME"; 208 case Types.TIMESTAMP: 209 return "TIMESTAMP"; 210 case Types.TINYINT: 211 return "TINYINT"; 212 case Types.VARBINARY: 213 return "VARBINARY"; 214 case Types.VARCHAR: 215 return "VARCHAR"; 216 default: 217 return "OTHER"; 218 } 219 } 220 221 223 228 229 public static int getSQLType (String sToken) { 230 int iSQLType; 231 if (sToken.equalsIgnoreCase("VARCHAR")) 232 iSQLType = Types.VARCHAR; 233 else if (sToken.equalsIgnoreCase("CHAR")) 234 iSQLType = Types.CHAR; 235 else if (sToken.equalsIgnoreCase("SMALLINT")) 236 iSQLType = Types.SMALLINT; 237 else if (sToken.equalsIgnoreCase("INTEGER")) 238 iSQLType = Types.INTEGER; 239 else if (sToken.equalsIgnoreCase("FLOAT")) 240 iSQLType = Types.FLOAT; 241 else if (sToken.equalsIgnoreCase("DOUBLE")) 242 iSQLType = Types.DOUBLE; 243 else if (sToken.equalsIgnoreCase("NUMERIC")) 244 iSQLType = Types.NUMERIC; 245 else if (sToken.equalsIgnoreCase("DECIMAL")) 246 iSQLType = Types.DECIMAL; 247 else if (sToken.equalsIgnoreCase("DATE")) 248 iSQLType = Types.DATE; 249 else if (sToken.equalsIgnoreCase("TIMESTAMP")) 250 iSQLType = Types.TIMESTAMP; 251 else if (sToken.equalsIgnoreCase("DATETIME")) 252 iSQLType = Types.TIMESTAMP; 253 else if (sToken.equalsIgnoreCase("NVARCHAR")) 254 iSQLType = Types.VARCHAR; 255 else if (sToken.equalsIgnoreCase("VARCHAR2")) 256 iSQLType = Types.VARCHAR; 257 else if (sToken.equalsIgnoreCase("LONGVARCHAR")) 258 iSQLType = Types.LONGVARCHAR; 259 else if (sToken.equalsIgnoreCase("LONG")) 260 iSQLType = Types.LONGVARCHAR; 261 else if (sToken.equalsIgnoreCase("TEXT")) 262 iSQLType = Types.LONGVARCHAR; 263 else if (sToken.equalsIgnoreCase("LONGVARBINARY")) 264 iSQLType = Types.LONGVARBINARY; 265 else if (sToken.equalsIgnoreCase("LONG RAW")) 266 iSQLType = Types.LONGVARBINARY; 267 else if (sToken.equalsIgnoreCase("BLOB")) 268 iSQLType = Types.BLOB; 269 else if (sToken.equalsIgnoreCase("CLOB")) 270 iSQLType = Types.CLOB; 271 else 272 iSQLType = Types.NULL; 273 return iSQLType; 274 } 275 276 278 public Object convert(String sIn) 279 throws NumberFormatException ,ParseException ,NullPointerException { 280 if (sIn==null) return null; 281 if (sIn.length()==0) return null; 282 switch (iSQLType) { 283 case Types.SMALLINT: 284 return new Short (sIn); 285 case Types.INTEGER: 286 return new Integer (sIn); 287 case Types.FLOAT: 288 return new Float (sIn); 289 case Types.DOUBLE: 290 return new Double (sIn); 291 case Types.DECIMAL: 292 case Types.NUMERIC: 293 return new BigDecimal (sIn); 294 case Types.DATE: 295 case Types.TIMESTAMP: 296 return oDtFmt.parse(sIn); 297 default: 298 return sIn; 299 } 300 } 302 304 private String sName; 305 private int iPosition; 306 private String sTableName; 307 private short iSQLType; 308 private String sSQLTypeName; 309 private int iMaxSize; 310 private int iDecimalDigits; 311 private boolean bNullable; 312 private SimpleDateFormat oDtFmt; 313 }
| Popular Tags
|