jsp第十周作业
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接
package bao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class lianDao { // 连接数据库 public Connection getConnection() { Connection con = null; try { Class.forName("com.mysql.jdbc.Driver"); // 建立连接 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf-8", "root", "123456"); } catch (Exception e) { e.printStackTrace(); } return con; } // 关闭数据库 protected void closeAll(Connection con, PreparedStatement ps, ResultSet rs) { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } }
package bao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; public class biao extends lianDao{ public boolean Deng(String stuname, String stupwd) { boolean f = false; Connection con = getConnection(); String sql = "select * from stu where stuname=? and stupwd=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = con.prepareStatement(sql); ps.setString(1, stuname); ps.setString(2, stupwd); rs = ps.executeQuery();//查询 while (rs.next()) { f = true; } } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, rs); } return f; } public void ZC(int stuid,String stuname,String stupwd,int stuage) { Connection con=getConnection(); PreparedStatement ps=null; try { String sql="insert into stu(stuid,stuname,stupwd,stuage) values(?,?,?,?)"; ps=con.prepareStatement(sql); ps.setInt(1, stuid); ps.setString(2, stuname); ps.setString(3, stupwd); ps.setInt(4, stuage); ps.executeUpdate();//增删改 } catch (Exception e) { e.printStackTrace(); }finally{ closeAll(con, ps, null); } } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>index.jsp</title> </head> <body> <form action="pan.jsp" method="post" name="form1"> 用户名:<input name="name" type="text"><br> <br> 密码: <input name="password" type="password"><br> <br> <input type="button" value="登录" onclick="fu()"> <a href="zc.jsp">注册</a> <script type="text/javascript"> function fu() { if (form1.name.value == "") { alert("用户名不能为空!"); return; } if (form1.password.value == "") { alert("密码不能为空!"); return; } form1.submit(); } </script> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>zc.jsp</title> </head> <body> <form action="pan2.jsp" method="post" name="form2"> id: <input type="number" name="id"><br><br> 用户名:<input type="text" name="name"><br><br> 密码: <input type="password" name="password1"><br><br> 确认密码:<input type="password" name="password2"><br><br> 年龄: <input type="number" name="age"><br><br> <input type="button" value="注册" onclick="zu()"> <a href="index.jsp">返回</a> <script type="text/javascript"> function zu() { if (form2.id.value == "") { alert("id不能为空!"); return; } if (form2.name.value == "") { alert("用户名不能为空!"); return; } if (form2.password1.value == "") { alert("密码不能为空!"); return; } if (form2.password2.value == "") { alert("确认密码不能为空!"); return; } if (form2.age.value == "") { alert("年龄不能为空!"); return; } if (form2.password1.value != form2.password2.value) { alert("密码不一致,请重新输入!"); return; } form2.submit(); } </script> </form> </body> </html>
<%@page import="bao.biao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); String password = request.getParameter("password"); biao stu = new biao(); if (stu.Deng(name, password)) { session.setAttribute("name", name); request.getRequestDispatcher("MyJsp.jsp").forward(request, response); } else { out.print("登录失败!"); response.setHeader("refresh", "1;url=index.jsp"); } %> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>MyJsp.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String name = request.getParameter("name"); %> <h4>登录成功!</h4> 欢迎您:<%=name%> </body> </html>
<%@page import="bao.biao"%> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>pan2.jsp</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String id = request.getParameter("id"); int stuid = Integer.parseInt(id); String name = request.getParameter("name"); String password1 = request.getParameter("password1"); String password2 = request.getParameter("password2"); String age = request.getParameter("age"); int stuage = Integer.parseInt(age); if (password1.equals(password2)) { biao stu = new biao(); stu.ZC(stuid, name, password2, stuage); out.print("注册成功!"); response.setHeader("refresh", "1;url=index.jsp"); } else { out.print("两次密码不一致!"); response.setHeader("refresh", "1;url=zc.jsp"); } %> </body> </html>