1:VO类
package cn.mldn.lxh.vo;
import java.util.Date;public class Emp {
private int empno;
private String ename;
private String job;
private Date hiredate;
private float sal;
public Emp()
{
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public float getSal() {
return sal;
}
public void setSal(float sal) {
this.sal = sal;
}
@Override
public String toString() {
return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job
+ ", hiredate=" + hiredate + ", sal=" + sal + "]";
}
}
2:数据库连接类
package cn.mldn.lxh.jdbc;
import java.sql.*;
public class DatabaseConnection {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/mydatabase";
private static String username = "root";
private static String password = "123456";
private Connection conn=null;
public DatabaseConnection()throws Exception
{
try
{
Class.forName(driver);
this.conn=DriverManager.getConnection(url,username,password);
}catch(Exception e)
{
e.printStackTrace();
}
}
public Connection getConnectoion()
{
return this.conn;
}
public void close()throws Exception
{
if(this.conn!=null)
{
try
{
conn.close();
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
3:定义DAO操作标准
package cn.mldn.lxh.dao;
import java.util.List;
import cn.mldn.lxh.vo.Emp;
public interface IEmpDAO {
public boolean doCreate(Emp emp)throws Exception;
public List<Emp> findAll(String keyWord)throws Exception;
public Emp findById(int empno)throws Exception;
}
4:DAO真实主题类
package cn.mldn.lxh.dao.impl;import java.sql.*;
import java.util.List;
import java.util.ArrayList;
import cn.mldn.lxh.dao.IEmpDAO;
import cn.mldn.lxh.vo.Emp;
public class EmpDAOImpl implements IEmpDAO{
private Connection conn=null;
private PreparedStatement pstmt=null;
public EmpDAOImpl(Connection conn)
{
this.conn=conn;
}
@Override
public boolean doCreate(Emp emp) throws Exception {
boolean flag=false;
String sql="INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES(?,?,?,?,?)";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setInt(1, emp.getEmpno());
this.pstmt.setString(2, emp.getEname());
this.pstmt.setString(3, emp.getJob());
this.pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
this.pstmt.setFloat(5, emp.getSal());
if(this.pstmt.executeUpdate()>0)
{
flag=true;
}
this.pstmt.close();
return flag;
}
@Override
public List<Emp> findAll(String keyWord) throws Exception {
List<Emp> all=new ArrayList<Emp>();
String sql="SELECT empno,ename,job,hiredate,sal FROM Emp WHERE ename LIKE?";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setString(1,"%"+keyWord+"%");
ResultSet rs=this.pstmt.executeQuery();
Emp emp=null;
while(rs.next())
{
emp=new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setSal(rs.getFloat("sal"));
all.add(emp);
}
this.pstmt.close();
return all;
}
@Override
public Emp findById(int empno) throws SQLException{
Emp emp=null;
String sql="SELECT empno,ename,job,hiredate,sal FROM Emp WHERE empno=?";
this.pstmt=this.conn.prepareStatement(sql);
this.pstmt.setInt(1, empno);
ResultSet rs=this.pstmt.executeQuery();
if(rs.next())
{
emp=new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setHiredate(rs.getDate("hiredate"));
emp.setSal(rs.getFloat("sal"));
}
this.pstmt.close();
return emp;
}
}
5:代理主题类
package cn.mldn.lxh.dao.proxy;
import java.util.List;
import cn.mldn.lxh.dao.IEmpDAO;
import cn.mldn.lxh.dao.impl.EmpDAOImpl;
import cn.mldn.lxh.vo.Emp;
import cn.mldn.lxh.jdbc.DatabaseConnection;;
public class EmpDAOProxy implements IEmpDAO{
private DatabaseConnection jdbc=null;
private IEmpDAO dao=null;
public EmpDAOProxy()throws Exception
{
this.jdbc=new DatabaseConnection();
System.out.println(this.jdbc.getConnectoion() == null);
this.dao=new EmpDAOImpl(this.jdbc.getConnectoion());
}
@Override
public boolean doCreate(Emp emp) throws Exception {
boolean flag=false;
try
{
flag=this.dao.doCreate(emp);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
this.jdbc.close();
}
return flag;
}
@Override
public List<Emp> findAll(String keyWord) throws Exception {
List<Emp> all=null;
try
{
all=this.dao.findAll(keyWord);
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
this.jdbc.close();
}
return all;
}
@Override
public Emp findById(int empno) throws Exception {
Emp emp=null;
try
{
emp=this.dao.findById(empno);
}catch(Exception e)
{
e.printStackTrace();
}finally
{
this.jdbc.close();
}
return emp;
}
}
6:真是实现类
package cn.mldn.lxh.factory;
import cn.mldn.lxh.dao.IEmpDAO;
import cn.mldn.lxh.dao.proxy.EmpDAOProxy;
public class DAOFactory {
public static IEmpDAO getIEmpDAOInstance()throws Exception
{
return new EmpDAOProxy();
}
}
7:jsp调用DAO
增加雇员
<%@ page contentType="text/html" language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML >
<html>
<head>
<title>增加雇员</title>
</head>
<body>
<form action="emp_insert_do.jsp" method="post">
雇员编号:<input type="text" name="empno"><br>
雇员姓名:<input type="text" name="ename"><br>
雇员工作:<input type="text" name="job"><br>
雇员日期:<input type="text" name="hiredate"><br>
雇员工资:<input type="text" name="sal"><br>
<input type="submit" value="注册">
<input type="reset" value="重置">
</form>
</body>
</html>
完成添加操作
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.mldn.lxh.factory.*,cn.mldn.lxh.vo.*"%>
<%@ page import="java.text.*" %>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<%
request.setCharacterEncoding("UTF-8");
%>
</head>
<%
Emp emp=new Emp();
emp.setEmpno(Integer.parseInt(request.getParameter("empno")));
emp.setEname(request.getParameter("ename"));
emp.setJob(request.getParameter("job"));
emp.setHiredate(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("hiredate")));
emp.setSal(Float.parseFloat(request.getParameter("sal")));
try
{
if(DAOFactory.getIEmpDAOInstance().doCreate(emp))
{
%>
<h3>雇员信息添加成功!</h3>
<%
}else{
%>
<h3>雇员信息添加失败</h3>
<%
}
}catch(Exception e)
{
e.printStackTrace();
}
%>
<body>
</body>
</html>
数据查询
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="cn.mldn.lxh.factory.*,cn.mldn.lxh.vo.*"%>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<%
request.setCharacterEncoding("UTF-8");
%>
</head>
<body>
<form action="findall_emp.jsp" method="post">
请输入要查询的关键字<input type="text" name="kw">
<input type="submit" value="查询">
</form>
<%
try
{
String keyWords=request.getParameter("kw");
if(keyWords!=null)
{
List<Emp> all=DAOFactory.getIEmpDAOInstance().findAll(keyWords);
Iterator<Emp> iter=all.iterator();
%>
<div align="center">
<table border="1">
<tr>
<td>雇员编号</td>
<td>雇员姓名</td>
<td>雇员职位</td>
<td>雇用日期</td>
<td>基本工资</td>
</tr>
<%
while(iter.hasNext())
{
Emp emp=iter.next();
%>
<tr>
<td><%=emp.getEmpno()%></td>
<td><%=emp.getEname()%></td>
<td><%=emp.getJob()%></td>
<td><%=emp.getHiredate()%></td>
<td><%=emp.getSal()%></td>
</tr>
<%
}
%>
</table>
</div>
<%
}
}catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>