JSP-2020年4月14日-第七周
- 注册页面(regist.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="doregist.jsp" method="post"> 账号:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit" value="注册"> </form> </body> </html>
- 注册处理(doregist.jsp)
<%@ page import="main.java.DAO.UserDAO" %> <%@ page import="main.java.entity.UserBean" %> <%@ page import="java.io.PrintWriter" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <% String username = request.getParameter("username"); String password = request.getParameter("password"); //注册功能 UserBean userBean = new UserBean(); userBean.setUsername(username); userBean.setPassword(password); UserDAO userDAO = new UserDAO(); Integer result = userDAO.insert(userBean); System.out.println(result); if (result>0){//注册成功 response.sendRedirect("success.jsp");//跳转至成功页面 } %> </body> </html>
- 注册成功(success.jsp)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> 注册成功 </body> </html>
JavaBean(UserBean)
package main.java.entity; public class UserBean { private int uid; private String username; private String password; @Override public String toString() { return "entity.UserBean{" + "uid=" + uid + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public UserBean() { } public UserBean(int uid, String username, String password) { this.uid = uid; this.username = username; this.password = password; } public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
DAO层(UserDAO)
package main.java.DAO; import main.java.entity.UserBean; import main.java.util.DBUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDAO { //新增 public int insert(UserBean userBean) { Connection connection = null; PreparedStatement preparedStatement = null; String sql = "insert into tb_user(username,password)value(?,?)"; try { connection = DBUtils.getConnection(); preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1,userBean.getUsername()); preparedStatement.setString(2,userBean.getPassword()); Integer result = preparedStatement.executeUpdate(); return result; } catch (SQLException e) { e.printStackTrace(); }finally { DBUtils.closeAll(connection,preparedStatement,null); } return 0; } }
补充:
数据库
# 建库 create database jsp; use jsp; # 建表 create table tb_user( uid int(10) primary key auto_increment, username varchar(10) not null , password varchar(10) not null )default charset utf8;
工具类(DBUtils)
package main.java.util; import javax.swing.plaf.nimbus.State; import java.sql.*; public class DBUtils { static { try { //1、加载驱动 Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } //2、获得连接 public static Connection getConnection() throws SQLException { Connection connection = null; connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp","root","root"); return connection; } //3、关闭连接 public static void closeAll(Connection connection, Statement statement, ResultSet resultSet){ try{ if (resultSet!=null){resultSet.close();} if (statement!=null){statement.close();} if (connection!=null){connection.close();} }catch (SQLException e){ e.printStackTrace(); } } }