用户表: uid (主键,自动增长) uname upwd
使用分层实现注册。(必做)
1.注册界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name="form1" method="post" action="mysql.jsp" > <table> <tr> <td>用户名:</td> <td> <input type="text" name="uname" id="userName" ></td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="upwd" id="pwd"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form> </body> </html>
2.跳转界面
<%@page import="com.gd.dao.StuDao"%> <%@page import="com.gd.bean.Stu"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Stu s = new Stu(); String uname = request.getParameter("uname"); s.setUname(uname); String upwd = request.getParameter("upwd"); s.setUpwd(upwd); StuDao sd=new StuDao(); if(sd.addStu(s)>0){ request.getRequestDispatcher("ok.jsp").forward(request,response); }else{ request.getRequestDispatcher("no.jsp").forward(request,response); } %>
3.表的建立并给sid添加主键及自动增长代码
/* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Version : 50045 Source Host : localhost:3306 Source Database : mysql Target Server Type : MYSQL Target Server Version : 50045 File Encoding : 65001 Date: 2021-04-18 21:55:25 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for `stu` -- ---------------------------- DROP TABLE IF EXISTS `stu`; CREATE TABLE `stu` ( `sid` int(10) NOT NULL auto_increment, `uname` varchar(10) default NULL, `upwd` int(10) default NULL, PRIMARY KEY (`sid`) ) ENGINE=InnoDB AUTO_INCREMENT=71 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of stu -- ---------------------------- INSERT INTO `stu` VALUES ('1', 'zs', '567'); INSERT INTO `stu` VALUES ('2', 'ff', '555'); INSERT INTO `stu` VALUES ('3', 'hh', '357'); INSERT INTO `stu` VALUES ('4', 'tt', '567'); INSERT INTO `stu` VALUES ('5', 'rr', '999'); INSERT INTO `stu` VALUES ('6', 'ty', '1111'); INSERT INTO `stu` VALUES ('7', 'xx', '123');
使用分层实现登录。(选做)
1.登录代码
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form name="form1" method="post" action="mysql.jsp" > <table> <tr> <td>用户名:</td> <td> <input type="text" name="uname" id="userName" ></td> </tr> <tr> <td>输入登录密码:</td> <td><input type="password" name="upwd" id="pwd"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"></td> </tr> </table> </form> </body> </html>
2.连接数据库代码
<%@page import="java.nio.channels.SeekableByteChannel"%> <%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@ page import="java.sql.*"%> <html> <head> <title>信息表</title> </head> <body> <%!public static final String DBDRIVER = "com.mysql.jdbc.Driver"; public static final String DBURL = "jdbc:mysql://localhost:3306/mysql"; public static final String DBUSER = "root"; public static final String DBPASS = "root";%> <% Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; boolean flag = false; String name = null; %> <% try { Class.forName(DBDRIVER); conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); String sql = "SELECT uname FROM num_one WHERE uname=? AND upwd=?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, request.getParameter("uname")); pstmt.setString(2, request.getParameter("upwd")); rs = pstmt.executeQuery(); if (rs.next()) { name = rs.getString(1); flag = true; } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (pstmt != null) pstmt.close(); if (conn != null) conn.close(); if (flag) { request.getRequestDispatcher("Copyok.jsp").forward(request,response); } else { response.sendRedirect("Copyno.jsp"); } } catch (Exception e) { e.printStackTrace(); } } %> </body> </html>