本测试基于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);
}}