使用JDBC操作数据库时挺方便的,暂时功能简单. .以后有时间还会完善。
如大家有指教请留言。
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 对SQL的数据库部分操作
* @author 卓越
* @version 1.1
* {@value}
*/
public class RFM {
public static void main(String[] args) throws Exception{
A a = new A("v1","v2","v3");
System.out.println("getFields(a): "+getFields(a));
System.out.println("getValues(a): "+getValues(a));
// fields是String类型
System.out.println("getValues(a, fields): "+getValues(a,getFields(a)));
System.out.println("updateSQL(a): "+updateSQL(a));
System.out.println("===========================================");
//有3个field,此处是为了观察有空field的效果
A a2 = new A("v1","v2","v3","v4");
System.out.println("getFields(a2): "+getFields(a2));
System.out.println("getValues(a2): "+getValues(a2));
// fields是String类型
System.out.println("getValues(a2, fields): "+getValues(a2,getFields(a2)));
System.out.println("updateSQL(a2): "+updateSQL(a2));
}
/**
* 得到javabean中所有的field名,并用","连接成为String类型的对象。格式:" c1 , c2 , c3 , c4 "
* 用于省略insert SQL字段部分的拼写
* @author 卓越
* @param object
* - javabean对象
* @return fields - 格式:" c1 , c2 , c3 , c4 "
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static String getFields(Object object) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException{
Field[] field = object.getClass().getDeclaredFields();
StringBuffer fieldBuffer = new StringBuffer();
for(int i=0;i<field.length;i++){
Method method = object.getClass().getMethod(getGetMethodName(field[i].getName()));
Object value = method.invoke(object);
if(isNull(value)){
continue;
}
fieldBuffer.append(" ").append(field[i].getName().toString()).append(" ,");
}
String fields = fieldBuffer.substring(0, fieldBuffer.length()-1);
return fields;
}
/**
* 利用getFields(Object object)方法得到javabean中所有的field的字符串,再通过field名找到所有对应的getter方法并依次
* 取出field值,并用","连接成为String类型的对象.格式:" 'v1' , 'v2' , 'v3' , 'v4' ".
* 用于省略insert SQL语句中值部分的拼写,建见与getFields(Object object)配合使用,以保障字段和值的顺序可以对确对应.
* 也可使用getValues(Object object,String fields)方法,此方法可彻底解决字段与与值对应问题.
* @param object
* - javabean对象
* @return values - 格式:" 'v1' , 'v2' , 'v3' , 'v4' "
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static String getValues(Object object) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException{
String fields = getFields(object);
String[] field = fields.split(",");
StringBuffer valueBuffer = new StringBuffer();
for(int i=0;i<field.length;i++){
Method method = object.getClass().getMethod(getGetMethodName(field[i].trim()), new Class[]{});
Object value = method.invoke(object);
if(isNull(value)){
continue;
}
valueBuffer.append(" '").append(value).append("' ,");
}
String values = valueBuffer.substring(0, valueBuffer.length()-1);
return values;
}
/**
* 利用object中的fields名字的拼接串来得到对应getter方法,再通过getter方法来取得
* 相应field的值,并用","连接成为String类型的对象.格式:" 'v1' , 'v2' , 'v3' , 'v4' ".
* 用于省略insert SQL语句中值部分的拼写.
*
* @param object
* - javabean对象
* @param fields
* - 此参数要是object对象中field的拼接串. 格式:" c1 , c2 , c3 , c4 "
* @return values - 格式:" 'v1' , 'v2' , 'v3' , 'v4' "
* @throws NoSuchMethodException
* @throws IllegalArgumentException
* @throws InvocationTargetException
*/
public static String getValues(Object object,String fields) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException{
String[] field = fields.split(",");
StringBuffer valueBuffer = new StringBuffer();
for(int i=0;i<field.length;i++){
Method method = object.getClass().getMethod(getGetMethodName(field[i].trim()));
Object value = method.invoke(object);
if(isNull(value)){
continue;
}
valueBuffer.append(" '").append(value).append("' ,");
}
String values = valueBuffer.substring(0, valueBuffer.length()-1);
return values;
}
/**
* 用于省略update SQL语句的set部分.
*
* @param object
* - javabean对象
* @return sqlString - 格式:" c1='v1' , c2='v2' , c3='v3' , c4='v4'"
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static String updateSQL(Object object) throws NoSuchMethodException,
IllegalAccessException, InvocationTargetException{
Field[] field = object.getClass().getDeclaredFields();
StringBuffer sqlBuffer = new StringBuffer();
for(int i=0;i<field.length;i++){
Object vString = object.getClass().getMethod(
getGetMethodName(field[i].getName()))
.invoke(object);
if(isNull(vString)){
continue;
}
sqlBuffer.append(" ").append(field[i].getName().toString())
.append("=").append("'").append(vString).append("' ,");
}
String sqlString = sqlBuffer.substring(0, sqlBuffer.length()-1);
return sqlString;
}
/**
* 将field名改为其getter方法名
* @param field
* @return
*/
public static String getGetMethodName(String field) {
return "get"+field.substring(0, 1).toUpperCase()+field.substring(1);
}
/**
* 将field名改为其setter方法名
* @param field
* @return
*/
public static String getSetMethodName(String field) {
return "set"+field.substring(0, 1).toUpperCase()+field.substring(1);
}
/**
* 判断JavaBean的field是否为空.
* @param field
* @return flag - 为空返回True
*/
public static boolean isNull(Object field) {
boolean flag=false;
if(field==null||field.toString().trim().equalsIgnoreCase("")){
flag=true;
}
return flag;
}
}
======================================================================
javabean:
public class A {
String c1;
String c2;
String c3;
String c4;
/**
* 有4个field的构造方法
* @param c1
* @param c2
* @param c3
* @param c4
*/
public A(String c1, String c2, String c3, String c4) {
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
this.c4 = c4;
}
/**
* 有3个field,此处是为了观察有空field的效果
* @param c1
* @param c2
* @param c3
*/
public A(String c1, String c2, String c3) {
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
public String getC1() {
return c1;
}
public void setC1(String c1) {
this.c1 = c1;
}
public String getC2() {
return c2;
}
public void setC2(String c2) {
this.c2 = c2;
}
public String getC3() {
return c3;
}
public void setC3(String c3) {
this.c3 = c3;
}
public String getC4() {
return c4;
}
public void setC4(String c4) {
this.c4 = c4;
}
}
@@@