Spring JdbcTemplate (使用c3po数据源)

本测试基于oracle 数据库的测试,使用oracle自带用户 scott 的dept表


1、加入Spring必要的包、ojdbc6.jar、c3p0-0.8.4.5.jar


2、创建 Dempartment. Pojo类

package com.dxf.pojo;

public class Dempartment {

	private int id;

	private String name;

	private String location;

	public Dempartment() {
		super();
	}

	public Dempartment(int id, String name, String location) {
		super();
		this.id = id;
		this.name = name;
		this.location = location;
	}

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

	public void setName(String name) {
		this.name = name;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getLocation() {
		return location;
	}

	@Override
	public String toString() {
		return "Dempartment [id=" + id + ", name=" + name + ", location=" + location + "]";
	}

}

3、编写db.properties数据库连接的配置文件

jdbcuser=scott
jdbcpassword=tiger
jdbcdriverclass=oracle.jdbc.driver.OracleDriver
jdbcurl=jdbc:oracle:thin:@127.0.0.1:1521:orcl

4、编写配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<bean id="dataSources" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbcurl}"></property>
		<property name="user" value="${jdbcuser}"></property>
		<property name="password" value="${jdbcpassword}"></property>
		<property name="driverClass" value="${jdbcdriverclass}"></property>
		<property name="initialPoolSize" value="5"></property>
		<property name="maxPoolSize" value="100"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSources"></property>
	</bean>
</beans>

5、测试类

package com.dxf.jdbcTemplat;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.dxf.pojo.Dempartment;
import com.dxf.pojo.DempartmentRowMapper;

public class TestJdbcTemplat {

	JdbcTemplate jdbcTemplate = null;

	@Before
	public void testConnect() {

		try {
			ClassPathXmlApplicationContext axt = new ClassPathXmlApplicationContext("applicationContext.xml");

			jdbcTemplate = axt.getBean(JdbcTemplate.class);

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	@Test
	public void TestSelect() throws Exception {

		String sql = "select  deptno  id,dname  name,loc  location from dept where DEPTNO = ?";
		RowMapper<Dempartment> rowMapper = new BeanPropertyRowMapper<Dempartment>(Dempartment.class);
		Dempartment dempartment = (Dempartment) jdbcTemplate.queryForObject(sql, rowMapper, 30);

		System.out.println(dempartment);

	}

	// insert delete update
	@Test
	public void TestInsert() {

		String sql = "insert into dept values(50,'tom','SuZhou') ";

		jdbcTemplate.update(sql);

	}

	@Test
	public void TestUpdate() {

		String sql = "update dept set dname='ttmo' where deptno=50";

		jdbcTemplate.update(sql);

	}

	@Test
	public void TestDelete() {

		String sql = "delete  dept  where deptno=50";

		jdbcTemplate.update(sql);
	}

	@Test
	public void TestBachUpdate() {

		String sql = "insert into dept values(?,?,?) ";

		List<Object[]> batchArgs = new ArrayList<>();

		batchArgs.add(new Object[] { 1, "1", "1" });
		batchArgs.add(new Object[] { 2, "2", "2" });
		batchArgs.add(new Object[] { 3, "3", "3" });
		batchArgs.add(new Object[] { 4, "4", "4" });

		jdbcTemplate.batchUpdate(sql, batchArgs);

	}

	@Test
	public void TestSelectCount() {

		String sql = "select count(*) from dept";

		Long count = jdbcTemplate.queryForObject(sql, Long.class);

		System.out.println(count);

	}}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值