第十周作业

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

 

 

 

 1 package com.study.stu;
 2 
 3 public class Stu {
 4     
 5     private int stuid;
 6     private String username;
 7     private String password;
 8     private int age;
 9     @Override
10     public String toString() {
11         return "Stu [stuid=" + stuid + ", username=" + username + ", password="
12                 + password + ", age=" + age + "]";
13     }
14     public Stu() {
15         super();
16         // TODO Auto-generated constructor stub
17     }
18     public Stu(int stuid, String username, String password, int age) {
19         super();
20         this.stuid = stuid;
21         this.username = username;
22         this.password = password;
23         this.age = age;
24     }
25     public int getStuid() {
26         return stuid;
27     }
28     public void setStuid(int stuid) {
29         this.stuid = stuid;
30     }
31     public String getUsername() {
32         return username;
33     }
34     public void setUsername(String username) {
35         this.username = username;
36     }
37     public String getPassword() {
38         return password;
39     }
40     public void setPassword(String password) {
41         this.password = password;
42     }
43     public int getAge() {
44         return age;
45     }
46     public void setAge(int age) {
47         this.age = age;
48     }
49     
50 
51 }
 1 package com.study.stu;
 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 import java.util.List;
 9 
10 import javax.naming.Context;
11 import javax.naming.InitialContext;
12 import javax.naming.NamingException;
13 import javax.sql.DataSource;
14 
15 public class JDBCUtils {
16 
17 
18     //获取连接
19     protected static Connection getConnection(){
20         Connection conn=null;
21             try {
22                 Class.forName("com.mysql.jdbc.Driver");
23                 // 2.建立连接
24                 conn = DriverManager.getConnection(
25                         "jdbc:mysql://localhost:3306/wl_tale", "root", "root");
26             } catch (Exception e) {
27                 e.printStackTrace();
28             } 
29             return conn;
30     }    
31     
32 
33     
34     
35     //关闭连接
36     protected static void closeAll(Connection con,PreparedStatement ps,ResultSet rs){        
37     try {
38         if(rs != null)
39             rs.close();
40         if(ps != null)
41             ps.close();
42         if(con != null)
43             con.close();
44         
45         } catch (SQLException e) {
46             e.printStackTrace();
47         }
48     }
49     
50 }
 1 package com.study.stu;
 2 
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 
 6 import com.mysql.jdbc.Connection;
 7 import com.mysql.jdbc.PreparedStatement;
 8 
 9 public class Service {
10     public boolean login(String username,String password) {
11         boolean re = false;
12         Connection conn = (Connection) JDBCUtils.getConnection();
13         String sql = "select * from t_stu where username=? and password=?";
14         PreparedStatement ps = null;
15         ResultSet rs = null;
16         try {
17             ps = (PreparedStatement) conn.prepareStatement(sql);
18             ps.setString(1, username);
19             ps.setString(2, password);
20             rs = ps.executeQuery();
21             if (rs.next())
22                 re = true;
23         } catch (SQLException e) {
24             e.printStackTrace();
25         } finally {
26             JDBCUtils.closeAll(conn, ps, rs);
27         }
28         return re;
29     }
30     public void register(String username, String password, String age) {
31         Connection conn = (Connection) JDBCUtils.getConnection();
32         PreparedStatement ps = null;
33         try {
34             String sql = "insert into t_stu(username,password,age) values(?,?,?)"; 
35             // 4.执行SQL语句
36             ps = (PreparedStatement) conn.prepareStatement(sql);
37             ps.setString(1, username);
38             ps.setString(2, password);
39             ps.setString(3, String.valueOf(age));
40             ps.executeUpdate();// 增删改都用这个
41         } catch (SQLException e) {
42             e.printStackTrace();
43         } finally {
44             JDBCUtils.closeAll(conn, ps, null);
45         }
46 
47     }
48 
49 }

1.设计一个注册页面,实现用户注册功能

 

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 
 3 <html>
 4 <head>
 5 <title>My JSP 'Register.jsp' starting page</title>
 6 <script type="text/javascript">
 7     function ok() {
 8         if (registerForm.username.value == "") {
 9             alert("账号不能为空!");
10             return;
11         }
12         if (registerForm.password.value == "") {
13             alert("密码不能为空!");
14             return;
15         }
16         registerForm.submit();
17         
18     }
19 </script>
20 </head>
21 <body>
22     <form name="registerForm" action="Add.jsp" method="post">
23         <table>
24             <tr>
25                 <td>
26                 <th align="center">请注册</th>
27                 </td>
28             </tr>
29             <tr>
30                 <td>用户名:</td>
31                 <td><input type="text" name="username"></input>
32                 </td>
33             </tr>
34             <tr>
35                 <td>密 码:</td>
36                 <td><input type="password" name="password"></input>
37                 </td>
38             </tr>
39             <tr>
40                 <td>年龄:</td>
41                 <td><input type="text" name="age"></input>
42                 </td>
43             </tr>
44             <tr>
45                 <td rowspan="2"><input type="button" value="完成注册" onClick="ok()"></td>
46             </tr>
47         </table>
48     </form>
49 </body>
50 </html>
 1 <%@page import="com.study.stu.Service"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6 <head>
 7 <title>My JSP 'Register.jsp' starting page</title>
 8 </head>
 9 
10 <body>
11     <%
12         Service se = new Service();
13         request.setCharacterEncoding("utf-8");
14         String uname = request.getParameter("username");
15         String upwd = request.getParameter("password");
16         String age = request.getParameter("age");
17         se.register(uname, upwd, age);
18         //session.setAttribute("uname", uname);
19     //    request.getRequestDispatcher("Hello.jsp")
20     //            .forward(request, response);
21         out.print("注册成功,即将跳回登陆页.....");
22         response.setHeader("refresh", "3;url=Login.jsp");
23 
24     %>
25 
26 </body>
27 </html>

 

 

 

 


2.设计一个登陆页面,实现用户名密码登陆

 1 <%@page import="com.study.stu.Service"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 
 4 <html>
 5 <head>
 6 <title>My JSP 'Register.jsp' starting page</title>
 7 </head>
 8 <script type="text/javascript">
 9     function validate() {
10         if (loginForm.username.value == "") {
11             alert("账号不能为空!");
12             return;
13         }
14         if (loginForm.password.value == "") {
15             alert("密码不能为空!");
16             return;
17         }
18         loginForm.submit();
19     }
20 </script>
21 <body>
22     <form name="loginForm" action="Service.jsp" method="post">
23         <table>
24             <tr><td><th align="center">欢迎登录</th></td></tr>
25             <tr>
26                 <td>用户名:</td>
27                 <td><input type="text" name="username"></input></td>
28             </tr>
29             <tr>
30                 <td>密        码:</td>
31                 <td><input type="password" name="password"></input></td>
32             </tr>
33             <tr>
34                 <td><input type="button" value="登录" onClick="validate()">
35                 </td>
36                 <td><a href="Register.jsp">注册</a>
37                 </td>
38             </tr>
39         </table>
40     </form>
41 
42     <br>
43 </body>
44 </html>
 1 <%@page import="com.study.stu.Service"%>
 2 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 3 
 4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 5 <html>
 6 <head>
 7 <title>My JSP 'Register.jsp' starting page</title>
 8 </head>
 9 
10 <body>
11     <%
12         Service se = new Service();
13         request.setCharacterEncoding("utf-8");
14         String uname = request.getParameter("username");
15         String upwd = request.getParameter("password");
16         if (se.login(uname, upwd)) {
17             request.getRequestDispatcher("Hello.jsp").forward(request,
18                     response);
19         } else {
20             %>
21             <script type="text/javascript">alert("账号或密码错误")</script>
22             <% 
23             response.setHeader("refresh", "0;url=Login.jsp");
24         }
25     %>
26 
27 </body>
28 </html>

 

 

 

 



posted @ 2022-05-08 20:22  王佳瑜  阅读(4)  评论(0编辑  收藏  举报