com.mycomm.dao.dao4comm.util.InstanceBuilder Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycomm.dao.dao4comm.util;
import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.IProtocol.sql.annotation.MyColumn;
import com.mycomm.IProtocol.sql.annotation.MyId;
import com.mycomm.dao.dao4comm.annotation.dialect.FieldTypeDetector;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author jw362j
*/
public class InstanceBuilder {
private static final Logger log = LoggerFactory.getLogger(InstanceBuilder.class);
public static void buildInstance(ResultSet rs, Object instance, Field[] fs, String tableIdColumName) {
try {
for (Field f : fs) {
f.setAccessible(true);
if (f.isAnnotationPresent(MyId.class)) {
//select id and give value to id field
try {
log.info("tableIdColumName:"+tableIdColumName+",class:"+instance.getClass());
f.set(instance, rs.getLong(tableIdColumName));
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
JDataTypes jDataType = FieldTypeDetector.getDataType(f);
// log.info("=========sss======>" + jDataType);
// if (JavaDataTypes.JNull.equals(jDataType)) {
// continue;
// }
if (f.isAnnotationPresent(MyColumn.class)) {
MyColumn myColumn = (MyColumn) f.getAnnotation(MyColumn.class);
String columName = myColumn.ColumnName();
if ("".equals(columName) || columName == null) {
columName = f.getName();
}
//i got the columName now ,so i can load the data from jdbc resultset,but i need data type also
if (JDataTypes.JByte.equals(jDataType)) {
try {
f.setByte(instance, rs.getByte(columName));
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JShort.equals(jDataType)) {
try {
short s = rs.getShort(columName);
f.setShort(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JFloat.equals(jDataType)) {
try {
float s = rs.getFloat(columName);
f.setFloat(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JDouble.equals(jDataType)) {
try {
double s = rs.getDouble(columName);
f.setDouble(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JInt.equals(jDataType)) {
try {
int s = rs.getInt(columName);
f.setInt(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JLong.equals(jDataType)) {
try {
long s = rs.getLong(columName);
f.setLong(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JBoolean.equals(jDataType)) {
try {
f.setBoolean(instance, rs.getBoolean(columName));
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JString.equals(jDataType)) {
try {
String s = rs.getString(columName);
f.set(instance, s);
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JChar1.equals(jDataType)) {
try {
if (rs.getString(columName) != null) {
f.setChar(instance, rs.getString(columName).charAt(0));
}
} catch (SQLException e) {
log.error(e.getMessage());
continue;
}
}
if (JDataTypes.JDate.equals(jDataType)) {
try {
if (rs.getTimestamp(columName) != null) {
f.set(instance, new Date(rs.getTimestamp(columName).getTime()));
}
} catch (SQLException e) {
log.error(e.getMessage());
}
}
}
}
} catch (IllegalAccessException ex) {
log.error(ex.getMessage());
}
}
}