数据库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>

搜索

复制

最新文章

  1. 【HDU4585 Shaolin】map的经典运用
  2. IoC框架(转载)
  3. [Flex] ButtonBar系列——flex3 皮肤和外观设置
  4. EasyMock 使用方法与原理剖析--转载
  5. USB做Host的OTG原理
  6. Cordova CLI源码分析(一)——简介
  7. Jquery.validate表单验证
  8. 程序设计 之 C#实现《拼图游戏》 (下) 原理篇
  9. Java 第一个程序案HelloWorld例记录
  10. xml解析总结-常用需掌握
  11. Python3 OS 文件/目录方法
  12. Android device debug (adb) by Charge Only mode
  13. 【Linux】常用命令,持续更新
  14. 自己总结的C#编码规范--5.如何写好注释篇
  15. 潭州课堂25班:Ph201805201 django 项目 第三十九课 后台 文章发布,图片上传到 FastDFS后端实现 七牛云讲解(课堂笔记)
  16. tcp编程 示例
  17. 写一个简单的C词法分析器
  18. GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403
  19. Python递归遍历《指定目录》下的所有《文件》
  20. Tempdb--查看tempdb使用的脚本

热门文章

  1. windows中 mysql 免安装版安装
  2. 【Docker】容器使用规范--安全挂载建议
  3. 【第6篇】AI语音测试简介
  4. 【Spring系列】- Bean生命周期底层原理
  5. 解决Anaconda关联VSCode使用conda运行Python报错(无法将“conda”项识别为 cmdlet、函数、脚本文件或可运行程序)
  6. Linux中如何开启一个定时任务
  7. Training: ASCII
  8. ArcObjects SDK开发 008 从mxd地图文件说起
  9. [.NET学习]EFCore学习之旅 -2 简单的增删改查
  10. 【每日一题】【找到位置返回&amp;升序数组中第K大就是n-K小】2022年1月17日-NC88 寻找第K大