javabean之LoginDao
1.src中创建包org.lanqiao.dao并在包里创建LoginDao.java
2.在WEB-INF中创建check.jsp
3.check.jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="org.lanqiao.dao.LoginDao" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String name = request.getParameter("uname"); String pwd = request.getParameter("upwd"); LoginDao dao = new LoginDao(); int result=dao.login(name,pwd); if(result>0){ out.print("登录成功"); }else if (result==0){ out.print("用户名或密码有误"); }else{ out.print("系统异常"); } %> </body> </html>
4.LoginDao.jsp代码为:
package org.lanqiao.dao; import java.sql.*; public class LoginDao { public int login(String name,String pwd) {//返回1登录成功 0登录失败 -1异常 String URL = "jdbc:mysql://localhost:3306/myemployees?serverTimezone=UTC"; String USERNAME = "root"; String PWD = "123456"; Connection connection = null; Statement stmt=null; ResultSet rs=null; //返回值表示增删改第几条数据 try { //a.导入驱动 Class.forName("com.mysql.cj.jdbc.Driver"); //b.与数据库进行连接 connection=DriverManager.getConnection(URL,USERNAME,PWD); //c.发送sql,执行查找 stmt=connection.createStatement(); //把名字密码一收 String sql="select count(*) from login where uname='"+name+"' and upwd='"+pwd+"'"; rs=stmt.executeQuery(sql);//返回值表示增删改第几条数据 //d.处理结果 int count=-1; if(rs.next()) { count=rs.getInt(1); } if(count>0){ return 1; } else{ return 0; } }catch(ClassNotFoundException e){ e.printStackTrace(); return -1; } catch(SQLException e) { e.printStackTrace(); return -1; }catch(Exception e) { e.printStackTrace(); return -1; }finally { try {if(rs!=null){ rs.close(); } if(stmt!=null) { stmt.close(); }if(connection!=null) { connection.close(); } }catch(SQLException e) { e.printStackTrace(); } } } }
5.如果遇到MyWebProject打着叉子

进入 BuildPath,将打叉的移除即可


浙公网安备 33010602011771号