使用c3p0连接池连接数据库
示例(mvc模式)
1.mysql.properties (连接数据库的)文件:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=false&serverTimezone=UTC&characterEncoding=utf8&useUnicode=true
user=root
password=root
2.c3p0-config.xml 文件(配置在src根目录下):
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///test</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</default-config>
<named-config name="oracleConfig">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///test</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>
3.util 类:
public class JDBCUtils {
// 初始化连接池
private static DataSource dataSource;
static {
dataSource=new ComboPooledDataSource();
}
public static DataSource getDataSource() {
return dataSource;
}
/**
* 创建DbUtils常用工具类对象
*/
public static QueryRunner getQuerrRunner() {
return new QueryRunner(dataSource);
}
}
注意:区别
4.dao 层:
/**
* 2. 菜系模块,dao接口设计
*/
public interface StudentTypeDao {
public void save(StudentType studentType);//添加
public void update(StudentType studentType);//更新
public void delete(int id);//删除
public StudentType findById(int id);//根据主键查询
public List<StudentType> getAll();//查询全部
public List<StudentType> getAllForName(String sName);//根据名称查询
}
5.dao 的实现:
/**
* 2. 学生系模块dao实现
*/
public class StudentTypeDaoimpl implements StudentTypeDao{
@Override
public void save(StudentType studentType) {//增加
//SQL语句
String sql="insert into student_list(id,sName,sex,major,nianji) values(?,?,?,?,?)";
try {//抛异常
//把得到的值放入一个数组
Object params[]= {studentType.getId(),studentType.getsName(),
studentType.getSex(),studentType.getMajor(),studentType.getNianji()};
//调用util类的getQuerrRunner()方法
JDBCUtils.getQuerrRunner().update(sql,params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void update(StudentType studentType) {//更新
String sql="update student_list set sName=?,major=?,nianji=? where id=?";
try {
Object params[]={studentType.getsName(),
studentType.getMajor(),studentType.getNianji(),studentType.getId()};
JDBCUtils.getQuerrRunner().update(sql,params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void delete(int id) {
String sql="delete from student_list where id=?";
try {
JDBCUtils.getQuerrRunner().update(sql, id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public StudentType findById(int id) {
String sql="select * from student_list where id=?";
try {
return JDBCUtils.getQuerrRunner().query(sql, new BeanHandler<StudentType>(StudentType.class), id);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<StudentType> getAll() {
String sql="select * from student_list";
try {
List<StudentType> list = JDBCUtils.getQuerrRunner().query(sql, new BeanListHandler<StudentType>(StudentType.class));
return list;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public List<StudentType> getAllForName(String sName) {
String sql="select * from student_list where sName like ?";
try {
return JDBCUtils.getQuerrRunner().query(sql, new BeanListHandler<StudentType>(StudentType.class), "%"+sName+"%");
} catch (Exception e) {;
throw new RuntimeException(e);
}
}
}
用到的jar包:
百度网盘:
链接:https://pan.baidu.com/s/1hiied02Gv53yGGxedJEz1A
提取码:u7fi