jsp的DAO三层-------------实现登录功能
1、controller 控制器层 相当于servlet
2、service层:业务逻辑层,主要用来处理业务逻辑
3、dao层 用来和数据库打交道
建包
servlet包、service包、dao包
前台jsp简单页面
<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>
一、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(); } }
二、service层
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; } }
三、dao层
将常用的连接数据库语句封装成一个工具类
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; } }