利用类反射机制简化Struts操纵程序开辟[Java编程]
本文“利用类反射机制简化Struts操纵程序开辟[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
1、先定义Action FormBean:
package com.bhsky.webis.system;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class UsersActionForm extends ActionForm {
private String usr_id;
private String usr_name;
public void setUsr_id(String usr_id) {
this.usr_id = usr_id;
}
public String getUsr_id() {
return usr_id;
}
public String getUsr_memo() {
return usr_memo;
}
public void setUsr_name(String usr_name) {
this.usr_name = usr_name;
}
}
2、编写通用的为ActionFormBean赋值的办法:
//Function: 完成ResultSet对象向ArrayList对象为调集的对象的转化
//Para:sql,指定的查询Sql
//Para:className,Sql相对应得JavaBean/FormBean类的名字
//Return:以类className为一条记录的后果集,
//完成ResultSet对象向ArrayList对象为调集的className对象的转化
public ArrayList Select(String sql,String className){
ArrayList paraList=new ArrayList();
try{
if (conn == null){
Connection();
}
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
String recordValue="";
Object c1=null;
paraList=new ArrayList();
ResultSetMetaData rsmd = rs.getMetaData();
int columnCount = rsmd.getColumnCount();
while (rs.next()){
c1=Class.forName(className).newInstance();
for (int i=1; i<=columnCount; i++) {
if(rs.getString(rsmd.getColumnName(i))!=null){
recordValue=rs.getString(rsmd.getColumnName(i));
}else{
recordValue="";
}
Method
m=c1.getClass().getMethod(getSetMethodName(rsmd.getColumnName(i)),
new Class[]{recordValue.getClass()});
m.invoke (c1, new Object[]{recordValue});
}
paraList.add(c1);
}
}catch(SQLException ex){
}catch(ClassNotFoundException e){
}catch(NoSuchMethodException e) {
}catch(InvocationTargetException e){
}catch (IllegalAccessException e){
}catch(InstantiationException e){
} finaly{
closeConnection();
return paraList;
}
}
以上是“利用类反射机制简化Struts操纵程序开辟[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |