BeanUtils、DBUtils和c3p0组件构建一个java web项目
本文是基于Windows 10系统环境,基于BeanUtils、DBUtils和c3p0组件构建一个java web项目
- Windows 10
- MyEclipse 10
- C3P0
- commons-beanutils-1.8.jar
- commons-logging-1.1.3.jar
- commons-dbutils-1.6.jar
- mysql-connector-java-5.1.7-bin.jar
一、目录结构
(1) src的目录结构
(2) WebRoot的目录结构
二、各个文件源码
(0) 配置文件
- db.properties
url=jdbc:mysql://172.30.12.59:3306/day15
user=root
password=123456
driverClass=com.mysql.jdbc.Driver
- c3p0-config.xml
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://172.30.12.59:3306/day15</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="maxIdleTime">3000</property>
<property name="maxPoolSize">6</property>
<property name="initialPoolSize">3</property>
</default-config>
<named-config name="mysql_config">
<property name="jdvcUrl">jdbc:mysql://172.30.12.59:3306/day15</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="maxIdleTime">3000</property>
<property name="maxPoolSize">6</property>
<property name="initialPoolSize">3</property>
<user-overrides user="poop">
<property name="maxStatements">300</property>
</user-overrides>
</named-config>
</c3p0-config>
- web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>AdminServlet</servlet-name>
<servlet-class>com.inspur.servlet.AdminServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AdminServlet</servlet-name>
<url-pattern>/AdminServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
(1) entity实体层
- Admin.java
/**
*
*/
package com.inspur.entity;
/**
* 1.实体类设计
* ClassName: Admin
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 上午9:46:45
*/
public class Admin {
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;
}
@Override
public String toString() {
return "Admin [id=" + id + ", userName=" + userName + ", password="
+ password + "]";
}
}
- UserExistsException.java
/**
*
*/
package com.inspur.exception;
/**
* ClassName: UserExistsException
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 ÏÂÎç2:39:37
*/
public class UserExistsException extends Exception {
/**
*
*/
public UserExistsException() {
// TODO Auto-generated constructor stub
}
/**
* @param message
*/
public UserExistsException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
/**
* @param cause
*/
public UserExistsException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
/**
* @param message
* @param cause
*/
public UserExistsException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
(2) dao数据访问层
- IAdminDao.java
/**
*
*/
package com.inspur.dao;
import com.inspur.entity.Admin;
/**
* 2.数据访问层,接口设计
* ClassName: IAdminDao
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 上午9:51:20
*/
public interface IAdminDao {
/*
* 保存用户
*/
public void save(Admin admin);
/*
* 根据用户名和密码查询
*/
public Admin findByNameAndPwd(Admin admin);
/*
* 检查用户名是否存在
*/
public boolean userExists(String name);
}
- AdminDao.java
/**
*
*/
package com.inspur.dao.impl;
import java.sql.Connection;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.inspur.dao.IAdminDao;
import com.inspur.entity.Admin;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**2.数据访问层,接口实现
* ClassName: AdminDao
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 上午9:58:44
*/
public class AdminDao implements IAdminDao {
private Connection conn = null;
private QueryRunner qr = new QueryRunner();
/* (non-Javadoc)
* @see com.inspur.dao.IAdminDao#save(com.inspur.entity.Admin)
*/
public void save(Admin admin) {
// TODO Auto-generated method stub
String sql = "insert into user(userName,password) values(?,?)";
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
qr.update(conn, sql, admin.getUserName(), admin.getPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} finally{
try {
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
/* (non-Javadoc)
* @see com.inspur.dao.IAdminDao#findByNameAndPwd(com.inspur.entity.Admin)
*/
public Admin findByNameAndPwd(Admin admin) {
// TODO Auto-generated method stub
String sql = "select * from user where userName=? and password=?";
try{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
Admin ad = qr.query(conn, sql, new BeanHandler<Admin>(Admin.class), admin.getUserName(), admin.getPassword());
return ad;
} catch (Exception e){
throw new RuntimeException(e);
} finally {
try {
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
/* (non-Javadoc)
* @see com.inspur.dao.IAdminDao#userExists(java.lang.String)
*/
public boolean userExists(String name) {
// TODO Auto-generated method stub
String sql = "select id from user where userName=?";
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
Integer id = qr.query(conn, sql, new ScalarHandler<Integer>(), name);
if(id != null){
return true;
}
return false;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} finally{
try {
conn.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
}
(3) service业务逻辑层
- IAdminService.java
/**
*
*/
package com.inspur.service;
import com.inspur.entity.Admin;
import com.inspur.exception.UserExistsException;
/**
* 3.业务逻辑层,接口设计
* ClassName: IAdminService
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 下午2:18:43
*/
public interface IAdminService {
/*
* 注册
*/
public void register(Admin admin) throws UserExistsException;
/*
* 登录
*/
public Admin login(Admin admin);
}
- AdminService.java
/**
*
*/
package com.inspur.service.impl;
import com.inspur.dao.IAdminDao;
import com.inspur.dao.impl.AdminDao;
import com.inspur.entity.Admin;
import com.inspur.exception.UserExistsException;
import com.inspur.service.IAdminService;
/**
* ClassName: AdminService
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 下午2:26:14
*/
public class AdminService implements IAdminService {
private IAdminDao dao = new AdminDao();
/* (non-Javadoc)
* @see com.inspur.servlet.IAdminService#register(com.inspur.entity.Admin)
*/
public void register(Admin admin) throws UserExistsException{
// TODO Auto-generated method stub
try {
if(dao.userExists(admin.getUserName())){
System.out.println("用户名已经存在,注册失败");
throw new UserExistsException("用户名已经存在,注册失败");
} else{
dao.save(admin);
}
} catch (UserExistsException e) {
// TODO Auto-generated catch block
throw e;
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
/* (non-Javadoc)
* @see com.inspur.servlet.IAdminService#login(com.inspur.entity.Admin)
*/
public Admin login(Admin admin) {
// TODO Auto-generated method stub
try {
return dao.findByNameAndPwd(admin);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
}
(4) control控制层
- AdminServlet.java
/**
*
*/
package com.inspur.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.inspur.entity.Admin;
import com.inspur.exception.UserExistsException;
import com.inspur.service.IAdminService;
import com.inspur.service.impl.AdminService;
/**
* ClassName: AdminServlet
* Function: TODO ADD FUNCTION
* @author: xuzheng
* date: 2019-5-22 下午2:49:01
*/
public class AdminServlet extends HttpServlet {
private IAdminService service = new AdminService();
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String method = request.getParameter("method");
if("register".equals(method)){
register(request, response);
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
private void register(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{
Admin admin = new Admin();
try {
BeanUtils.populate(admin, request.getParameterMap());
System.out.println(admin);
service.register(admin);
} catch (UserExistsException e) {
// TODO Auto-generated catch block
System.out.println("跳转用户存在页面");
request.setAttribute("message", "用户名已经存在");
request.getRequestDispatcher("/register.jsp").forward(request, response);
} catch (Exception e1) {
System.out.println("跳转错误页面");
response.sendRedirect(request.getContextPath() + "/error/error.jsp");
}
}
}
(5) view视图层
- index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
</body>
</html>
- register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
<form name="form1" action="${pageContext.request.contextPath}/AdminServlet?method=register" method="post">
<table>
<tr>
<td>用户名</td>
<td>
<input type="text" name="userName"/>
${requestScope.message}
</td>
</tr>
<tr>
<td>密 码</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
</form>
</body>
</html>
- error.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>错误页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
系统忙,稍后再试!
</body>
</html>