利用反射自动拼接增加的sql工具类



package test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import com.uplooking.club.vip.entity.Vip;

public class testCommonDao {

/*

* 获取set或get方法的函数

*/

public String getSetorGetMethod(String classfield,String setorget){

if (setorget.equals(“set”)||setorget.equals(“get”)) {

char cur=classfield.charAt(0);

cur=(char) (cur-32);

classfield=setorget+cur+classfield.substring(1);

return classfield;

}

return null;

}

@Test

public void testgetSetorGet(){

System.out.println(getSetorGetMethod(“name”, “set”));

}

/*

* 获取表名

* 先获取类名,然后从最后一个“.”+1的位置取到结束

*/

@Test

public void testStringApi(){

String clz=“com.uplooking.club.vip.entity.Vip”;

String sql=“insert into “+clz.substring(clz.lastIndexOf(”.”)+1);

System.out.println(sql);

}

/**

* 工具类dbutilDao

* @throws SecurityException

* @throws NoSuchMethodException

* @throws InvocationTargetException

* @throws IllegalArgumentException

* @throws IllegalAccessException

* @throws ClassNotFoundException

* @throws SQLException

*/

public void dbUtilDao(Object obj) throws NoSuchMethodException, SecurityException, IllegalAccessException,

IllegalArgumentException, InvocationTargetException, ClassNotFoundException, SQLException{

Connection conn=null;

Class claz = obj.getClass();

Field[] declaredFields = claz.getDeclaredFields();

String sql=“insert into “+claz.getName().substring(claz.getName().lastIndexOf(”.”)+1);

String column="(";

String values=“values(”;

for(Field field:declaredFields){

Object resultvalue=null;

if(field.getType().getSimpleName().equals(“String”)){

String methodname=getSetorGetMethod(field.getName(), “get”);//获得getName;

Method method = claz.getMethod(methodname);

resultvalue=method.invoke(obj);

}

if(field.getType().getSimpleName().equals(“Integer”)){

String methodname=getSetorGetMethod(field.getName(), “get”);

Method method = claz.getMethod(methodname);

resultvalue=method.invoke(obj);

}

if(resultvalue!=null){

column=column+field.getName()+",";

values=values+"?,";

}
}

sql=sql+column.substring(0,column.length()-1)+") “+values.substring(0,values.length()-1)+”)";

Class.forName(“com.mysql.jdbc.Driver”);

conn=DriverManager.getConnection(“jdbc:mysql://127.0.0.1/uplooking1?

useUnicode=true&characterEncoding=utf-8”,“root”,"");

PreparedStatement pst=conn.prepareStatement(sql);

int x=1;

for(Field field:declaredFields){

if(field.getType().getSimpleName().equals(“String”)){

String methodName=getSetorGetMethod(field.getName(),“get”);

Method method = claz.getMethod(methodName);

if(method.invoke(obj)==null)

continue;

pst.setString(x, (String)method.invoke(obj));

x++;

}

if(field.getType().getSimpleName().equals(“Integer”)){

String methodName=getSetorGetMethod(field.getName(), “get”);

Method method = claz.getMethod(methodName);

if(method.invoke(obj)==null)

continue;

pst.setInt(x, (Integer)method.invoke(obj));

x++;

}
}

pst.execute();

conn.close();

}

}