从业务里抽出实体类,并添加service层!管理user的业务逻辑。
1. 主要文件
|
2. registerDeal.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="java.sql.Connection" %> <%@ page import="java.sql.DriverManager" %> <%@ page import="java.sql.PreparedStatement" %> <%@ page import="java.sql.SQLException" %> <%@ page import="com.edu.ssh.model.User" %> <%@ page import="com.edu.ssh.service.UserManager" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html>
<%
String username = request.getParameter("username").trim(); String password = request.getParameter("password").trim(); String password2 = request.getParameter("password2").trim(); User user = new User(); user.setUsername(username); user.setPassword(password); UserManager userManager = new UserManager();
// 简单验证,不填其他的复杂逻辑 if (!userManager.exists(user)) { userManager.insertOneUser(user); //response.sendRedirect("registerSucess.jsp"); request.setAttribute("username", username); request.getRequestDispatcher("registerSucess.jsp").forward(request, response); } else { //response.sendRedirect("registerFail.jsp"); request.getRequestDispatcher("registerFail.jsp").forward(request, response); }
%>
</html> |
3. UserManager.java
package com.edu.ssh.service;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import com.edu.ssh.model.User;
public class UserManager {
private static String url = "jdbc:mysql://localhost:3306/ssh"; private static String dbusername = "root"; private static String pw = "mysql123";
public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } try { return DriverManager.getConnection(url, dbusername, pw); } catch (SQLException e) { e.printStackTrace(); }
return null; }
public void closeAll(PreparedStatement pstat, Connection conn) { if (null != null) { try { pstat.close(); } catch (SQLException e) { e.printStackTrace(); } }
if (null != conn) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public boolean exists(User user) { String selectSql = "select count(*) from tb_user where username=?"; Connection conn = this.getConnection(); PreparedStatement pstat = null;
try { pstat = conn.prepareStatement(selectSql); pstat.setString(1, user.getUsername()); ResultSet rs = pstat.executeQuery(); rs.next(); int count = rs.getInt(1); if (count > 0) {
return true; } } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(pstat, conn); }
return false; }
public void insertOneUser(User user) { String sql = "insert into tb_user(id, username, password) values(null, ?, ?)"; Connection conn = this.getConnection(); PreparedStatement pstat = null; try { pstat = conn.prepareStatement(sql); pstat.setString(1, user.getUsername()); pstat.setString(2, user.getPassword()); pstat.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } finally { this.closeAll(pstat, conn); } } } |
4. User.java
package com.edu.ssh.model;
public class User { private int id; private String username; private String password;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
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; } }
|
5. UserManagerTest
package com.edu.ssh.service;
import static org.junit.Assert.*;
import org.junit.After; import org.junit.Before; import org.junit.Test;
import com.edu.ssh.model.User;
public class UserManagerTest {
private UserManager userManager = null;
@Before public void setUp() throws Exception { userManager = new UserManager(); }
@After public void tearDown() throws Exception { userManager = null; }
@Test public void testExists() { User user = new User(); user.setUsername("admin"); user.setPassword("admin"); assertTrue("exists() 方法失败了!!", userManager.exists(user)); }
@Test public void testInsertOneUser() { User user = new User(); user.setUsername("adminff"); user.setPassword("adminff"); userManager.insertOneUser(user); assertTrue("insertOneUser() 方法失败了!!", userManager.exists(user)); }
} |
6. 结果
|
|
|
|
7. UML 图
|
8. 分析
版权声明:本文为博主原创文章,未经博主允许不得转载。