?? value.java
字號:
package com.workingdogs.town;
import java.sql.*;
import java.math.*;
import java.util.Calendar;
/*
Town, a Java JDBC abstraction layer
Copyright (C) 1999 Serge Knystautas, Jon S. Stevens
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
A Value represents a single cell in a database table. In other words, it is the
cross between a row and column and contains the information held there.
@author Jon S. Stevens <A HREF="mailto:jon@working-dogs.com">jon@working-dogs.com</A>
@author Serge Knystautas <a href="mailto:sergek@lokitech.com">sergek@lokitech.com</a>
@version 1.0
*/
public class Value
{
/** the object that is stored in this object */
private Object valueObject;
/** the column number that this object came from */
private int columnNumber;
/** what sql type of object is this? */
private int type;
private Value ()
{
}
/**
* Creates a new Value object based on the ResultSet, columnNumber and type
*
* @param rs
* @param columnNumber
* @param type
* @exception ConnectionException
*/
public Value (ResultSet rs, int columnNumber,
int type) throws ConnectionException
{
this.columnNumber = columnNumber;
this.type = type;
this.valueObject = null;
try
{
if (rs == null)
return;
switch (type())
{
case Types.BIT:
String tmp = rs.getString (columnNumber);
if (tmp == null)
valueObject = new Boolean (false);
else if (tmp.equalsIgnoreCase ("true") ||
tmp.equalsIgnoreCase ("yes") ||
tmp.equals ("1"))
valueObject = new Boolean (true);
else
valueObject = new Boolean (false);
break;
case Types.TINYINT:
valueObject = new Byte (rs.getByte (columnNumber));
break;
case Types.BIGINT:
valueObject = new Long (rs.getLong (columnNumber));
break;
case Types.SMALLINT:
valueObject = new Short (rs.getShort (columnNumber));
break;
case Types.INTEGER:
valueObject = new Integer (rs.getInt (columnNumber));
break;
case Types.REAL:
valueObject = new Float (rs.getFloat (columnNumber));
break;
case Types.FLOAT:
case Types.DOUBLE:
valueObject = new Double (rs.getDouble (columnNumber));
break;
case Types.NUMERIC:
case Types.DECIMAL:
valueObject =
new BigDecimal (rs.getDouble (columnNumber));
break;
case Types.LONGVARBINARY:
case Types.VARBINARY:
case Types.BINARY:
valueObject = rs.getBytes (columnNumber);
break;
case Types.LONGVARCHAR:
case Types.CHAR:
case Types.VARCHAR:
case Types.OTHER:
valueObject = rs.getString (columnNumber);
break;
case Types.DATE:
valueObject = rs.getDate (columnNumber);
break;
case Types.TIME:
valueObject = rs.getTime (columnNumber);
break;
case Types.TIMESTAMP:
valueObject = rs.getTimestamp (columnNumber);
break;
case Types.NULL:
valueObject = null;
break;
default:
valueObject = rs.getString (columnNumber);
break;
}
}
catch (SQLException sqle)
{
throw new ConnectionException (sqle);
}
return;
}
/**
* Get the value as a BigDecimal
*
* @return a BigDecimal
* @exception DataSetException
*/
public BigDecimal asBigDecimal() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBigDecimal())
return (BigDecimal) valueObject;
else if (isString() || isDouble() || isFloat() || isInt() ||
isLong())
return new BigDecimal (asString());
else
return null;
}
catch (Exception e)
{
throw new DataSetException("Illegal conversion: " +
e.toString());
}
}
/**
* Get the value as a BigDecimal
*
* @return a BigDecimal
* @exception DataSetException
*/
public BigDecimal asBigDecimal(int scale) throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBigDecimal())
return ((BigDecimal) valueObject).setScale (scale);
else if (isString() || isDouble() || isFloat() || isInt() ||
isLong())
return new BigDecimal (asString()).setScale (scale);
else
return null;
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asBoolean
*
* @return a boolean
* @exception DataSetException
*/
public boolean asBoolean() throws DataSetException
{
try
{
if (isNull())
return false;
else if (isBoolean())
return ((Boolean) valueObject).booleanValue();
String check = asString();
if (check == null)
return false;
else if (check.equalsIgnoreCase ("yes") ||
check.equalsIgnoreCase ("true") || check.equals ("1"))
return true;
else
return false;
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asByte
*
* @return a byte
* @exception DataSetException
*/
public byte asByte() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isByte())
return ((Byte) valueObject).byteValue();
else if (isString())
return Integer.valueOf((String) valueObject).byteValue();
else if (isShort())
return ((Short) valueObject).byteValue();
else if (isInt())
return ((Integer) valueObject).byteValue();
else if (isLong())
return ((Long) valueObject).byteValue();
else if (isDouble())
return ((Double) valueObject).byteValue();
else if (isFloat())
return ((Float) valueObject).byteValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).byteValue();
else
return Integer.valueOf(asString()).byteValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asBytes
*
* @return a byte array
* @exception DataSetException
*/
public byte[] asBytes() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isBytes())
return (byte[]) valueObject;
else if (isString())
return ((String) valueObject).getBytes();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
return null;
}
/**
* Get the value as a asDate
*
* @return a java.sql.Date
* @exception DataSetException
*/
public java.sql.Date asDate() throws DataSetException
{
try
{
if (isNull())
return null;
else if (isDate())
return (java.sql.Date) valueObject;
Calendar cal = Calendar.getInstance();
if (isTimestamp())
{
cal.setTime ((Timestamp) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isTime())
{
cal.setTime ((Time) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isUtilDate())
{
cal.setTime ((java.util.Date) valueObject);
return java.sql.Date.valueOf (cal.get(Calendar.YEAR) +
"-" + (cal.get(Calendar.MONTH) + 1) + "-" +
cal.get(Calendar.DAY_OF_MONTH));
}
else if (isString())
return java.sql.Date.valueOf((String) valueObject);
else
return java.sql.Date.valueOf(asString());
}
catch (IllegalArgumentException a)
{
throw new DataSetException("Bad date value - Java Timestamp Objects cannot be earlier than 1/1/70");
}
catch (Exception b)
{
throw new DataSetException("Bad conversion: " + b.toString());
}
}
/**
* Get the value as a asDouble
*
* @return a double
* @exception DataSetException
*/
public double asDouble() throws DataSetException
{
try
{
if (isNull())
return 0.0D;
else if (isDouble())
return ((Double) valueObject).doubleValue();
else if (isString())
return Integer.valueOf((String) valueObject).doubleValue();
else if (isShort())
return ((Short) valueObject).doubleValue();
else if (isInt())
return ((Integer) valueObject).doubleValue();
else if (isLong())
return ((Long) valueObject).doubleValue();
else if (isFloat())
return ((Float) valueObject).doubleValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).doubleValue();
else
return Integer.valueOf(asString()).doubleValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asFloat
*
* @return a float
* @exception DataSetException
*/
public float asFloat() throws DataSetException
{
try
{
if (isNull())
return 0.0F;
else if (isFloat())
return ((Float) valueObject).floatValue();
else if (isString())
return Integer.valueOf((String) valueObject).floatValue();
else if (isShort())
return ((Short) valueObject).floatValue();
else if (isInt())
return ((Integer) valueObject).floatValue();
else if (isLong())
return ((Long) valueObject).floatValue();
else if (isDouble())
return ((Double) valueObject).floatValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).floatValue();
else
return Integer.valueOf(asString()).floatValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asInt
*
* @return an int
* @exception DataSetException
*/
public int asInt() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isInt())
return ((Integer) valueObject).intValue();
else if (isString())
return Integer.valueOf((String) valueObject).intValue();
else if (isLong())
return ((Long) valueObject).intValue();
else if (isDouble())
return ((Double) valueObject).intValue();
else if (isFloat())
return ((Float) valueObject).intValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).intValue();
else
return Integer.valueOf(asString()).intValue();
}
catch (Exception e)
{
throw new DataSetException("Bad conversion: " + e.toString());
}
}
/**
* Get the value as a asLong
*
* @return a long
* @exception DataSetException
*/
public long asLong() throws DataSetException
{
try
{
if (isNull())
return 0;
else if (isLong())
return ((Long) valueObject).longValue();
else if (isString())
return Integer.valueOf((String) valueObject).longValue();
else if (isShort())
return ((Short) valueObject).longValue();
else if (isInt())
return ((Integer) valueObject).longValue();
else if (isDouble())
return ((Double) valueObject).longValue();
else if (isFloat())
return ((Float) valueObject).longValue();
else if (isBigDecimal())
return ((BigDecimal) valueObject).longValue();
else
return Integer.valueOf(asString()).longValue();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -