panda.dao.sql.adapter.BigDecimalAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.dao.sql.adapter;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* BigDecimal implementation of TypeAdapter
* @param Java Type
*/
public class BigDecimalAdapter extends AbstractCastTypeAdapter {
public BigDecimalAdapter(TypeAdapters adapters, Class javaType) {
super(adapters, javaType, BigDecimal.class);
}
/**
* Gets a column from a result set
*
* @param rs - the result set
* @param column - the column name to get
* @return the column value
* @throws SQLException if getting the value fails
*/
public T getResult(ResultSet rs, String column) throws SQLException {
BigDecimal bigdec = rs.getBigDecimal(column);
return castToJava(bigdec);
}
/**
* Gets a column from a result set
*
* @param rs - the result set
* @param column - the column to get (by index)
* @return the column value
* @throws SQLException if getting the value fails
*/
public T getResult(ResultSet rs, int column) throws SQLException {
BigDecimal bigdec = rs.getBigDecimal(column);
return castToJava(bigdec);
}
/**
* Gets a column from a callable statement
*
* @param cs - the statement
* @param column - the column to get (by index)
* @return the column value
* @throws SQLException if getting the value fails
*/
public T getResult(CallableStatement cs, int column) throws SQLException {
BigDecimal bigdec = cs.getBigDecimal(column);
return castToJava(bigdec);
}
/**
* Update column value to result set
*
* @param rs - the result set
* @param column - the column name to get
* @param value - the value to update
* @throws SQLException if getting the value fails
*/
public void updateResult(ResultSet rs, String column, T value) throws SQLException {
BigDecimal bd = castToJdbc(value);
if (value == null) {
rs.updateNull(column);
}
else {
rs.updateBigDecimal(column, bd);
}
}
/**
* Update column value to result set
*
* @param rs - the result set
* @param column - the column to get (by index)
* @param value - the value to update
* @throws SQLException if getting the value fails
*/
public void updateResult(ResultSet rs, int column, T value) throws SQLException {
BigDecimal bd = castToJdbc(value);
if (value == null) {
rs.updateNull(column);
}
else {
rs.updateBigDecimal(column, bd);
}
}
/**
* Sets a parameter on a prepared statement
*
* @param ps - the prepared statement
* @param i - the parameter index
* @param value - the parameter value
* @throws SQLException if setting the parameter fails
*/
public void setParameter(PreparedStatement ps, int i, T value) throws SQLException {
BigDecimal bd = castToJdbc(value);
ps.setBigDecimal(i, bd);
}
}