JSP第七周作业
用户表: uid (主键,自动增长) uname upwd
使用分层实现注册。(必做)
使用分层实现登录。(选做)
jm.jsp
<%@ 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>jm</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="control.jsp"> <table> <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> <td colspan="2" style=""><input type="submit" value="登录"> </td> </tr> </table> </form> </body> </html>
zc.jsp
<%@ page import="com.bean.pyh.UserBean"%> <%@ page import="com.dao.pyh.Dao"%> <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% Stu s = new Stu(); int id = Integer.parseInt(request.getParameter("sid")); s.setUid(id); 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) { out.print("注册成功"); } else { out.print("注册失败"); } %>
stu.java
package com.pyh.UserBean; public class stu { private int uid; private String uname; private String upwd; public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUpwd() { return upwd; } public void setUpwd(String upwd) { this.upwd = upwd; } public Stu(int uid, String uname, String upwd) { super(); this.uid = uid; this.uname = uname; this.upwd = upwd; } public Stu() { super(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }
stuDao.java
package com.pyh.Dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import com.pyh.bean.Stu; public class StuDao { public int addStu(Stu s) { int i = 0; try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection( "jdbc:mysql://localhost:3306/mysql", "root", "root"); String sql = "insert into stu values(?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, s.getUname()); ps.setString(2, s.getUpwd()); i = ps.executeUpdate(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return i; } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub } }