怎么样使用JdbcTemplate连接数据库?
老张说:什么叫框架? 原本自己要写的程序现在用框架写变成写 XML配置文件了.
下面就示例一个程序,用xml配制文件和JdbcTemplate来连接一个数据库(mysql)实现下面三个功能:
1:Spring中用JdbcTemplate的update插入一条数据
2:Spring中用JdbcTemplate的batchUpdate插入一组数据
3:Spring中用JdbcTemplate的RowMapper查询数据
1:首先写一个Student类
package cn.itcast;
public class Student {
private int id;
private String name;
private String sex;
private int salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
2:写一个设置属性的类并继承BatchPreparedStatementSetter这个接口,并实现它的setValues方法
package cn.itcast;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
public class InsertPersonStatementCreator implements
BatchPreparedStatementSetter {
List list;
public void setValues(PreparedStatement arg0, int arg1) throws SQLException {
Student stu = (Student) list.get(arg1);
arg0.setString(1, stu.getName());
arg0.setString(2, stu.getSex());
arg0.setInt(3, stu.getSalary());
}
public int getBatchSize() {
return list.size();
}
public void setList(List list) {
this.list = list;
}
}
3:在写一个客户端调用程序
由于时间的因素,没有把三个实现功能分开写,读者自己实验时可以分开写.
package cn.itcast;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
public class JdbcTempleDemo {
private final static String sql = "insert into student (name,sex,salary)values(?,?,?)";
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
JdbcTemplate jt = (JdbcTemplate) context.getBean("jt");
InsertPersonStatementCreator ipstc = (InsertPersonStatementCreator) context
.getBean("config");
// 1:Spring中用JdbcTemplate的update插入一条数据
// jt.update("insert into student
// (name,sex,salary)values('bcd','m',1500) ");
// 2:Spring中用JdbcTemplate的batchUpdate插入一组数据
//jt.batchUpdate(sql, ipstc);
//System.out.println("插入成功!...");
//3:Spring中用RowMapper查询数据
final String sql2 = "select * from Student";
RowMapper rowMapper = new RowMapper() {
public Object mapRow(ResultSet rs, int index) throws SQLException {
Student student = new Student();
student.setId(rs.getInt(1));
student.setName(rs.getString(2));
student.setSex(rs.getString(3));
student.setSalary(rs.getInt(4));
return student;
}
};
PreparedStatementCreator psc = new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection arg0)
throws SQLException {
return arg0.prepareStatement(sql2);
}
};
List list = jt.query(psc, rowMapper);
for (Object obj : list) {
Student stu = (Student) obj;
System.out.println(stu.getId() + "/t" + stu.getName() + "/t"
+ stu.getSex() + "/t" + stu.getSalary());
}
}
}
4:最后写个配置文件就可以了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql:///itcast</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value></value>
</property>
</bean>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="ds" />
</property>
</bean>
<bean id="config" class="cn.itcast.InsertPersonStatementCreator">
<property name="list">
<list>
<ref bean="stu1" />
<ref bean="stu2" />
<ref bean="stu3" />
</list>
</property>
</bean>
<bean id="stu1" class="cn.itcast.Student">
<property name="name"><value>ccc</value></property>
<property name="sex"><value>m</value></property>
<property name="salary"><value>3000</value></property>
</bean>
<bean id="stu2" class="cn.itcast.Student">
<property name="name"><value>ddd</value></property>
<property name="sex"><value>f</value></property>
<property name="salary"><value>5000</value></property>
</bean>
<bean id="stu3" class="cn.itcast.Student">
<property name="name"><value>eee</value></property>
<property name="sex"><value>m</value></property>
<property name="salary"><value>1000</value></property>
</bean>
</beans>
以上程序我都实验过,可以顺利跑通,如果读者实验时,遇到什么问题,可以给我留言.
本文地址:http://www.45fan.com/a/question/68330.html