oracel資料類型跟java類型的對應
rid:java.lang.String
type_binary_double:java.lang.Double
type_binary_float:java.lang.Float
type_blob:oracle.sql.BLOB
type_clob:oracle.sql.CLOB
type_date:java.sql.Timestamp
type_long:java.lang.String
type_number:java.math.BigDecimal
type_nvarchar2:java.lang.String
type_raw:[B
type_timestamp:oracle.sql.TIMESTAMP
type_timestamp_with_local:oracle.sql.TIMESTAMPLTZ
type_timestamp_with_time:oracle.sql.TIMESTAMPTZ
type_varchar2:java.lang.String
一個類方法:
public T handler(ResultSet rs,Class<T> clazz){
T entity=null;
try {
entity=clazz.newInstance();
ResultSetMetaData rsMetaData=rs.getMetaData();
int columnCount=rsMetaData.getColumnCount();
List<String> columnNameList=new ArrayList<String>();
for (int i = 0; i < columnCount; i++) {
columnNameList.add(rsMetaData.getColumnLabel(i + 1).toLowerCase());
}
Class<?> fieldTypeClazz=null;
Field[] fieldArr=getWholeFields(clazz);
for(Field field:fieldArr){
field.setAccessible(true);
if(columnNameList.contains(field.getName().toLowerCase())){
Object value = rs.getObject(field.getName());
fieldTypeClazz=field.getType();
if(value!=null){
Class<?> dbType = value.getClass();
if("java.lang.String".equals(dbType.getName())){//對應oracle的char、LONG、nvarchar2、varchar2
}else if("java.lang.Double".equals(dbType.getName())){//對應oracle的binary_double
value=Double.valueOf(value.toString());
}else if("java.lang.Float".equals(dbType.getName())){//對應oracle的binary_float
value=Float.valueOf(value.toString());
}else if("java.math.BigDecimal".equals(dbType.getName())){//對應oracel的number
if("java.lang.Integer".equalsIgnoreCase(fieldTypeClazz.getName())){
value = Integer.valueOf(value.toString());
}else if("java.lang.Long".equalsIgnoreCase(fieldTypeClazz.getName())){
value = Long.valueOf(value.toString());
}else if("java.lang.Double".equalsIgnoreCase(fieldTypeClazz.getName())){
value = Double.valueOf(value.toString());
}
}else if("oracle.sql.BLOB".equals(dbType.getName())){//對應oracle的BLOB
if("java.io.InputStream".equalsIgnoreCase(fieldTypeClazz.getName())){
value=rs.getBlob(field.getName()).getBinaryStream();
}
}else if("oracle.sql.CLOB".equals(dbType.getName())){//對應oracle的CLOB
if("java.lang.String".equalsIgnoreCase(fieldTypeClazz.getName())){
value=rs.getClob(field.getName()).toString();
}else if("java.io.Reader".equalsIgnoreCase(fieldTypeClazz.getName())){
value=rs.getClob(field.getName()).getCharacterStream();
}
}else if("java.sql.Timestamp".equals(dbType.getName())){//對應oralce date
}else if("[B".equals(dbType.getName())){
}else if("oracle.sql.TIMESTAMPTZ".equals(dbType.getName())//對應oralce的timestamp_with_tim
||"oracle.sql.TIMESTAMPLTZ".equals(dbType.getName())//對應oralce的timestamp_with_local
||"oracle.sql.TIMESTAMP".equals(dbType.getName())){//對應oracel的timestamp
if("java.sql.Timestamp".equals(fieldTypeClazz.getName())){
value=rs.getTimestamp(field.getName());
}
}
}
field.set(entity, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return entity;
}
public Field[] getWholeFields(Class<?> clazz){
Field[] result=clazz.getDeclaredFields();
Class<?> superClass=clazz.getSuperclass();
while(superClass!=null){
Field[] tempField=superClass.getDeclaredFields();
Field[] tempResult=new Field[result.length+tempField.length];
for(int i=0;i<result.length;i++){
tempResult[i]=result[i];
}
for(int i=0;i<tempField.length;i++){
tempResult[result.length+i]=tempField[i];
}
result=tempResult;
superClass=superClass.getSuperclass();
}
return result;
}