最简单的方法,还是将类当作一个javabean来调用
解决方案 »
- sping无法注入值,但是直接从配置文件读取又可以
- 请问:在weblogic923下读取中文数字证书主题的时候,却出来的中文为“?”,怎么解决?谢谢
- 运行hibernate报错!请求帮忙!
- 大家帮我看看。这个到底是为什么啊?
- 紧急,请高人赐一段jasperreport输出到excel的代码
- JAVA EE真实企业级应用界面,看上去很爽,真想自己做一个
- java specification到底看哪个?
- SSH AOP事务配置求解
- JSONException: A JSONObject text must begin with '{' at character 1 of 2
- 在进行数据查询时,查询不出结果
- cvs问题请教
- tomcat 最新是什么版本 ?在哪里下载啊?请给我直接下载地址。我重新装个试试!!
private String name="tomuno";
private String age="100";
private String id="8888";
public String getId(){
return this.id;}
public String getName(){
return this.name;}
public String getAge(){
return this.age;}
}
public class Smalldeer extends Action{
....
Tomuno tomuno=new Tomuno();
String id=(String)request.getParameter("id");
String mapping=null;
String name;
String age; if(id.equals(tomuno.getId())){
mapping="success";
name = tomuno.getName();
age = tomuno.getAge();
request.getSession.setAttribute("name",name);
request.getSession.setAttribute("age",age);
}
else{mapping="fail";} return actionmapping.findForward(mapping);
}struts-config.xml
<action path="/tomuno" type="Tomuno">
<forward name="success" path="/smalldeer.jsp">
<forward name="fail" path="http://www.tom.com">
</action>tomuno.jsptomuno.do?id=8888smalldeer.jsp
<font color="blue">
My name is:
<%=session.getAttribute("name")%>
<br>
and my age is;
<%=session.getAttribute("age")%>
</font>
首先生成关于这条记录的简单Bean;
jb和Eclipse都有自动生成的功能;例如:public class MagModuleBean implements Serializable {
private String moduleCode;
private String moduleName;
private String levelCode;
private String isShow;
public String getModuleCode() {
return moduleCode;
}
public void setModuleCode(String moduleCode) {
this.moduleCode = moduleCode;
}
public String getModuleName() {
return moduleName;
}
public void setModuleName(String moduleName) {
this.moduleName = moduleName;
}
public String getLevelCode() {
return levelCode;
}
public void setLevelCode(String levelCode) {
this.levelCode = levelCode;
}
public String getIsShow() {
return isShow;
}
public void setIsShow(String isShow) {
this.isShow = isShow;
}
private void writeObject(ObjectOutputStream oos) throws IOException {
oos.defaultWriteObject();
}
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
ois.defaultReadObject();
}
如果你要显示多条记录,那把bean都放在ArrayList里面不是更好么~
解决的方法是使用类映射机制;
/////////////////////////////////////////////////////////////////////////////
//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;
}
这个是个简单的例子;里面的用ArrayList封装可按照你的要求封装,但是有个问题也不好处理;
bean的属性名和数据库里面的字段名称对应的问题!这个还是要注意的。如果你嫌上面写的麻烦,可以到Apache的网站上下载一个类包叫做dbutils 1.0.jar
他的BasicRowProcessor类有个方法,以下是他的简介:
toBeanList
public java.util.List toBeanList(java.sql.ResultSet rs,
java.lang.Class type)
throws java.sql.SQLException
Convert a ResultSet into a List of JavaBeans. This implementation uses reflection and BeanInfo classes to match column names to bean property names. Properties are matched to columns based on several factors: The class has a writable property with the same name as a column. The name comparison is case insensitive.
The property's set method parameter type matches the column type. If the data types do not match, the setter will not be called.
Primitive bean properties are set to their defaults when SQL NULL is returned from the ResultSet. Numeric fields are set to 0 and booleans are set to false. Object bean properties are set to null when SQL NULL is returned. This is the same behavior as the ResultSet get* methods.
Specified by:
toBeanList in interface RowProcessor
Returns:
A List of beans with the given type in the order they were returned by the ResultSet.
Throws:
java.sql.SQLException
See Also:
RowProcessor.toBeanList(java.sql.ResultSet, java.lang.Class)
看到是否是你想要的呢?其实他封装的操作和上面写的实例原理是一样的。
好了,希望对楼主有所帮助!