首先去http://sourceforge.net/project/showfiles.php?group_id=73357下载spring 开发包。把
spring.jar,log4j.jar等放到web-inf/lib/下,就ok了。
spring jdbc framework由四个包组成:org.springframework.jdbc.core;org.springframework.jdbc.datasource;org.springframework.jdbc.object;org.springframework.jdbc.support
spring jdbc的exception在org.springframework.dao中实现
1。org.springframework.jdbc.core.JdbcTemplate是spring 核心类,它从datasource取得connection,并关闭连接。它完成sql的insert/update/delete/query/store proc
下面是它 的一些例子:
import org.springframework.jdbc.core.JdbcTemplate;
public void do(Datasource ds) {
JdbcTemplate jt = new JdbcTemplate(ds);
jt.execute("create table mytable (id integer, name varchar(100))");
jt.update("update table set name= ? where id= ? ", new Object[]{name, new Integer(id)});
int count =jt.queryForInt("select count(*) from table");
String str = (String)jt.queryForObject("select name from table where id=1", String.class);
List list = (List)jt.queryForList("select * from table");
}
2。查询
org.springframework.jdbc.object.MappingSqlQuery
import org.springframework.jdbc.object.MappingSqlQuery;
public class mappingSqlQuery {
public Product getProduct() {
CustomerMappingSqlQuery cm = new CustomerMappingSqlQuery(ds);
Object[] parms = new Object[1];
parms[0] = new Integer(1);
List list = cm.execute(parms);
}
private class CustomerMappingSqlQuery extends MappingSqlQuery {
public CustomerMappingSqlQuery(DataSource ds) {
super(ds, "select id, name from table where id= ?");
super.declareParameter(new SqlParameter("id", Types.INTEGER));
compile();
}
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException
{
Product product = new Product();
product.setId(rs.getInt("id"));
product.setDescription(rs.getString("name"));
return product;
}
}
}
3。修改
org.springframework.jdbc.object.SqlUpdate
public int doUpdate(DataSource ds) {
SqlUpdate su = new SqlUpdate();
su.setDataSource(ds);
su.setSql("update table set name = ? where id = ?");
su.declareParameter(new SqlParameter("name", Types.VARCHAR));
su.compile();
Object[] params = new Object[2];
params[0] = "name";
params[1] = new Integer(1);
return su.update(params);
}
4。简单操作
org.springframework.jdbc.object.SqlFunction
public int countRows() {
SqlFunction sf = new SqlFunction(ds, "select count(*) from mytable");
sf.compile();
return sf.run();
}
下面是spring 整合的技术
spring mvc /spring aop/ spring transaction/ spring context
integrating spring view/ integrating spring o/r mapping
spring ejb spring web service/spring remoting