java连接池 dbcp 样例_Spring dbcp连接池配置与示例(以及JDBCTemplate的使用)

Apache的DBCP

首先要导入的jar包:

dbpc需要的包:

0472e09c5f21420e80cbda642b9a5b28.png

除了Spring核心包之外的jar包:

391828cbb85a9558a81c18047e1604f3.png

我们要做的示例:(利用dbcp连接池实现对t_student表的增删改查)

废话不多少,上xml配置

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd">

jdbc.properties文件

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/hibernate?characterEncoding=GBK

jdbc.username=root

jdbc.password=

model类

packagecom.maya.model;public classStudent {private intid;privateString name;private intage;publicStudent() {super();//TODO Auto-generated constructor stub

}public Student(String name, intage) {super();this.name =name;this.age =age;

}public Student(int id, String name, intage) {super();this.id =id;this.name =name;this.age =age;

}public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public intgetAge() {returnage;

}public void setAge(intage) {this.age =age;

}

@OverridepublicString toString() {return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";

}

dao类接口就不在这里贴了,直接贴其的实现类

packagecom.maya.daoImp;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowCallbackHandler;importcom.maya.dao.StudentDao;importcom.maya.model.Student;public class StudentDaoImpl implementsStudentDao{//利用Spring的jdbc对其进行crud操作

privateJdbcTemplate jdbcTemplate;public voidsetJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate =jdbcTemplate;

}

@Overridepublic intaddStudent(Student student) {

String sql="insert into t_student values(null,?,?)";//因为Spring的jdbc对原生jdbc进行了封装,在这里给出的是数组方式,当然其底层也是需要将数组进行遍历

Object []params=newObject[]{student.getName(),student.getAge()};returnjdbcTemplate.update(sql,params);

}

@Overridepublic intupdateStudent(Student student) {

String sql="update t_student set name=?,age=? where id=?";

Object []params=newObject[]{student.getName(),student.getAge(),student.getId()};//传入两个参数

returnjdbcTemplate.update(sql,params);

}

@Overridepublic int deleteStudent(intid) {

String sql="delete from t_student where id=?";

Object []params=newObject[]{id};returnjdbcTemplate.update(sql,params);

}

@Overridepublic ListfindStudents() {

String sql="select * from t_student";//这里的list对象必须是个final常量

final List studentList=new ArrayList();//同样也是传入两个参数,//第二个参数:是一个回掉方法,我们要实现它的processRow方法

jdbcTemplate.query(sql, newRowCallbackHandler(){

@Overridepublic void processRow(ResultSet rs) throwsSQLException {//同jdbc一样也是要在其中把结果集遍历出来,赋值给新对象

Student student=newStudent();

student.setId(rs.getInt("id"));

student.setName(rs.getString("name"));

student.setAge(rs.getInt("age"));

studentList.add(student);

}

});returnstudentList;

}

}

service的接口也不再这里贴了,直接贴其实现类

packagecom.maya.serviceImp;importjava.util.List;importcom.maya.dao.StudentDao;importcom.maya.model.Student;importcom.maya.service.StudentService;public class StudentServiceImpl implementsStudentService{privateStudentDao studentDao;public voidsetStudentDao(StudentDao studentDao) {this.studentDao =studentDao;

}//当调用该逻辑时,将其执行的结果条数返回

@Overridepublic intaddStudent(Student student) {returnstudentDao.addStudent(student);

}

@Overridepublic intupdateStudent(Student student) {returnstudentDao.updateStudent(student);

}

@Overridepublic int deleteStudent(intid) {returnstudentDao.deleteStudent(id);

}

@Overridepublic ListfindStudents() {returnstudentDao.findStudents();

}

}

测试类

packagecom.maya.test;importjava.util.List;importorg.junit.Before;importorg.junit.Test;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importcom.maya.model.Student;importcom.maya.service.StudentService;importjunit.framework.TestCase;public class T extendsTestCase {privateApplicationContext ac;

@Before//利用前置通知执行

public void setUp() throwsException {

ac=new ClassPathXmlApplicationContext("beans.xml");

}

@Testpublic voidtestaddStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");int addNums=studentService.addStudent(new Student("王五", 1));if(addNums==1){

System.out.println("添加成功");

}

}

@Testpublic voidtestupdateStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");int updateNums=studentService.updateStudent(new Student(3,"王五2", 2));if(updateNums==1){

System.out.println("更新成功");

}

}

@Testpublic voidtestdeleteStudent() {

StudentService studentService=(StudentService)ac.getBean("studentService");int deleteNums=studentService.deleteStudent(3);if(deleteNums==1){

System.out.println("删除成功");

}

}

@Testpublic voidtestfindStudents() {

StudentService studentService=(StudentService)ac.getBean("studentService");

List studentList=studentService.findStudents();for(Student student:studentList){

System.out.println(student);

}

}

}

*****************************************************************************************************************

如何使用JDBCTemplate(在上面已经列出了其增删改查的使用方式,这里在介绍一下xml文件中的配置和查询单个对象,多个对象集合)

查询

packagecom.itnba.maya.test;importjava.sql.Connection;importjava.sql.SQLException;importjava.util.ArrayList;importjava.util.List;importjavax.sql.DataSource;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.jdbc.core.BeanPropertyRowMapper;importorg.springframework.jdbc.core.JdbcTemplate;importorg.springframework.jdbc.core.RowMapper;importcom.itnba.maya.entities.Nation;importcom.mchange.v2.c3p0.ComboPooledDataSource;public classTestC3P0 {public static void main(String[] args) throwsException{

ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");

DataSource ds= (DataSource) context.getBean("dataSource");

JdbcTemplate j= (JdbcTemplate)context.getBean("jdbcTemplate");

String sql= "select count(*) from nation";//查询单行单列

long count = j.queryForObject(sql, Long.class);

System.out.println(count);

}public static void main77(String[] args) throwsException{

ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");

DataSource ds= (DataSource) context.getBean("dataSource");

JdbcTemplate j= (JdbcTemplate)context.getBean("jdbcTemplate");

String sql= "select * from nation";//查询多行

RowMapper rowMapper = new BeanPropertyRowMapper(Nation.class);

List list =j.query(sql, rowMapper);for(Nation data : list){

System.out.println(data);

}

}public static void main66(String[] args) throwsException{

ApplicationContext context= new ClassPathXmlApplicationContext("beans.xml");

DataSource ds= (DataSource) context.getBean("dataSource");

JdbcTemplate j= (JdbcTemplate)context.getBean("jdbcTemplate");

String sql= "select * from nation where code=?";//查询单个

RowMapper rowMapper = new BeanPropertyRowMapper(Nation.class);

Nation data= j.queryForObject(sql,rowMapper,"n003");

System.out.println(data);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值