费用管理系统:使用servlet实现
一、环境配置--IDEA
1. c3p0数据库连接技术
1) c3p0配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<!-- 数据库驱动 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<!-- 数据库连接 -->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/cost?useUnicode=true&characterEncoding=utf-8</property>
<!-- 数据库用户名 -->
<property name="user">root</property>
<!-- 数据库密码 -->
<property name="password">root</property>
<!-- 初始化数据库连接数量大小 -->
<property name="initialPoolSize">5</property>
<!-- 设置连接池的最大连接数量 -->
<property name="maxPoolSize">20</property>
<!-- 设置连接的最大空闲时间,单位是秒 -->
<property name="maxIdleTime">1000</property>
</default-config>
</c3p0-config>
2) c3p0Utils:使用QueryRunner进行查询 且使用c3p0连接池
https://blog.csdn.net/weixin_36208125/article/details/82191353 作者:胡萝卜88号
https://www.cnblogs.com/nizuimeiabc1/p/7841385.html 作者:weixin_38465623 dbutil包的简单使用(主要对应方法名的解释)
package com.nm.utils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Util {
// 获取连接池
private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
// 获取连接
private static QueryRunner qr=new QueryRunner(dataSource);
//私有化构造函数
private C3p0Util(){
}
//提供一个外部静态的访问方法
public static ComboPooledDataSource getInstance(){
return dataSource;
}
//获取连接的方法
public static Connection getConn() throws SQLException{
return dataSource.getConnection();
}
/**
* 通用的查询多条数据的方法
* @param sql
* @param t
* @param params
* @return
* @throws SQLException
*/
public static <T>List<T> queryList(String sql,Class<T> t,Object...params)
throws SQLException{
List<T> list=qr.query(sql, new BeanListHandler<T>(t), params);
return list;
}
/**
* 查询一个对象的方法
* @param sql
* @param t
* @param params
* @return
* @throws SQLException
*/
public static <T>T queryOne(String sql,Class<T> t,Object...params)
throws SQLException{
T entity=qr.query(sql, new BeanHandler<T>(t), params);
return entity;
}
/**
* 查询一个数值型的数据的方法
* @param sql
* @param t
* @param params
* @return
* @throws SQLException
*/
public static <T>T queryNumber(String sql,Class<T> t,Object...params)
throws SQLException{
T entity=qr.query(sql, new ScalarHandler<T>(), params);
return entity;
}
/**
* 通用的执行更新数据的方法,包括增加、修改、删除
* @param sql
* @param params
* @return
* @throws SQLException
*/
public static int update(String sql,Object...params)
throws SQLException{
int rows=qr.update(sql, params);
return rows;
}
}
2. Servlet逻辑目录:
- dao--持久层
- service-逻辑层
- web(servlet)-控制层
3. 从页面