BeanUtils、DBUtils和c3p0组件构建一个java web项目
本文是基于Windows 10系统环境,基于BeanUtils、DBUtils和c3p0组件构建一个java web项目
Windows 10MyEclipse 10C3P0commons-beanutils-1.8.jarcommons-logging-1.1.3.jarcommons-dbutils-1.6.jarmysql-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
;
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
;
public class UserExistsException extends Exception {
public UserExistsException() {
}
public UserExistsException(String message
) {
super(message
);
}
public UserExistsException(Throwable cause
) {
super(cause
);
}
public UserExistsException(String message
, Throwable cause
) {
super(message
, cause
);
}
}
(2) dao数据访问层
IAdminDao.java
package com
.inspur
.dao
;
import com
.inspur
.entity
.Admin
;
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
;
public class AdminDao implements IAdminDao {
private Connection conn
= null
;
private QueryRunner qr
= new QueryRunner();
public void save(Admin admin
) {
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
) {
e
.printStackTrace();
throw new RuntimeException(e
);
} finally{
try {
conn
.close();
} catch (Exception e
) {
throw new RuntimeException(e
);
}
}
}
public Admin
findByNameAndPwd(Admin admin
) {
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
) {
throw new RuntimeException(e
);
}
}
}
public boolean userExists(String name
) {
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
) {
e
.printStackTrace();
throw new RuntimeException(e
);
} finally{
try {
conn
.close();
} catch (Exception e
) {
throw new RuntimeException(e
);
}
}
}
}
(3) service业务逻辑层
IAdminService.java
package com
.inspur
.service
;
import com
.inspur
.entity
.Admin
;
import com
.inspur
.exception
.UserExistsException
;
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
;
public class AdminService implements IAdminService {
private IAdminDao dao
= new AdminDao();
public void register(Admin admin
) throws UserExistsException
{
try {
if(dao
.userExists(admin
.getUserName())){
System
.out
.println("用户名已经存在,注册失败");
throw new UserExistsException("用户名已经存在,注册失败");
} else{
dao
.save(admin
);
}
} catch (UserExistsException e
) {
throw e
;
} catch (Exception e
) {
throw new RuntimeException(e
);
}
}
public Admin
login(Admin admin
) {
try {
return dao
.findByNameAndPwd(admin
);
} catch (Exception e
) {
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
;
public class AdminServlet extends HttpServlet {
private IAdminService service
= new AdminService();
public void doGet(HttpServletRequest request
, HttpServletResponse response
)
throws ServletException
, IOException
{
String method
= request
.getParameter("method");
if("register".equals(method
)){
register(request
, response
);
}
}
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
) {
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
>