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

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

复制代码
 1 package com.gd.entity;
 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     public Connection getConnection() {
11         Connection con = null;
12         try {
13             Class.forName("com.mysql.jdbc.Driver");
14             con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
15         } catch (Exception e) {
16             e.printStackTrace();
17         }
18         return con;
19     }
20     
21     protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
22         try {
23             if(rs != null)
24                 rs.close();
25             if(ps != null)
26                 ps.close();
27             if(con != null)
28                 con.close();
29             } catch (SQLException e) {
30                 e.printStackTrace();
31             }
32         }
33 
34 }
复制代码
复制代码
 1 package com.gd.entity;
 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      public int Register(String uname, String password, int age) {
10             int i = -1;
11             Connection con = getConnection();
12             String sql = "insert into stu(uname,password,age)values(?,?,?)";
13             PreparedStatement pred = null;
14             try {
15                 pred = con.prepareStatement(sql);
16                 pred.setString(1, uname);
17                 pred.setString(2, password);
18                 pred.setInt(3, age);
19                 i = pred.executeUpdate();
20             } catch (SQLException e) {
21                 e.printStackTrace();
22             } finally {
23                 closeAll(con, pred, null);
24             }
25             return i;
26         }
27         public boolean Login(String uname, String password) {
28             boolean f=false;
29             Connection con = getConnection();
30             String sql = "select * from stu where uname=? and password=?";
31             PreparedStatement pred = null;
32             ResultSet resultSet = null;
33             try {
34                 pred = con.prepareStatement(sql);
35                 pred.setString(1, uname);
36                 pred.setString(2, password);
37                 resultSet = pred.executeQuery();
38                 while (resultSet.next()) {
39                     f=true;
40                 }
41             } catch (SQLException e) {
42                 e.printStackTrace();
43             } finally {
44                 closeAll(con, pred, resultSet);
45             }
46             return f;
47         }
48 
49 }
复制代码
复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3     request.setCharacterEncoding("utf-8");
 4     response.setCharacterEncoding("utf-8");
 5 %>
 6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 7 <html>
 8   <head>  
 9     <title>My JSP 'register.jsp' starting page</title>
10   </head> 
11   <body>
12      <h1>注册</h1>
13     <form action="doregister.jsp" method="post">
14         <table>
15             <tr>
16                 <td>用户名</td>
17                 <td><input type="text" name="uname"></td>
18             </tr>
19 
20             <tr>
21                 <td>密码</td>
22                 <td><input type="password" name="password"></td>
23             </tr>
24             <tr>
25                 <td>年龄</td>
26                 <td><input type="number" name="age"></td>
27             </tr>
28             <tr>
29                 <td><input type="submit" value="注册"></td>
30                 <td><input type="reset" value="重置"></td>
31             </tr>
32         </table>
33     </form>
34   </body>
35 </html>
复制代码
复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.StuDao"%>
 3 <%@page import="javax.xml.bind.ParseConversionEvent"%>
 4 <%
 5     request.setCharacterEncoding("utf-8");
 6     response.setCharacterEncoding("utf-8");
 7 %>
 8 
 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
10 <html>
11   <head>
12     
13     
14     <title>My JSP 'doregister.jsp' starting page</title>
15 
16 
17   </head>
18   
19   <body>
20     <%
21         String uname = request.getParameter("uname");
22         String password = request.getParameter("password");
23         String age = request.getParameter("age");
24         int age1 = age == null ? -1 : Integer.parseInt(age);
25         StuDao sd=new StuDao();
26         int i=sd.Register(uname, password, age1);
27         if(i>0){
28         request.getRequestDispatcher("login.jsp").forward(request, response);
29         }else{
30         out.print("注册失败");
31         }
32     %>
33   </body>
34 </html>
复制代码
复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 3 <html>
 4   <head>   
 5     <title>My JSP 'login.jsp' starting page</title>
 6   </head>
 7   
 8   <body>
 9     <h1>欢迎</h1>
10     <form action="dologin.jsp" method="post">
11         <table>
12             <tr>
13                 <td>用户名</td>
14                 <td><input type="text" name="uname"></td>
15             </tr>
16 
17             <tr>
18                 <td>密码</td>
19                 <td><input type="password" name="password"></td>
20             </tr>
21             <tr>
22                 <td><input type="submit" value="登录"></td>
23                 <td><a href="register.jsp">注册</a></td>
24             </tr>
25         </table>
26     </form>
27   </body>
28 </html>
复制代码
复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%@page import="com.gd.entity.StuDao"%>
 3 <%@page import="javax.xml.bind.ParseConversionEvent"%>
 4 <%
 5     request.setCharacterEncoding("utf-8");
 6     response.setCharacterEncoding("utf-8");
 7 %>
 8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 9 <html>
10   <head>  
11     <title>My JSP 'dologin.jsp' starting page</title>
12   </head>
13   
14   <body>
15      <%
16         String uname = request.getParameter("uname");
17         String password = request.getParameter("password");
18         StuDao sd=new StuDao();
19         if(sd.Login(uname, password)){
20         request.getRequestDispatcher("index.jsp").forward(request, response);
21         }else{
22         out.print("登陆失败,即将跳回登陆页.....");
23         response.setHeader("refresh", "2;url=login.jsp");
24         }
25         
26     %>
27   </body>
28 </html>
复制代码
复制代码
 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6   <head>
 7    
 8     
 9     <title>My JSP 'index.jsp' starting page</title>
10     
11   </head>
12   
13   <body>
14     <h1>登录成功</h1>
15   </body>
16 </html>
 
 
posted on 2022-05-08 16:47  李育博  阅读(3)  评论(0编辑  收藏  举报