学习 spring jdbc

本文介绍了Spring JDBC框架的使用。首先说明了下载Spring开发包并放置到指定目录的方法,接着阐述了Spring JDBC框架由四个包组成,还给出了JdbcTemplate的操作示例,包括创建表、更新数据等。此外,还介绍了查询、修改、简单操作的相关类使用,最后提及了Spring的整合技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先去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

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值