第七次作业
BaseDao.java package com.tao.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; public class BaseDao { protected Connection conn = null; protected Statement stmt = null; protected ResultSet rs = null; protected String url = "jdbc:mysql://localhost:3306/school?characterEncoding=utf-8"; protected String name = "root"; protected String password = "201906"; protected PreparedStatement pstmt=null; public void connect(){ try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection(url, "root", "201906"); stmt = conn.createStatement(); } catch(Exception e) { e.printStackTrace(); } } public void closeAll(){ try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } if(pstmt!=null){ pstmt.close(); } }catch(Exception e) { } } }
UserDao.java package com.tao.dao; import com.tao.entity.User; public class UserDao extends BaseDao{ public User dologin(String name,String pass){ User u=null; try { super.connect(); String sql="select * from user where name=? and pass=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, name); pstmt.setString(2,pass); rs=pstmt.executeQuery(); while(rs.next()){ u=new User(); u.setId(rs.getInt(1)); u.setName(rs.getString(2)); u.setPass(rs.getString(3)); u.setAge(rs.getInt(4)); } } catch (Exception e) { e.printStackTrace(); }finally{ super.closeAll(); } return u; } public int insert(User u){ int row=0; try { super.connect(); String sql="insert into user(name,pass,age) values(?,?,?)"; pstmt=conn.prepareStatement(sql); pstmt.setString(1, u.getName()); pstmt.setString(2,u.getPass()); pstmt.setInt(3, u.getAge()); row=pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); }finally{ super.closeAll(); } return row; } }
User.java package com.tao.entity; public class User { private Integer id; private String name; private String pass; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPass() { return pass; } public void setPass(String pass) { this.pass = pass; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
register.jsp <%@ 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="doregister.jsp" method="post"> 用户名:<input name="username" > 密码:<input name="password"> 年龄:<input name="age"> <hr> <input type="submit" value="注册" > <input type="reset" > </form> </body> </html>
doregister.jsp <%@page import="com.tao.dao.UserDao"%> <%@page import="com.tao.entity.User"%> <%@ 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> <% //获取姓名密码和年龄 String name=request.getParameter("username"); String pass=request.getParameter("password"); int age=Integer.parseInt(request.getParameter("age")); //创建User对象,赋值 User u=new User(); u.setName(name); u.setPass(pass); u.setAge(age); UserDao udao=new UserDao(); //调用udao的方法实现插入功能 int x=udao.insert(u); if(x>=1){ //转发,如果>1,跳转到success2.jsp页面 request.getRequestDispatcher("success2.jsp").forward(request, response); } %> </body> </html>
success2.jsp <%@ 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> 注册成功 </body> </html>