利用回调简化JDBC编程

简单看了一下spring 的jdbc支持,现在又要直接用到jdbc,想想就是痛苦。于是参考了spring,自己写了一些简单的java封装类来简化编程。


废话少说,我这里就用代码来代言吧,看看怎样简化我们的JDBC编程,可以和以前进行对比。


(1) JdbcTemplate。

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import javax.sql.DataSource;


import org.winter.util.DBUtil;


/**
 * a simple JDBC template 模仿spring的JdbcTemplate
 *
 * @author bluestone
 * @version 1.0 2006-8-8
 *
 */
public class JdbcTemplate {


 private DataSource dataSource = null;


 public JdbcTemplate(DataSource ds) {
  this.dataSource = ds;
 }


/**
  * 执行更新操作
  *
  * @param sql
  * @param setter
  * @return
  * @throws SQLException
  */
 public int update(String sql, PreparedStatementSetter setter)
   throws SQLException {
  Connection conn = null;
  reparedStatement ps = null;
  try {
   conn = dataSource.getConnection();
   ps = conn.prepareStatement(sql);
   setter.setValues(ps);
   return ps.executeUpdate();
  } finally {
   DBUtil.colseConnection(conn, ps, null);
  }
 }


 /**
  *
  * @param sql
  * @return
  * @throws SQLException
  */
 public boolean execute(String sql) throws SQLException {
  Connection conn = null;
  Statement stmt = null;
  try {
   conn = dataSource.getConnection();
   stmt = conn.createStatement();
   return stmt.execute(sql);
  } finally {
   DBUtil.colseConnection(conn, stmt, null);
  }
 }


 /**
  *
  * @param sql
  * @param setter
  * @param extractor
  * @return
  * @throws SQLException
  */
 public Object query(String sql, PreparedStatementSetter setter,
   ResultSetExtractor extractor) throws SQLException {
  Connection conn = null;
  reparedStatement ps = null;
  ResultSet rs = null;
  try {
   conn = dataSource.getConnection();
   ps = conn.prepareStatement(sql);
   setter.setValues(ps);
   rs = ps.executeQuery();
   return extractor.extractData(rs);
  } finally {
   DBUtil.colseConnection(conn, ps, rs);
  }
 }


 // .........................
}


(2)  PreparedStatementSetter


public interface PreparedStatementSetter {
 void setValues(PreparedStatement ps) throws SQLException;
}


(3)  ResultSetExtractor


public interface ResultSetExtractor {
 Object extractData(ResultSet rs) throws SQLException;
}



(4) 可以参考spring自己定义其他接口。。。


用了这些辅助类,我们就可以像用spring那样编程了(当然这只能用在对事务要求不高的应用环境中)。看看怎么使用:



 private JdbcTemplate template;


 public JobManageDao() throws BusinessException {
  try {
   template = new JdbcTemplate(DBHelper.getDataSource());
  } catch (NamingException e) {
   throw new BusinessException(e);
  }
 }


public long saveJobInfo(final JobInfo info) throws BusinessException {
  final long id = IdGenerator.getIdLong();
  try {
   int j = template.update(INSERT_JOB_SQL, new PreparedStatementSetter() {


  public void setValues(PreparedStatement ps) throws SQLException {
     int i = 1;
     ps.setLong(i++, id);


   //......


   }
   });
      return j > 0 ? id : 0L;
  } catch (SQLException e) {
      throw new BusinessException(e);
  }
 }

posted on 2006-08-24 12:38 bluestone 阅读(328) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: