package chs.jhsyss.form;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
public class Waper {
public Waper(){
}
/**
* 本方法用于将FORM对象组装成一条INSERT语句
* @param obj ActionForm 以及其子对象
* @param formPath ActionForm 以及其子对象的包路径和名称
* @param tableName 所要操作的数据表
* @return 返回相应的INSERT语句
*/
public String insert(Object obj,String formPath,String tableName){
long start = new Date().getTime();
String sqlToVar = "insert into "+tableName+"(";
String sqlToValues = " ) values(";
Class $fj = null;
try {
$fj = Class.forName(formPath);//example "chs.jhsyss.form.FJhsyss"
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Class clazz = obj.getClass();
Method[] method = clazz.getDeclaredMethods();
for(int i=0;i<method.length;i++){
String methodName = method[i].getName();
if(methodName.indexOf("get")<0){
continue;// 除去非get方法
}else{
try {
Method m = $fj.getMethod(methodName, null);//根据指定的方法名取得相应方法
String $methodName = methodName.toLowerCase().substring(3,methodName.length());//裁剪getXXX方
法取得相应变量名称
try {
String result = (String) m.invoke(obj, null);//调用方法,前一个参数代表传进来的FORM对
象,后一个为传递给方法的参数;返回结果为原来方法相应的返回值
sqlToVar+=$methodName+",";
sqlToValues+="'"+result+"',";
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sqlToVar=sqlToVar.substring(0,sqlToVar.length()-1);
sqlToValues=sqlToValues.substring(0,sqlToValues.length()-1)+")";
long end = new Date().getTime();
System.out.println("插入操作花费总时间->"+(end-start)+"<-ms");
System.out.println(sqlToVar+sqlToValues);
return sqlToVar+sqlToValues;
}
public String update(Object obj,String formPath,String xh,String tableName){
long start = new Date().getTime();
String sqlToVar = "update "+tableName+" set ";
Class $clazz = null;
try{
$clazz = Class.forName(formPath);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
Class clazz = obj.getClass();
Method[] method = clazz.getDeclaredMethods();
for(int i = 0;i<method.length;i++){
String methodName = method[i].getName();
if(methodName.indexOf("get")<0){
continue;// 除去非get方法
}else{
try {
Method m = $clazz.getMethod(methodName, null);
String $methodName = methodName.toLowerCase().substring(3,methodName.length());//裁剪
getXXX方法取得相应变量名称
String result = (String) m.invoke(obj, null);
sqlToVar+=$methodName+"='"+result+"',";
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//根据指定的方法名取得相应方法
catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
sqlToVar=sqlToVar.substring(0,sqlToVar.length()-1)+" where xh="+xh;
long end = new Date().getTime();
System.out.println("更性操作花费总时间->"+(end-start)+"<-ms");
System.out.println("SQL->"+sqlToVar);
return sqlToVar;
}
public static void main(String[] args){
FJhsyss fj = new FJhsyss();
fj.setBcrq("20050202");
fj.setDwfzr("fafa");
fj.setGnjygr("fafa");
fj.setGnjyls("2000");
fj.setXh("seq_hqjc_jhsyss,");
String seq = "seq_hqjc_jhsyss.nextval";
String path = "chs.jhsyss.form.FJhsyss";
Waper r = new Waper();
r.insert(fj,path,"MYTABLE");
r.update(fj, path, "1", "mytable");
}
}