准备工作:
编写数据库脚本,准备数据。
View Code
1 create table Info(
2 id bigint not null auto_increment primary key,
3 name varchar(20) not null,
4 age int not null,
5 email varchar(30) not null,
6 job varchar(20) not null,
7 employ DATE not null
8 );
9
10 insert into Info(name,age,email,job,employ) values('张三','20','xxx_00180@qq.com','软件工程师','2001-06-04');
11 insert into Info(name,age,email,job,employ) values('李四','22','45221@qq.com','测试工程师','2010-08-05');
12 insert into Info(name,age,email,job,employ) values('王武','33','3435345@qq.com','售前工程师','2008-12-16');
13 insert into Info(name,age,email,job,employ) values('赵柳','44','7574356@qq.com','售后工程师','2004-07-09');
14 insert into Info(name,age,email,job,employ) values('孙琦','59','3425475@qq.com','项目经理','1991-10-08');
15 insert into Info(id,name,age,email,job,employ) values(6,'赵八','36','74812@qq.com','总经理','1990-12-08');
1.先编写VO类Info
package cn.xiong.daostudy.vo; import java.util.Date; public class Info { private int id; private String name; private int age; private String email; private String job; private Date employ; public Info() { super(); } public Info(int id, String name, int age, String email, String job, Date employ) { super(); this.id = id; this.name = name; this.age = age; this.email = email; this.job = job; this.employ = employ; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Date getEmploy() { return employ; } public void setEmploy(Date employ) { this.employ = employ; } }
2,创建数据库操作类DatabaseConnection,主要负责数据库连接及关闭。
package cn.xiong.daostudy.database; import java.sql.Connection; import java.sql.DriverManager; public class DatabaseConnection { private final String DBDRIVER="org.gjt.mm.mysql.Driver"; private final String DBURL="jdbc:mysql://localhost:3306/study_java"; private final String DBUSER="root"; private final String DBPASSWORD="mysqladmin"; private Connection conn=null; public DatabaseConnection() { try { Class.forName(DBDRIVER); } catch(Exception ex) { ex.printStackTrace(); } try { this.conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD); } catch(Exception ex) { ex.printStackTrace(); } } public Connection getConnection() { return this.conn; } public void close() { try { this.conn.close(); } catch(Exception ex) { ex.printStackTrace(); } } }
3,定义接口IDao,操作数据库
package cn.xiong.daostudy.dao; import cn.xiong.daostudy.vo.Info; import java.util.List; public interface IDao { public boolean doInsert(Info info) throws Exception; public Info findById(int id) throws Exception; public List<Info> findAll() throws Exception; }
4,编写IDao接口的实现类DaoImpl
package cn.xiong.daostudy.impl; import cn.xiong.daostudy.dao.IDao; import cn.xiong.daostudy.vo.Info; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.List; import java.util.ArrayList; public class DaoImpl implements IDao { private Connection conn=null; private PreparedStatement psmt=null; public DaoImpl(Connection conn) { this.conn=conn; } public boolean doInsert(Info info) throws Exception { boolean flag=false; String sql="insert into Info(name,age,email,job,employ) values(?,?,?,?,?);"; this.psmt=this.conn.prepareStatement(sql); this.psmt.setString(1,info.getName()); this.psmt.setInt(2,info.getAge()); this.psmt.setString(3,info.getEmail()); this.psmt.setString(4,info.getJob()); this.psmt.setDate(5,new java.sql.Date(info.getEmploy().getTime())); int n=this.psmt.executeUpdate(); this.psmt.close(); if(n>0) { flag=true; } return flag; } public Info findById(int id) throws Exception { String sql="select id,name,age,email,job,employ from Info where id=? ;"; ResultSet rst=null; this.psmt=this.conn.prepareStatement(sql); rst=this.psmt.executeQuery(); Info info=new Info(); if(rst.next()) { info.setId(rst.getInt(1)); info.setName(rst.getString(2)); info.setAge(rst.getInt(3)); info.setEmail(rst.getString(4)); info.setJob(rst.getString(5)); info.setEmploy(new java.util.Date(rst.getDate(6).getTime())); } this.psmt.close(); return info; } public List<Info> findAll() throws Exception { List<Info> list=new ArrayList<Info>(); String sql="select id,name,age,email,job,employ from Info;"; ResultSet rst=null; this.psmt=this.conn.prepareStatement(sql); rst=this.psmt.executeQuery(); while(rst.next()) { Info info=new Info(); info.setId(rst.getInt(1)); info.setName(rst.getString(2)); info.setAge(rst.getInt(3)); info.setEmail(rst.getString(4)); info.setJob(rst.getString(5)); info.setEmploy(new java.util.Date(rst.getDate(6).getTime())); list.add(info); } this.psmt.close(); return list; } }
5,编写代理操作类DaoProxy,负责将实现类与数据库操作类整合,调用实现类中数据库的CRUD操作,并处理数据库连接的关闭。
package cn.xiong.daostudy.proxy; import cn.xiong.daostudy.database.DatabaseConnection; import cn.xiong.daostudy.vo.Info; import cn.xiong.daostudy.dao.IDao; import cn.xiong.daostudy.impl.DaoImpl; import java.util.List; public class DaoProxy implements IDao { private DatabaseConnection dc=null; private IDao dao=null; public DaoProxy() { this.dc=new DatabaseConnection(); this.dao=new DaoImpl(this.dc.getConnection()); } public boolean doInsert(Info info) throws Exception { boolean flag=false; try { if(null==this.dao.findById(info.getId())) { flag=this.dao.doInsert(info); } } catch (Exception e) { throw e; } finally { this.dc.close(); } return flag; } public Info findById(int id) throws Exception { Info info=null; try { info=this.dao.findById(id); } catch (Exception e) { throw e; } finally { this.dc.close(); } return info; } public List<Info> findAll() throws Exception { List<Info> list=null; try { list=this.dao.findAll(); } catch (Exception e) { throw e; } finally { this.dc.close(); } return list; } }
6,定义工厂类DaoFactory。工厂类通过DaoProxy实例化IDao对象,供调用者操作数据库。
package cn.xiong.daostudy.daofactory; import cn.xiong.daostudy.dao.IDao; import cn.xiong.daostudy.proxy.DaoProxy; public class DaoFactory { public static IDao getDaoInstance() { return new DaoProxy(); } }
7,编写前台显示页面DaoBeanDemo_01.jsp,展示数据库查询的结果
View Code
1 <%@ page contentType="text/html" pageEncoding="GBK"%>
2 <%@ page import="java.util.List"%>
3 <%@ page import="java.util.Iterator"%>
4 <%@ page import="cn.xiong.daostudy.vo.Info"%>
5 <html>
6 <head>
7 <% request.setCharacterEncoding("GBK");%>
8 </head>
9 <jsp:useBean id="info" class="cn.xiong.daostudy.daofactory.DaoFactory" scope="page" />
10 <%
11 List list=info.getDaoInstance().findAll();
12 Iterator it=list.iterator();
13 %>
14 <body>
15 <table border="1" width="80%">
16 <tr>
17 <th>ID</th>
18 <th>姓名</th>
19 <th>年龄</th>
20 <th>E-Mail</th>
21 <th>岗位</th>
22 <th>雇佣日期</th>
23 </tr>
24 <%
25 while(it.hasNext())
26 {
27 Info in=(Info)it.next();
28 %>
29 <tr>
30 <td><%=in.getId()%></td>
31 <td><%=in.getName()%></td>
32 <td><%=in.getAge()%></td>
33 <td><%=in.getEmail()%></td>
34 <td><%=in.getJob()%></td>
35 <td><%=in.getEmploy()%></td>
36 </tr>
37 <%
38 }
39 %>
40 </table>
41 </body>
42 </html>