DAO的组成
1.实体
一个java类,这个类与数据库中的表对应。
比如,table_user表与User类对应:
对应关系指的是:
table_user表名与User类名对应
table_user表的列(column)与User类的属性对应
声明一系列方法(即对数据库进行哪些操作--crud),
这些方法应该与具体的技术无关。
3.dao实现
提供符合接口定义的对象,调用者不用关心对象的创建细节。
实体VO->emp.java:
数据库->DatabaseConnection.java:
DAO接口->IEmpDAO.java:
DAO实现类->EmpDAOImpl.java:
DAO代理->EmpDAOProxy.java:
DAO工厂->EmpDAOFactory.java:
我觉得对于初学者,如果是为了测试一下这种的设计模式,最后一步一步来,免得出麻烦,搞到头都大,像我那些注释的那样,
1.实体
一个java类,这个类与数据库中的表对应。
比如,table_user表与User类对应:
对应关系指的是:
table_user表名与User类名对应
table_user表的列(column)与User类的属性对应
table_user表中的一条记录与User类的一个实例对应
2.数据库
数据库的创建,链接。
3.dao接口声明一系列方法(即对数据库进行哪些操作--crud),
这些方法应该与具体的技术无关。
3.dao实现
实现dao接口的一个具体类DaoImpl
4.dao的代理
对象访问的管理
5.工厂提供符合接口定义的对象,调用者不用关心对象的创建细节。
也就是说,通过工厂,可以将调用者与要调用的对象解耦了。
显示层->DAO.HTML:
<html>
<head><title>DAO</title></head>
<body>
<form action="createEmp.jsp" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="user" width="20px"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="text" name="email"/></td>
</tr>
<tr>
<td><input type="submit" value=" 注册"/></td>
<td><input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</body>
</html>
控制层->createEmp.jsp
<%@page contentType="text/html" pageEncoding="GBK"%>
<%@page import="DAO.VO.Emp"%>
<html>
<head><title>createEmp</title></head>
<body>
<center>hello</center>
<jsp:useBean id="emp" class="DAO.VO.Emp" />
<jsp:useBean id="factory" class="DAO.Factory.EmpDAOFactory" />
<%
String user = request.getParameter("user");
String pass = request.getParameter("password");
String email = request.getParameter("email");
emp = new Emp(user,pass,email);
/*emp.setUser(user);
emp.setPassword(pass);
emp.setEmail(email);*/
factory.instance().createEmp(emp);
%>
<%="user->"+user%><br />
<%="pass->"+pass%><br />
<%="email->"+email%><br />
</body>
</html>
实体VO->emp.java:
package DAO.VO;
public class Emp
{
private int id;
private String user;
private String password;
private String email;
public Emp(){
}
public Emp(String user,String password,String email){
this.user = user;
this.password = password;
this.email = email;
}
public Emp(int id,String user,String password,String email){
this.id = id;
this.user = user;
this.password = password;
this.email = email;
}
public void setID(int id){
this.id = id;
}
public void setUser(String user){
this.user = user;
}
public void setPassword(String password){
this.password = password;
}
public void setEmail(String email){
this.email = email;
}
public int getID(){
return this.id;
}
public String getUser(){
return user;
}
public String getPassword(){
return password;
}
public String getEmail(){
return email;
}
/*public static void main(String[] args){
Emp e = new Emp();
e.setID(2);
e.setUser("hua");
e.setPassword("aaaaa.0");
e.setEmail("980914629@qq.com");
println("ID:"+e.getID());
println("User:"+e.getUser());
println("Password:"+e.getPassword());
println("Email:"+e.getEmail());
}
public static void println(Object obj){
System.out.println(obj);
}*/
}
数据库->DatabaseConnection.java:
package DAO.DBC;
import java.sql.*;
public class DatabaseConnection
{
private static final String DRIVER = "org.gjt.mm.mysql.Driver";
private static final String DBURL = "jdbc:mysql://localhost:3306/emp";
private static final String DBUSER = "scott";
private static final String DBPASSWORD = "tiger";
private static Connection conn;
public DatabaseConnection()throws Exception{
Class.forName(DRIVER);//驱动加载
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//连接数据库
}
public Connection getConnection(){
return conn;
}
public void close()throws Exception{
conn.close();
}
/*public static void main(String[] args)throws Exception
{
DatabaseConnection dc = new DatabaseConnection();
dc.connection();
printf(dc.getConnection());
Connection conn = dc.getConnection();
Statement state = conn.createStatement();
state.executeUpdate("use study");//首先要使用哪个数据库
ResultSet result = state.executeQuery("select * from emp");
while(result.next()){
printf(result.getObject("id"));
printf(result.getObject("user"));
printf(result.getObject("password"));
printf(result.getObject("email"));
}
dc.close();
}
public static void printf(Object obj){
System.out.println(obj);
}*/
}
DAO接口->IEmpDAO.java:
package DAO.IEmpDAO;
import DAO.VO.*;
import java.util.*;
public interface IEmpDAO
{
public boolean createEmp(Emp emp)throws Exception;
public List<Emp> findAll(String keyWord)throws Exception;
public Emp findByID(int id)throws Exception;
}
DAO实现类->EmpDAOImpl.java:
package DAO.EmpDAOImpl;
import DAO.VO.*;
import DAO.IEmpDAO.*;
import java.util.*;
import java.sql.*;
public class EmpDAOImpl implements IEmpDAO
{
private Connection conn;
PreparedStatement preStatement;
ResultSet result;
public EmpDAOImpl(Connection conn){
preStatement = null;
result = null;
this.conn = conn;
}
public boolean createEmp(Emp e)throws Exception{
preStatement = conn.prepareStatement("INSERT INTO emp(user,password,email) VALUES(?,?,?)");
preStatement.setString(1,e.getUser());
preStatement.setString(2,e.getPassword());
preStatement.setString(3,e.getEmail());
preStatement.execute("use study");//先使用哪个数据库
preStatement.execute();
preStatement.close();
return true;
}
public List<Emp> findAll(String keyWord)throws Exception{
List <Emp> emps = new ArrayList<Emp>();
preStatement = conn.prepareStatement("select * from emp where user like ? or password like ?");
preStatement.setString(1,"%"+keyWord+"%");
preStatement.setString(2,"%"+keyWord+"%");
preStatement.execute("use study");//先使用哪个数据库
result = preStatement.executeQuery();
while(result.next()){
Emp emp = new Emp(result.getInt("id"),
result.getString("user"),
result.getString("password"),
result.getString("email")
);
emps.add(emp);
}
preStatement.close();
return emps;
}
public Emp findByID(int id)throws Exception{
Emp emp = null;
preStatement = conn.prepareStatement("select * from emp where id=?");
preStatement.setInt(1,id);
result = preStatement.executeQuery();
while(result.next()){
emp = new Emp(result.getInt("id"),
result.getString("user"),
result.getString("password"),
result.getString("email")
);
}
preStatement.close();
return emp;
}
public boolean findEmp(String user)throws Exception{
List <Emp> emps = new ArrayList<Emp>();
preStatement = conn.prepareStatement("select * from emp where user=?");
preStatement.setString(1,user);
preStatement.execute("use study");
result = preStatement.executeQuery();
while(result.next()){
String userTemp = result.getString("user");
if(user.equals(userTemp)){
preStatement.close();
return true;
}
}
preStatement.close();
return false;
}
/*public static void main(String[] args)throws Exception{
EmpDAOImpl edi = new EmpDAOImpl(new DatabaseConnection().getConnection());
edi.createEmp(new Emp("hua1","aaaaa.0","435501646@qq.com"));
List <Emp> emps = edi.findAll("hu");
for(int i=0;i<emps.size();i++){
printf(emps.get(i).getID()+" ");
printf(emps.get(i).getUser()+" ");
printf(emps.get(i).getPassword()+" ");
printfln(emps.get(i).getEmail());
}
Emp emp;
emp = edi.findByID(5);
printf(emp.getID()+" ");
printf(emp.getUser()+" ");
printf(emp.getPassword()+" ");
printfln(emp.getEmail());
}
public static void printf(Object obj){
System.out.print(obj);
}
public static void printfln(Object obj){
System.out.println(obj);
}*/
}
DAO代理->EmpDAOProxy.java:
package DAO.EmpProxy;
import DAO.VO.*;
import DAO.IEmpDAO.*;
import DAO.DBC.*;
import DAO.EmpDAOImpl.*;
import java.util.*;
public class EmpDAOProxy implements IEmpDAO
{
private DatabaseConnection dc;
private EmpDAOImpl edi;
public EmpDAOProxy()throws Exception{
dc = new DatabaseConnection();
edi = new EmpDAOImpl(dc.getConnection());
}
public boolean createEmp(Emp emp){
try
{
if(!edi.findEmp(emp.getUser()))
edi.createEmp(emp);
else
return false;
}
catch (Exception e)
{
}
finally
{
try
{
dc.close();
}
catch (Exception e)
{
}
}
return true;
}
public List<Emp> findAll(String keyWord){
List<Emp> emps = new ArrayList<Emp>();
try
{
emps = edi.findAll(keyWord);
}
catch (Exception e)
{
}
return emps;
}
public Emp findByID(int id){
Emp emp = null;
try
{
emp = edi.findByID(id);
}
catch (Exception e)
{
}
return emp;
}
/*public static void main(String[] args){
System.out.println("Hello World!");
}*/
public static void printf(Object obj){
System.out.print(obj);
}
public static void printfln(Object obj){
System.out.println(obj);
}
}
DAO工厂->EmpDAOFactory.java:
package DAO.Factory;
import DAO.VO.*;
import DAO.EmpProxy.*;
public class EmpDAOFactory
{
public static EmpDAOProxy instance()throws Exception{
return new EmpDAOProxy();
}
}
我觉得对于初学者,如果是为了测试一下这种的设计模式,最后一步一步来,免得出麻烦,搞到头都大,像我那些注释的那样,
都是一步一步来的,虽然可能有点慢,但是自己会更加注意那些细节,而且如果一下子写完的话,如果出错了,那时候头就大了,
不要看到那些代码都是一下子出现你面前,你就要一下子写完它,应该理解之后,再一步一步慢慢写,这样才能有好的收获的。
<pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><pre name="code" class="java"><pre>