首先使用mysql建立数据库,并建立表如下:
CREATE TABLE `login` (
`username` varchar(10) default NULL,
`passwd` varchar(10) default NULL,
`address` varchar(10) default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
Spring的JDBC框架承担了资源管理和错误处理的重担,使你的JDBC代码非常干净,这就是spring为我们提供的模板类- JdbcTemplate,他是线程安全的
首先,我们编写配置文件,这里数据库链接部队使用了apache的pool和DBCP作为连接池
我们为PersonDAO这个数据库操作类,注入JdbcTemplate
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
< beans >
< bean id ="dataSource" class ="org.apache.commons.dbcp.BasicDataSource" >
< property name ="driverClassName" >
< value > com.mysql.jdbc.Driver </ value >
</ property >
< property name ="url" >
< value > jdbc:mysql://localhost:3306/javaee </ value >
</ property >
< property name ="username" >
< value > root </ value >
</ property >
< property name ="password" >
< value > 1234 </ value >
</ property >
</ bean >
< bean id ="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate" >
< property name ="dataSource" >
< ref local ="dataSource" />
</ property >
</ bean >
< bean id ="personDAO" class ="SpringJDBCSupport.WriteData.PersonDAO" >
< property name ="jdbcTemplate" >
< ref local ="jdbcTemplate" />
</ property >
</ bean >
</ beans >
下面是我们的javaBean
import com.mysql.jdbc.Driver;
public class Person {
private String name;
private String password;
private String address;
public Person(String name,String password,String address) {
this .name = name;
this .password = password;
this .address = address;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this .address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this .name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this .password = password;
}
}
这是我们的数据库访问类:
包含了三个方法,其中execute方法是有三个参数,sql,params,type,定义type表示这是一个类型安全的方法
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
public class PersonDAO {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this .jdbcTemplate = jdbcTemplate;
}
public int insertPersonUseUpdate(Person person) {
String sql = " insert into login values(?,?,?) " ;
Object[] params = new Object[] {
person.getName(),
person.getPassword(),
person.getAddress()
} ;
return this .getJdbcTemplate().update(sql,params);
}
public int insertPersonUseExecute(Person person) {
String sql = " insert into login values(?,?,?) " ;
Object[] params = new Object[] {
person.getName(),
person.getPassword(),
person.getAddress()
} ;
int [] types = new int [] {
Types.VARCHAR,
Types.VARCHAR,
Types.VARCHAR
} ;
return this .getJdbcTemplate().update(sql,params,types);
}
public int [] updatePersonUseBatchUpdate( final List persons) {
String sql = " insert into login values(?,?,?) " ;
BatchPreparedStatementSetter setter = null ;
setter = new BatchPreparedStatementSetter() {
public int getBatchSize() {
return persons.size();
}
public void setValues(PreparedStatement ps, int index) throws SQLException {
Person person = (Person)persons.get(index);
ps.setString( 1 ,person.getName());
ps.setString( 2 ,person.getPassword());
ps.setString( 3 ,person.getAddress());
}
} ;
return this .getJdbcTemplate().batchUpdate(sql,setter);
}
}
测试代码:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestJDBCTemplate {
public static String filePath = "" ;
public static BeanFactory factory = null ;
public static void main(String[] args) {
filePath = System.getProperty( " user.dir " ) + File.separator + " SpringJDBCSupport " + File.separator + " WriteData " + File.separator + " hello.xml " ;
factory = new XmlBeanFactory( new FileSystemResource(filePath));
PersonDAO personDAO = (PersonDAO)factory.getBean( " personDAO " );
/*
* 准备数据
*/
Person p1 = new Person( " test1 " , " test1 " , " test1 " );