第十周作业
数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄)
1.设计一个注册页面,实现用户注册功能
2.设计一个登陆页面,实现用户名密码登陆
3.两个页面可以互相超链接
Login页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <script type="text/javascript"> function validate(){ if(loginForm.uname.value==""){ alert("账号不能为空!"); return; } if(loginForm.upwd.value==""){ alert("密码不能为空!"); return; } loginForm.submit(); } </script> <form name="loginForm" action="LoginGo.jsp" method="post"> <b>用户名</b> <input type="text" name="uname" /> <br /> <b>密码</b> <input type="text" name="upwd" /> <br /> <input type="submit" value="登录" onClick="validate()"/> <a href="Res.jsp">注册</a> <a href="Del.jsp">删除</a> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="jdbc.StuDao"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); StuDao sd = new StuDao(); if (sd.login(uname, upwd)) { request.getRequestDispatcher("true.jsp").forward(request, response); } else { request.getRequestDispatcher("false.jsp").forward(request, response); } %> </body> </html>
<%@ page language="java" contentType="text/html;charset=utf-8" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="po.*"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 登录成功 <a href="Login.jsp">返回</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <h1>登录失败,返回登录界面</h1> <% response.setHeader("refresh", "1;url=Login.jsp"); %> </body> </html>
Res注册页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="ResGo.jsp" method="post"> <b>用户名</b> <input type="text" name="uname" /> <br /> <b>密码</b> <input type="text" name="upwd" /> <br /> <b>性别</b> <input type="text" name="usex"/> <br /> <input type="submit" value="注册" /> <a href="Login.jsp">登录</a> <a href="Del.jsp">删除</a> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="jdbc.StuDao"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); String usex = request.getParameter("usex"); StuDao sd = new StuDao(); sd.reg(uname, upwd, usex); request.getRequestDispatcher("Login.jsp").forward(request, response); %> </body> </html>
Del删除页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <form action="DelGo.jsp" method="post"> <b>输入要删除的用户名</b> <input type="text" name="uname" /> <br /> <input type="submit" value="删除" /> <a href="Login.jsp">登录</a> <a href="Res.jsp">注册</a> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="jdbc.StuDao"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("utf-8"); String uname = request.getParameter("uname"); StuDao sd = new StuDao(); if (sd.del(uname)) { request.getRequestDispatcher("deltrue.jsp").forward(request, response); } else { request.getRequestDispatcher("delfalse.jsp").forward(request, response); } %> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 删除成功<br> <a href="Login.jsp">登录</a> <a href="Res.jsp">注册</a> <a href="Del.jsp">删除</a> </body> </html>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 删除失败<br> <a href="Login.jsp">登录</a> <a href="Res.jsp">注册</a> <a href="Del.jsp">删除</a> </body> </html>
java代码
package jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class StuDao extends BaseDao { public boolean login(String name, String pwd) { boolean f = false; Connection conn = getConnection(); String sql = "select * from stu where name=? and pwd=?"; PreparedStatement ps = null; ResultSet rs = null; try { ps = conn.prepareStatement(sql); ps.setString(1, name); ps.setString(2, pwd); rs = ps.executeQuery(); if (rs.next()) { f = true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, rs); } return f; } public void reg(String uname, String upwd ,String sex) { Connection conn = getConnection(); PreparedStatement ps = null; try { String sql = "insert into stu(name,pwd,sex) values(?,?,?)"; // 2个占位符 // 4.执行SQL语句 ps = conn.prepareStatement(sql); ps.setString(1, uname); ps.setString(2, upwd); ps.setString(3, sex); ps.executeUpdate();// 增删改都用这个 } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } } public boolean del(String uname) { boolean f = false; Connection conn = getConnection(); PreparedStatement ps = null; ResultSet rs = null; try { String sql = "delete from stu where name=?"; // 4.执行SQL语句 ps = conn.prepareStatement(sql); ps.setString(1, uname); int i=ps.executeUpdate();// 增删改都用这个 if (i>0) f = true; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { closeAll(conn, ps, null); } return f; } }
package jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; public class BaseDao { //获取连接 protected Connection getConnection(){ Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.建立连接 conn = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mycollege?useSSL=false", "root", "root"); } catch (Exception e) { e.printStackTrace(); } return conn; } //关闭连接 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 (SQLException e) { e.printStackTrace(); } } }
package po; public class stu { private int id; private String name; private String pwd; private String sex; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public stu(int id, String name, String pwd, String sex) { super(); this.id = id; this.name = name; this.pwd = pwd; this.sex = sex; } public stu() { super(); } @Override public String toString() { return "stu [id=" + id + ", name=" + name + ", pwd=" + pwd + ", sex=" + sex + "]"; } }