jsp的DAO三層-------------實現登陸功能

一、controller 控制器層  至關於servletjava

二、service層:業務邏輯層,主要用來處理業務邏輯sql

三、dao層   用來和數據庫打交道數據庫

 

 

建包oracle

servlet包、service包、dao包jsp

 

前臺jsp簡單頁面ide

<body>
  <div id="lg_div1"></div>
    <div id="lg_container">
           <div id="lg_container_content">
               <div id="lg_text">
                   <div>歡迎登陸留言板系統</div>
               </div>
                    <form id="lg_form" action="<%=path %>/logincheck" method="post">
                        <div id="lg_name">用戶名:<div id="lg_name_tp"></div><div id="lg_name_name"><input type="text" name="userName" placeholder="用戶名"></div></div>
                        <div id="lg_pwd">密碼:<div id="lg_pwd_tp"></div><div id="lg_pwd_pwd"><input type="password" name="pwd" placeholder="用戶名密碼"></div></div>
                        <div id="lg_denglu">
                            <div id="lg_denglu_dl"><input type="button" value="登陸" onclick="login()"></div>
                            <div id="lg_handlu_zc"><input type="button" value="註冊"></a></div>
                        </div>
                    </form>
                    <div id="lg_handle">
                        <div id="lg_handle_findPwd"><a href="https://www.baidu.com">忘記密碼?</a></div>
                        <div id="lg_handle_attention">
                            <div id="attention_text"><span>關注咱們,讓咱們變得更好:</span></div>
                            <div id="attention_tp"></div>
                        </div>

                    </div>
           </div>
    </div>
    <div id="lg_div2"></div>
  </body>

 

 

1、servlet  工具

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.jereh.service.UserService;
import com.jereh.service.impl.UserServiceImpl;

public class LoginCheck extends HttpServlet {

    UserService service=new UserServiceImpl();
    
 
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String userName=request.getParameter("userName");
        String pwd=request.getParameter("pwd");
        
        boolean isSuccess=service.existsWithNameAndPwd(userName, pwd);//邏輯判斷交給service層 //獲取打印器
        PrintWriter out= response.getWriter();
        if(isSuccess){
            out.write("1");
        }else{
            out.write("0");
        }
        
        //刷新關閉
        out.flush();
        out.close();
        

    }

}

 

 

 

2、service層post

public interface UserService {
    
    /**
     * 
     * @param userName
     * @param pwd
     * @return
     */
    public boolean existsWithNameAndPwd(String userName,String pwd);

}
public class UserServiceImpl implements UserService {
    
    UserDao dao=new UserDaoImpl();

    @Override
    public boolean existsWithNameAndPwd(String userName, String pwd) {
        
        String sql="select count(*) num from t_account where username=?  and pwd=?";
        List<Object> pram=new ArrayList<Object>();
        pram.add(userName);
        pram.add(pwd);
        
        try {
            int count=dao.isExists(sql, pram);
            return count>0;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return false;
    }

}

 

3、dao層this

將經常使用的鏈接數據庫語句封裝成一個工具類spa

public class BaseDao {
    
    //數據庫鏈接
    private static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
    //驅動
    private static final String DRIVER="oracle.jdbc.driver.OracleDriver";
    //用戶名
    private static final String USRE="XXXXXXX";
    //用戶密碼
    private static final String PWD="123456";
    
    /**
     * @throws Exception 
     * 獲取數據庫鏈接
     */
    Connection conn=null;
    PreparedStatement pstmt=null;
    ResultSet rs=null;
    
    private void getConnection() throws Exception{
        
        if(conn==null){
             //1 加載驅動類
            Class.forName(DRIVER);
            // 2 獲取數據庫鏈接
            conn = DriverManager.getConnection(
                    URL, USRE,
                    PWD);
        }
        
    }
    
    
    /***
     * 
     * @param sql
     * @param params
     * @return
     */
    public int executeUpdate(String sql,List<Object> params)throws Exception{
        getConnection();
        //初始化句柄
         pstmt= conn.prepareStatement(sql);
        //綁定參數
        if(params!=null){
                for(int i=0;i<params.size();i++){
                    pstmt.setObject(i+1, params.get(i));
                }
        }
        //執行sql
        int count=pstmt.executeUpdate();
        //關閉資源
        pstmt.close();
        pstmt=null;
        conn.close();
        conn=null;
        
       return count;
    }
    
    
    /**
     * 
     * @param sql
     * @param params
     * @return
     */
     public ResultSet excuteQuery(String sql,List<Object> params)throws Exception{
            getConnection();
             pstmt= conn.prepareStatement(sql);
            //綁定參數
            if(params!=null){
                    for(int i=0;i<params.size();i++){
                        pstmt.setObject(i+1, params.get(i));
                    }
            }
            //執行查詢
            rs=pstmt.executeQuery();
            return rs;
     }
     
     
     /**
      * 關閉
      */
     public void close(){
                 try {
                     if(rs!=null){
                        rs.close();
                        rs=null;
                     }
                     if(pstmt!=null){
                         pstmt.close();
                         pstmt=null;
                     }
                     if(conn!=null){
                         conn.close();
                         conn=null;
                     }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
     }
    
    
    

}

 

import java.util.List;

public interface UserDao {
    public int isExists(String sql,List<Object> params) throws Exception;
}

 

import java.sql.ResultSet;
import java.util.List;

import com.jereh.dao.BaseDao;
import com.jereh.dao.UserDao;

public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public int isExists(String sql, List<Object> params) throws Exception{
         ResultSet rs=excuteQuery(sql, params);
         int count=0;
         if(rs.next()){
             count= rs.getInt("num");
             this.close();
         }
         return count;
    }

}
相關文章
相關標籤/搜索