JSP第十周作业

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

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

CREATE TABLE `week10`.`stu` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '',
  `password` varchar(255) NOT NULL DEFAULT '',
  `age` int(11) NOT NULL DEFAULT 0,
  PRIMARY KEY (`Id`)
);
package week10;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class StudentDao {
	public static Connection CreateConnection()
	{
		try
		{
			String driver = "com.mysql.jdbc.Driver";
			Class.forName(driver).newInstance();
			System.out.println("test");
			Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/week10", "root", "1234567");
			return conn;
		}
		catch (Exception e)
		{
			System.out.println("CreateConnection");
			e.printStackTrace();
			return null;
		}
	}
	
	public static void closeConn(Connection conn)
	{
		try
		{
			conn.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public static void register(String name, String password, int age)
	{
		
		Connection conn = null;

		try
		{
			conn = CreateConnection();
			java.sql.Statement sqlStatement = conn.createStatement();
			String sqlQuery = "INSERT INTO `stu` SET `name`='" + name + "',`password`='" + password + "',`age`=" + age + ";";
			sqlStatement.executeQuery(sqlQuery);
		}
		catch (java.sql.SQLException throwables)
		{
			throwables.printStackTrace();
		}
		finally
		{
			if (conn != null)
			{
				closeConn(conn);	
			}
		}
	}
	
	public static boolean login(String name, String password)
	{
		
		Connection conn = null;
		
		
		
		
		int count = 0;
		
		try
		{
			conn = CreateConnection();
			java.sql.Statement sqlStatement = conn.createStatement();
			String sqlQuery = "select count(*) from stu where name = '"+ name +"', password = '" + password + "';";
			ResultSet rs = sqlStatement.executeQuery(sqlQuery);
			while (rs.next() != false)
			{
				count = rs.getInt(1);
			}			
		}
		catch (java.sql.SQLException throwables)
		{
			throwables.printStackTrace();
		}
		finally
		{
			if (conn != null)
			{
				closeConn(conn);	
			}
		}
		return (count > 0);
	}
}

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Register</title>
<script>
function verify() {
	let user = document.getElementById("username").value;
	let password = document.getElementById("password").value;
	let age = document.getElementById("age").value;
	if (user == '' || password == '' || age == '') {
		alert("用户名, 密码与年龄不能为空.");
		return;
	}
	loginForm.submit();
}
</script>
</head>
<body>
<form action="register_process.jsp" method="POST" name="loginForm">
用户名: <input type="text" id="username" name="username"><br/>
密码: <input type="password" id="password" name="password"><br/>
年龄: <input type="number" id="age" name="age"><br/>
<input type="button" name="submitButton" value="提交" onclick="verify()">
<a href="login.jsp">login page</a>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    
<%@ page import="week10.StudentDao" %>
<!DOCTYPE html>
<html>
<head>

<title>Register Process</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	String age = request.getParameter("age");
	
	StudentDao.register(username, password, Integer.parseInt(age));
	out.println("<h1>注册成功</h1>");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Login</title>
<script>
function verify() {
	let user = document.getElementById("username").value;
	let password = document.getElementById("password").value;
	if (user == '' || password == '') {
		alert("用户名与密码不能为空.");
		return;
	}
	loginForm.submit();
}
</script>
</head>
<body>
<form action="login_process.jsp" method="POST" name="loginForm">
用户名: <input type="text" id="username" name="username"><br/>
密码: <input type="password" id="password" name="password"><br/>
<input type="button" name="submitButton" value="提交" onclick="verify()">
<a href="register.jsp">register page</a>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>

<%@ page import="week10.StudentDao" %>
<!DOCTYPE html>
<html>
<head>

<title>Login Process</title>
</head>
<body>
<%
	request.setCharacterEncoding("utf-8");
	String username = request.getParameter("username");
	String password = request.getParameter("password");
	if (StudentDao.login(username, password))
	{
		out.println("<h1>登录成功</h1>");
	}
	else
	{
		out.println("<h1>登录失败</h1>");
	}
%>


</body>

</html>

posted @ 2022-05-08 14:08  SuoJing  阅读(3)  评论(0编辑  收藏  举报