1、何为VO和DAO
VO就是一个普通的JavaBean,而对于JavaBean的一个重要的应用就是将数据库查询的代码,从jsp中移植到JavaBean中。使jsp作为真正的表示层,通过在jsp中调用这个特定的类提供的方法,去对数据库进行操作
DAO叫做Data Access Object类,专门负责对数据库的访问,显然,可以将数据库查询的代码写在DAO 中,通常情况下,可以使用VO(value object)来配合DAO来使用,在DAO中,可以每查询到一条记录,就将其封装为VO中的对象,最后将所有实例化的对象放回到一个集合内进行返回,这样就可以实现层次的分开,降低耦合度,在使用DAO时,必须要导入相应的包才能够实现,接着就可以使用DAO类进行实例对数据库的访问
2、实现:
下面我们实现使用VO和DAO对数据的访问,首先我们确定工作原理,需要将数据库的操作都封装在DAO内,把从数据库查询到的信息实例化为VO,放到ArrayList数组里返回,DAO类的代码如下(此代码用于对Oracle数据库的访问,并且表newmessage结构已构建,实现的功能是基本的新闻发布系统)建立一个类,将其命名为NewsRealese,下图为部分文件的布局,如下:
图 建立VO与DAO的部分类
package dao; import com.lut.beans.NewsRealese; import static java.lang.System.out; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; public class NewsRealeseDao { public ArrayList queryAllNews() throws Exception { Connection conn = null; ArrayList newsRealese = new ArrayList(); try { //获取连接 Class.forName("oracle.jdbc.driver.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:orcl"; conn = DriverManager.getConnection(url, "scott", "wjk139"); //运行SQL语句 String sql = "select * from newmessage";//获取 Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery(sql); while (rs.next()) { //实例化VO NewsRealese news = new NewsRealese(); news.setNewsId(rs.getString("newsid")); news.setClassId(rs.getString("CLASSID")); news.setKindId(rs.getString("KINDID")); news.setMyOther(rs.getString("MYOTHER")); news.setHeadTitle(rs.getString("HEADTITLE")); news.setContent(rs.getString("CONTENT")); news.setConnectRealtive(rs.getString("CONNECTREALTIVE")); news.setAuthor(rs.getString("AUTHOR")); news.setEditor(rs.getString("EDITOR")); news.setNewsFrom(rs.getString("NEWSFROM")); news.setTop(rs.getString("TOP")); news.setNewsTime(rs.getString("NEWSTIME")); news.setHits(rs.getString("HITS")); news.setState(rs.getString("STATE")); news.setTag(rs.getString("TAG")); newsRealese.add(news); } rs.close(); stat.close(); } catch (Exception e1) { e1.printStackTrace(); } finally { try {//关闭连接 if (conn != null) { conn.close(); conn = null; } } catch (Exception ex) { } return newsRealese; } }}下面就是实现VO的代码示例,其中各项都是数据库里边的表项内容,集定义了一个基本的新闻属性要素:,使用封装函数进行封装即可,在这里我们建立一个JavaBean类命名为NewsRealese,并且将其部署到web.xml中
package com.lut.beans; public class NewsRealese { private String newsId; private String classId; private String kindId; private String myOther; private String headTitle; private String content; private String connectRealtive; private String author; private String editor; private String newsFrom; private String top; private String newsTime; private String hits; private String state; private String tag; public String getNewsId() { return newsId; } public void setNewsId(String newsId) { this.newsId = newsId; } public String getClassId() { return classId; } public void setClassId(String classId) { this.classId = classId; } public String getKindId() { return kindId; } public void setKindId(String kindId) { this.kindId = kindId; } public String getMyOther() { return myOther; } public void setMyOther(String myOther) { this.myOther = myOther; } public String getHeadTitle() { return headTitle; } public void setHeadTitle(String headTitle) { this.headTitle = headTitle; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getConnectRealtive() { return connectRealtive; } public void setConnectRealtive(String connectRealtive) { this.connectRealtive = connectRealtive; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getEditor() { return editor; } public void setEditor(String editor) { this.editor = editor; } public String getNewsFrom() { return newsFrom; } public void setNewsFrom(String newsFrom) { this.newsFrom = newsFrom; } public String getTop() { return top; } public void setTop(String top) { this.top = top; } public String getNewsTime() { return newsTime; } public void setNewsTime(String newsTime) { this.newsTime = newsTime; } public String getHits() { return hits; } public void setHits(String hits) { this.hits = hits; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } }
<%@page contentType="text/html" import="java.util.*,java.sql.*,com.lut.beans.NewsRealese,dao.NewsRealeseDao" pageEncoding="UTF-8" language="java"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>新闻发布系统--最前沿的时尚信息、最有看点的社会聚焦、最富得浪漫的殿堂</title> <link type="text/css" rel="stylesheet" href="newsRealese.css"/> </head> <body id="body"> <a href="adminLogin.jsp" style="table-layout: right">管理员登录</a><h1 id="p">最新资讯</h1><br> <table border="0" style="width:100%; height:100%" align="center"> <tr> <td style="width: 10%"> </td> <td> <% NewsRealeseDao newsRealeseDao = new NewsRealeseDao(); ArrayList newsRealese = newsRealeseDao.queryAllNews(); %> <% for (int i = 0; i < newsRealese.size(); i++) { NewsRealese newRealese = (NewsRealese) newsRealese.get(i); %> <center><table border="0" width="100%" id="table"> <tr> <td width="128"><p>此处假设无图片</p> <p>此处假设无等级信息</p> </td> <td ><table border="0" width="100%"> <tr> <td width="11%" >原创或转载:</td> <td colspan="3"><%=newRealese.getMyOther()%> </td> <td width="10%" >新闻标题:</td> <td width="44%" ><%=newRealese.getHeadTitle()%> </td> </tr> <tr> <td colspan="6">新闻内容:</td> </table> <table width="100%" border="0"> <tr> <td> <a target="_blank" href="newRealese_detail.jsp?newsid=<%=newRealese.getNewsId()%>"/> <table width="100%" border="0" align="center"> <tr> <td> <%=newRealese.getContent()%> </td> </tr> </table> </td> </tr> </table> <table border="0" width="100%"> <tr> <td width="51" height="30">作者:</td> <td width="203"><%=newRealese.getAuthor()%> </td> <td width="97">新闻发布时间:</td> <td width="167"><%=newRealese.getNewsTime()%> </td> <td width="99">新闻点击次数:</td> <td width="191"><%=newRealese.getHits()%> </td> </tr> </table></td> </tr> <tr> <td height="21" colspan="2"><hr></td> </tr> </table></center> <% } %> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> <td> </td> </tr> </table> </body> </html>
不过,用了DAO和VO,似乎也没有从jsp页面中完全消除Java代码,但是与之前直接写JDBC代码来讲,还是好多了,另外在jsp页面没有出现任何与jdbc相关的代码,并且由于编程人员不需要知道数据库的细节和构架,便于分工开发
以上就是使用DAO和VO实现对数据库的访问。