📄 convertutil.java
字号:
package org.speedframework.util;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import org.speedframework.exception.SpeedException;
/**
*
* <p>
* Title: SpeedFrameworkWork持久层框架
* </p>
* <p>
* Description:数据类型转换工具
* </p>
* <p>
* Copyright: Copyright (c) 2005
* </p>
* <p>
* Company: SpeedFrameworkWork
* </p>
*
* @author 程伟杰 电话:13926047208
* @version 1.1beta
*/
public class ConvertUtil {
public ConvertUtil() {
}
/**
* importData
*
* @param ps
* PreparedStatement
* @param index
* int
* @param obj
* Object
* @throws SQLException
*/
public static void importData(PreparedStatement ps, int index, Object obj,
boolean isUpdate) throws SQLException {
if (obj == null) {
ps.setObject(index, null);
} else {
if (obj instanceof String && obj.toString().length() < 500) {
ps.setString(index, obj.toString());
}
if (obj instanceof String && obj.toString().length() > 500) {
ps.setCharacterStream(index, new InputStreamReader(
new ByteArrayInputStream(obj.toString().getBytes())),
obj.toString().length());
}
if (obj instanceof Integer) {
ps.setInt(index, ((Integer) obj).intValue());
}
if (obj instanceof Long) {
ps.setLong(index, ((Long) obj).longValue());
}
if (obj instanceof Short) {
ps.setShort(index, ((Short) obj).shortValue());
}
if (obj instanceof Boolean) {
ps.setBoolean(index, ((Boolean) obj).booleanValue());
}
if (obj instanceof BigDecimal) {
ps.setBigDecimal(index, (BigDecimal) obj);
}
if (obj instanceof Double) {
ps.setDouble(index, ((Double) obj).doubleValue());
}
if (obj instanceof Float) {
ps.setFloat(index, ((Float) obj).floatValue());
}
if (obj instanceof Date) {
ps.setDate(index, new java.sql.Date(((Date) obj).getTime()));
}
if (obj instanceof Byte) {
ps.setByte(index, ((Byte) obj).byteValue());
}
if (obj instanceof Blob && isUpdate) {
ps.setBlob(index, ((Blob) obj));
}
if (obj instanceof Blob && !isUpdate) {
ps.setBinaryStream(index, ((Blob) obj).getBinaryStream(),
(int) ((Blob) obj).length());
}
if (obj instanceof Clob && isUpdate) {
ps.setClob(index, ((Clob) obj));
}
if (obj instanceof Clob && !isUpdate) {
ps.setCharacterStream(index, ((Clob) obj).getCharacterStream(),
(int) ((Clob) obj).length());
}
if (obj instanceof Time) {
ps.setTime(index, ((Time) obj));
}
if (obj instanceof Timestamp) {
ps.setTimestamp(index, ((Timestamp) obj));
}
}
}
/**
* 废弃 数据库对象转换方法
*
* @param rs
* @param index
* @param columnName
* @param voClass
* @return
* @throws SQLException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws SpeedException
*/
public static Object outPortData(ResultSet rs, int index,
String columnName, Class voClass) throws SQLException,
InstantiationException, NoSuchMethodException,
InvocationTargetException, IllegalAccessException, SpeedException {
Object fieldValue = null;
if (columnName != null) {
if ("rownum".equals(columnName)) {
fieldValue = new Long(rs.getLong(index));
} else if ("rownum_".equals(columnName)) {
fieldValue = new Long(rs.getLong(index));
} else if ("count_".equals(columnName)) {
fieldValue = new Long(rs.getLong(index));
} else {
if (rs.getObject(index) != null) {
Class propertyClass = PropertyUtil.getPropertyType(voClass
.newInstance(), columnName);
if (propertyClass == null) {
throw new SpeedException("Property not found "
+ columnName);
}
String classType = propertyClass.getName();
if (classType.equals("java.lang.Integer")
|| classType.equals("int")) {
fieldValue = new Integer(rs.getInt(index));
} else if (classType.equals("java.lang.Long")
|| classType.equals("long")) {
fieldValue = new Long(rs.getLong(index));
} else if (classType.equals("java.math.BigDecimal")) {
fieldValue = rs.getBigDecimal(index);
} else if (classType.equals("java.lang.String")) {
fieldValue = rs.getString(index);
} else if (classType.equals("java.util.Date")) {
java.sql.Date date = rs.getDate(index);
fieldValue = new java.util.Date(date.getTime());
} else if (classType.equals("java.sql.Time")) {
java.sql.Time date = rs.getTime(index);
fieldValue = new java.sql.Time(date.getTime());
} else if (classType.equals("java.sql.Timestamp")) {
java.sql.Timestamp date = rs.getTimestamp(index);
fieldValue = new java.sql.Timestamp(date.getTime());
} else if (classType.equals("java.lang.Double")
|| classType.equals("double")) {
fieldValue = new Double(rs.getDouble(index));
} else if (classType.equals("java.lang.Float")
|| classType.equals("float")) {
fieldValue = new Float(rs.getFloat(index));
} else if (classType.equals("java.lang.Byte")
|| classType.equals("byte")) {
fieldValue = new Byte(rs.getByte(index));
} else if (classType.equals("java.lang.Boolean")
|| classType.equals("boolean")) {
fieldValue = new Boolean(rs.getBoolean(index));
} else if (classType.equals("java.lang.Char")
|| classType.equals("char")) {
fieldValue = new java.lang.Character(rs
.getString(index).charAt(0));
}
}
}
}
return fieldValue;
}
/**
* 数据库对象转换存值
*
* @param rs
* @param columnName
* @param voClass
* @return
* @throws SQLException
* @throws InstantiationException
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws SpeedException
*/
public static Object outPortData(ResultSet rs, String columnName,
Class voClass) throws SQLException, InstantiationException,
NoSuchMethodException, InvocationTargetException,
IllegalAccessException, SpeedException {
Object fieldValue = null;
// Object ObjValue=null;
// ObjValue= rs.getObject(columnName);
if (columnName != null) {
if ("rownum".equals(columnName)) {
fieldValue = new Long(rs.getLong(columnName));
} else if ("rownum_".equals(columnName)) {
fieldValue = new Long(rs.getLong(columnName));
} else if ("count_".equals(columnName)) {
fieldValue = new Long(rs.getLong(columnName));
} else {
// if (rs.getObject(columnName) != null) {
Class propertyClass = PropertyUtil.getPropertyType(voClass
.newInstance(), columnName);
if (propertyClass == null) {
throw new SpeedException("Property not found " + columnName);
}
String classType = propertyClass.getName();
if (classType.equals("java.lang.Integer")
|| classType.equals("int")) {
fieldValue = new Integer(rs.getInt(columnName));
} else if (classType.equals("java.lang.Long")
|| classType.equals("long")) {
fieldValue = new Long(rs.getLong(columnName));
} else if (classType.equals("java.math.BigDecimal")) {
fieldValue = rs.getBigDecimal(columnName);
} else if (classType.equals("java.lang.String")) {
fieldValue = rs.getString(columnName);
} else if (classType.equals("java.util.Date")) {
java.sql.Date date = rs.getDate(columnName);
if (date != null) {
fieldValue = new java.util.Date(date.getTime());
} else {
fieldValue = null;
}
} else if (classType.equals("java.sql.Time")) {
java.sql.Time date = rs.getTime(columnName);
if (date != null) {
fieldValue = date;
} else {
fieldValue = null;
}
} else if (classType.equals("java.sql.Timestamp")) {
java.sql.Timestamp date = rs.getTimestamp(columnName);
if (date != null) {
fieldValue = date;
} else {
fieldValue = null;
}
}
else if (classType.equals("java.lang.Double")
|| classType.equals("double")) {
fieldValue = new Double(rs.getDouble(columnName));
} else if (classType.equals("java.lang.Float")
|| classType.equals("float")) {
fieldValue = new Float(rs.getFloat(columnName));
} else if (classType.equals("java.lang.Byte")
|| classType.equals("byte")) {
fieldValue = new Byte(rs.getByte(columnName));
} else if (classType.equals("java.lang.Boolean")
|| classType.equals("boolean")) {
fieldValue = new Boolean(rs.getBoolean(columnName));
} else if (classType.equals("java.lang.Char")
|| classType.equals("char")) {
fieldValue = new java.lang.Character(rs.getString(
columnName).charAt(0));
} else if (classType.equals("java.sql.Blob")
|| classType.equals("Blob")) {
fieldValue = rs.getBlob(columnName);
} else if (classType.equals("java.sql.Clob")
|| classType.equals("Clob")) {
fieldValue = rs.getClob(columnName);
}
}
// }
}
return fieldValue;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -