第十周

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

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

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

 

 

 

 

 

 

posted @ 2022-05-08 15:10  刘姝彤  阅读(6)  评论(0编辑  收藏  举报