您的位置:首页 > 编程语言 > Java开发

java反射机制set方法 连接数据库(还有待修改,没有循环ResultSet对象)

2011-01-17 17:58 417 查看
public ArrayList query() {
String methodname;
ArrayList list=new ArrayList();
try {
con=db.getCon();
ps=con.prepareStatement(this.getQuery());
for(int i=0;i<this.getFiled().length-1;i++){
//System.out.println("列"+this.getFiled()[i].getName());
//System.out.println("方法名:"+methodname);
if(this.getFiled()[i].getGenericType().toString().equals("int")){
methodname="set"+this.getFiled()[i].getName().substring(0,1).toUpperCase()+this.getFiled()[i].getName().substring(1,this.getFiled()[i].getName().length())
+"("+rs.getInt(this.getFiled()[i].getName())+")";
}else{
// System.out.print("字符");
methodname="set"+this.getFiled()[i].getName().substring(0,1).toUpperCase()+this.getFiled()[i].getName().substring(1,this.getFiled()[i].getName().length())
+"("+rs.getString(this.getFiled()[i].getName())+")";

}
Method me= this.getBean().getClass().getDeclaredMethod(methodname,new Class[]{});
me.invoke(this.getBean(),new Object[]{});

//System.out.print(this.getFiled()[i].getGenericType());
list.add(this.getBean());
}
// System.out.print("list大小"+list.size());
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
这是我写的通用代码,执行查询方法,获得查询语句如下
public String getQuery() {
if(this.getId()>0){
this.setQuery("select * from "+this.getTablename());
}else if(this.colname!=null){
String colvalue=null;
for(int i=0;i<this.invokeGet().size()-1;i++){
if(this.getFiled()[i+1].getName().toLowerCase().equals(this.colname.toLowerCase())){
colvalue=this.invokeGet().get(i).toString();
}
}
this.setQuery("select * from "+this.getTablename()+" where "+this.colname+"='"+colvalue+"'");
}
else{
this.setQuery("select * from "+this.getTablename());
}
return query;
}

上述语句获得查询语句肯定是没有问题,问题出现在给methodname赋値,也就是set方法的名称获得就出错,执行也有疑问,我执行get方法的时候不需要带()只需要方法名,可是用set方法,必须得要括号,因为要把rs包含起来,怎么办?

问题补充 2009-06-17 14:45
如果换个角度,只获得set方法的方法名,执行的时候会因为没有参数报错,那么set方法的参数何时加载?

问题补充 2009-06-17 14:49
我试试,如果成功就采纳,失败再看

问题补充 2009-06-17 14:57
java.lang.NoSuchMethodException: com.itcase.dao.Userinfo.setId(java.lang.Integer)
怎么搞?

问题补充 2009-06-17 15:58
me.invoke(this.getBean(),new Object[]{methodname,rs.getInt(col)}); 里面的两个参数,都是有値的比如 methodname 是setId 値是:123456 执行还是不行啊,怎么搞,急,在线等

//反射机制取得带参数的方法名时,必须给定参数类型。
//假如类BorderTest声明方法 setBeanValue(String v,int id),则反射调用需要以下步骤
BorderTest bt = new BorderTest();
try {
//取得方法签名
Method me = BorderTest.class
.getDeclaredMethod("setBeanValue",
new Class[]{java.lang.String.class,
java.lang.Integer.class
});
//传递参数调用方法
me.invoke(bt, new Object[]{"strvalue",1});
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}


回答人的补充 2009-06-17 15:03
说明你的 com.itcase.dao.Userinfo类并没有setId(java.lang.Integer)这个方法?
原形是不是setId(int id)?
如果是的话,使用getDeclaredMethod("setId",new Class[]{int.class});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐