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 执行还是不行啊,怎么搞,急,在线等
回答人的补充 2009-06-17 15:03
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});
相关文章推荐
- [转]"此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- SQL2005,修改数据库对象的构架(即所有者)的方法
- "此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- "此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- plsql development工具在本地没有oracle数据库情况下连接远程服务器数据库的方法
- 动态修改Web.config中数据库连接字符串的方法
- sqlserver2005"此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- "此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- JDBC连接数据库(适当的封装查询、新增修改删除)的方法
- "此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- 循环体内包含了大量没有必要在循环中处理的语句或获取数据库连接或进行不必要的try-catch操作
- “数据库关系图”提示:此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- Java_jdbc 基础笔记之八 数据库连接(写一个查询Student对象的方法)
- 数据库关系图:“此数据库没有有效所有者,因此无法安装数据库关系图支持对象"的解决方法
- "此数据库没有有效所有者,因此无法安装数据库关系图支持对象" 解决方法
- jsp中上传大文件(200M已测试),存到数据库,易发生连接超时的ie找不到网页的错误,其解决方法。略写代码,参考(.net修改代码应该可以,改天修改)
- pdo连接数据库及pdo对象的方法介绍
- ADO.NET笔记——使用Connection连接数据库,使用Command对象的ExecuteReader()方法创建DataReader对象返回多行数据
- Hibernate的方法获取对象后,对象调用set后会自动更新数据库内容的解决办法
- PHP中用set_time_limit(0)长连接的实现--更新数据库时间限制中断解决方法