user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver
initialPoolSize=12
maxPoolSize=20
minPoolSize=5
<?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:p="http://www.springframework.org/schema/p"
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.xsd">
<context:property-placeholder location="classpath:dataSourceConfig.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
</bean>
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
package com.zr.entity;
public class User {
private int id;
private String username;
private String password;
private String email;
private String phone;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public User() {
super();
}
public User(int id, String username, String password, String email,
String phone) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", email=" + email + ", phone=" + phone + "]";
}
}
package com.zr.utils;
import java.util.List;
import org.springframework.context.ApplicationContext;
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.zr.entity.User;
public class TemplateOperation {
private ApplicationContext ctc = null;
private JdbcTemplate template = null;
{
ctc = new ClassPathXmlApplicationContext("applicationContext.xml");
template = (JdbcTemplate) ctc.getBean("template");
}
/**
* @return the row number from table
*/
public int queryRowNumber(){
int result = 0;
String sql = "SELECT COUNT(*) FROM USERTEST";
result = template.queryForObject(sql, Integer.class);
return result;
}
/**
* update table data by sql command and parameters
* @param sql sql-command
* @param args
* @return the number how many rows be operated
*/
public int updateUser(String sql,Object...args){
return template.update(sql,args);
}
/**
* query user infomation by username from table
* @param sql sql-command
* @param args
* @return user infomation
*/
public User queryUserByUsername(String sql,Object...args){
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
User user = template.queryForObject(sql, rowMapper, args);
return user;
}
/**
* @return the set storing all data from table
*/
public List<User> queryAllUser(){
String sql = "SELECT * FROM USERTEST";
RowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class);
List<User> list = template.query(sql, rowMapper);
return list;
}
}
package com.zr.utils;
import java.util.List;
import com.zr.entity.User;
public class TestOperation {
public static void main(String[] args) {
TemplateOperation operation = new TemplateOperation();
/*String sql = "INSERT INTO USERTEST VALUES(?,?,?,?,?)";
System.out.println(operation.updateUser(sql,3,"lhh","123","123","123"));*/
String sql = "SELECT * FROM USERTEST WHERE USERNAME=?";
User user1 = operation.queryUserByUsername(sql, "lf");
System.out.println("user1:"+user1);
List<User> list = operation.queryAllUser();
for(User user : list){
System.out.println(user);
}
System.out.println(operation.queryRowNumber());
}
}