JSP第十周作业

数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)

1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接

StuDao.java

 1 package dao;
 2  
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8  
 9 public class BaseDao {
10     protected Connection getConnection(){
11         Connection conn=null;
12             try {
13                 Class.forName("com.mysql.jdbc.Driver");
14                  
15                 conn = DriverManager.getConnection(
16                         "jdbc:mysql://localhost:3306/test", "root", "root");
17             } catch (Exception e) {
18                 e.printStackTrace();
19             }
20             return conn;
21     }  
22      
23  
24  
25     protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){     
26     try {
27         if(rs != null)
28             rs.close();
29         if(ps != null)
30             ps.close();
31         if(con != null)
32             con.close();
33          
34         } catch (SQLException e) {
35             e.printStackTrace();
36         }
37     }
38 }

BaseDao.java

 1 package dao;
 2 
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 
 8 public class StuDao extends BaseDao {
 9 
10     public boolean login(String name, String pwd) {
11         boolean f = false;
12         Connection conn = getConnection();
13         String sql = "select * from stu where uname=? and upwd=?";
14         PreparedStatement ps = null;
15         ResultSet rs = null;
16         try {
17             ps = conn.prepareStatement(sql);
18             ps.setString(1, name);
19             ps.setString(2, pwd);
20             rs = ps.executeQuery();
21             if (rs.next())
22                 f = true;
23         } catch (SQLException e) {
24             // TODO Auto-generated catch block
25             e.printStackTrace();
26         } finally {
27             closeAll(conn, ps, rs);
28         }
29         return f;
30     }
31 
32     public int reg(String uname, String upwd, int uage) {
33         Connection conn = getConnection();
34         PreparedStatement ps = null;
35         int i = -1;
36         try {
37             String sql = "insert into stu(uname,upwd,uage) values(?,?,?)";
38             // 4.执行SQL语句
39             ps = conn.prepareStatement(sql);
40             ps.setString(1, uname);
41             ps.setString(2, upwd);
42             ps.setInt(3, uage);
43             i = ps.executeUpdate();
44             return i;
45         } catch (SQLException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         } finally {
49             closeAll(conn, ps, null);
50         }
51         return i;
52 
53     }
54 
55 }

login.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <form action="verification.jsp" method="post">
11             <b>用户名</b>
12             <input type="text" name="uname" />
13             <br />
14             <b>密码</b>
15             <input type="text" name="upwd" />
16             <br />
17             <input type="submit" value="登录" />
18             <a href="register.jsp">注册</a>
19 </form>
20 </body>
21 </html>

register.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3      
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11  
12 <form action="jump.jsp" method="post">
13             <b>用户名</b>
14             <input type="text" name="uname" />
15             <br />
16             <b>密码</b>
17             <input type="text" name="upwd" />
18             <br />
19             <b>年龄</b>
20             <input type="text" name="uage"/>
21             <br />
22             <input type="submit" value="注册" />
23             <a href="login.jsp">登录</a>
24         </form>
25 </body>
26 </html>

jump.jsp

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8     <%
 9         String name = request.getParameter("uname");
10         String upwd = request.getParameter("upwd");
11         String uages = request.getParameter("uage");
12         String uname = new String(name.getBytes("ISO-8859-1"), "utf-8");
13         int uage = Integer.parseInt(uages);
14         StuDao s1 = new StuDao();
15          if (s1.reg(uname, upwd, uages) != -1) {
16         out.println("注册成功!");
17         out.println("三秒后跳转到主页...");
18         response.setHeader("refresh", "3;url=login.jsp");
19     } else {
20         out.println("注册失败!");
21         out.println("三秒后跳转到主页...");
22         response.setHeader("refresh", "3;url=login.jsp");
23     }
24     %>
25 </body>
26 </html>

verification.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <%@ page import="dao.StuDao"%>
 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5 <html>
 6 <head>
 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 8 <title>Insert title here</title>
 9 </head>
10 <body>
11     <%
12         String name = request.getParameter("uname");
13         String upwd = request.getParameter("upwd");
14         String uname = new String(name.getBytes("ISO-8859-1"), "utf-8");
15         try {
16             int pwd = Integer.parseInt(upwd);
17             StuDao s = new StuDao();
18             if (s.login(uname, upwd)) {
19                 out.println("登录成功!");
20             } else {
21                 out.println("登陆失败!");
22                 out.println("三秒后跳转到主页面...");
23                 response.setHeader("refresh", "3;url=login.jsp");
24             }
25         } catch (Exception e) {
26             out.println("异常!!");
27             out.println("三秒后跳转到主页面...");
28             response.setHeader("refresh", "3;url=login.jsp");
29         }
30         
31     %>
32     <a href="login.jsp">返回</a>
33 </body>
34 </html>

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2022-05-07 00:08  唐一南  阅读(39)  评论(0)    收藏  举报