【程序编程相关:轻松实现Java 用户界面编程】
【推荐阅读:24点算法的java代码】首先介绍一下整个流程:【扩展信息:建立对象数据库-内存映射范式,需要中间层】
第一步:因为需要从数据库中取出查询的结果并显示在网页上,所以必须先写一个查询数据库的类,我们把它命名为article.java,里面有一个query()方法,用它来处理查询,代码如下:package com.swsoftware.beansimport java.sql.connection;import java.sql.preparedstatement;import java.sql.resultset;import java.sql.sqlexception;import java.util.arraylist;import java.util.list;import org.apache.commons.logging.log;import org.apache.commons.logging.logfactory;import com.swsoftware.hljhz.admin.forms.articleform;//struts中的form类,此处是有关article的formimport com.swsoftware.core.db.dbutils;//此外引用的类dbutils中无非是一些连接数据库//用的代码,所以不做介绍了
public class article{ protected log log = logfactory.getlog(this.getclass()); private articleform articleform = null; public list query(string where) throws sqlexception { list list = new arraylist(); connection connection = null; preparedstatement statement = null; resultset rs = null; stringbuffer buf = new stringbuffer(); buf.append("select * from article order by articleid desc "); try { connection = dbutils.getconnection();//dbutils中的方法,取得连接用 statement = connection.preparestatement(buf.tostring()); rs = statement.executequery(); while (rs.next()) { articleform = new articleform(); articleform.setarticleid(rs.getint("articleid")); articleform.setcontent(rs.getstring("content")); articleform.settitle(rs.getstring("title")); list.add(articleform); } return list; } catch (sqlexception sqle) { log.error("sql execution error!", sqle); throw new sqlexception("error executing sql in " + this.getclass().getname()); } finally { dbutils.close(rs, statement, connection);// dbutils中的方法,用以关闭连接 } }}
第二步:由于前面引用了articleform,所以这里给出了articleform的代码:package com.swsoftware.formsimport org.apache.struts.validator.validatorform;public class articleform extends validatorform{ private int articleid = 0;private string title = "";private string content = "";
public int getarticleid() { return articleid; }
public void setarticleid(int a) { this.articleid = a; }
public string gettitle() { return title; }
public void settitle(string t) {... 下一页