DAO模式普通写法(步骤)
第一步:封装好类对象
例如:
package com.sk.object;
public class Users {
private String userName="";
private String passWord="";
public Users(String userName,String passWord){
this.userName=userName;
this.passWord=passWord;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
第二步:写类对象所对应的DAO接口
1.此接口没有业务逻辑方法
例如:
package com.sk.dao;
import com.sk.object.Users;
public interface UserDAO {
public Users getUser(String userName);
}
第三步:写此DAO接口所对应的实现类
1.此类为单态类(构造函数私有,通过方法返回此类的实例)
例如:
{
private UserDAOImp(){}//私有构造
public static UserDAOImp getUserDaoImp(){
return new UserDAOImp();
} //通过此方法得到UserDAOImp的实例
}
2.此类实现了db操作
例如:
package com.sk.dao.imp;
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 com.sk.dao.UserDAO;
import com.sk.object.Users;
public class UserDAOImp implements UserDAO {
private Connection conn=null;
private PreparedStatement ps=null;
private String url="jdbc:odbc:users;databaseName=users;user=sa;password=";
private String sql="";
private ResultSet rs=null;
private UserDAOImp(){} //私有构造
public static UserDAOImp getUserDaoImp(){//静态方法返回实例
return new UserDAOImp();
}
private Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection(url);
return conn;
}
public Users getUser(String userName) {
// TODO Auto-generated method stub
try {
this.getConnection();
sql="select username,password from users where username=?";
ps=conn.prepareStatement(sql);
ps.setString(1, userName);
rs=ps.executeQuery();
if(rs.next()){
return new Users(rs.getString("username"), rs.getString("password"));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
return null;
}
}
第四步:写工厂(制造dao接口实现的静态方法,既是写制造上面第三步类的工厂)
1.public static UserDAO getUserDAO(){
return UserDAOImp.getUserDaoImp();
}
例如:
package com.sk.factory;
import com.sk.dao.UserDAO;
import com.sk.dao.imp.UserDAOImp;
public class DAOFactory {
public static UserDAO getUserDAO(){
return UserDAOImp.getUserDaoImp();
}
}
第五步:写service类(业务逻辑操作)
例如:
package com.sk.service;
import com.sk.dao.UserDAO;
import com.sk.factory.DAOFactory;
import com.sk.object.Users;
public class UserDAOService {
public Users checkLogin(String userName,String passWord){
UserDAO userDAO=DAOFactory.getUserDAO();
Users user= userDAO.getUser(userName);
if(user!=null){
if(user.getPassWord().equals(passWord)){
return user;
}
}
return null;
}
}
其余的功能各自添加即可(调用service的方法);
例如:
package com.sk.view;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sk.service.UserDAOService;
public class ChkUser extends HttpServlet {
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName=request.getParameter("userName");
String passWord=request.getParameter("passWord");
UserDAOService userDAOService=new UserDAOService();
if( userDAOService.checkLogin(userName, passWord)!=null){
out.println("OK");
}
else {
out.println("error");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}