Commons -dbUtils的开发应用如下:
如果你自己观察他代码你会被它的简明。易用征服的
(*^◎^*) (*^◎^*)
好了开始工作了
z
package cn.com.huawei.opensource.common.dbutils;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.RowProcessor;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import cn.com.huawei.opensource.common.pool.dbcppool.DataBaseSource;
/**
*
* @author bailonggang
* 2009-2-26
* 下午11:55:40
*/
public class DBUtils {
private String driverClass;
private String url;
private String username;
private String password;
public DataSource getDataSource() {
return DataBaseSource.getDbcpDataSource(driverClass, url, username,
password);
}
/**
* 添加删除修改的方法的应用
*
* @param sql
* @param params
* @throws SQLException
*/
public void executeUpdate(String sql, Object[] params) throws SQLException {
QueryRunner run = new QueryRunner(getDataSource());
run.update(sql, params);
}
/**
* 修改添加修改删除使用事务
* @param con
* @param sql
* @param params
*/
public void executeUpdateWithTrans(Connection con,String sql, Object[] params) {
QueryRunner run = new QueryRunner();
try {
if(!con.isClosed())
{
con.setAutoCommit(false);
run.update(sql, params);
con.commit();
DbUtils.commitAndClose(con);
}
} catch (SQLException e) {
DbUtils.rollbackAndCloseQuietly(con);
}
}
/**
* 查询结果以Collection的形式出现(List中的每一个对象是以一种Bean形式)
*
* @param sql
* @param cls
* @param params
* @return
* @throws SQLException
*/
public Collection findAll(String sql, Class cls, Object[] params)
throws SQLException {
/**
*
*/
ResultSetHandler rowhandler = new BeanListHandler(cls);
QueryRunner run = new QueryRunner(getDataSource());
return (Collection) run.query(sql, params, rowhandler);
}
/**
* 查询结果以Map形式出现的方法
*
* @param query
* @param cls
* @param params
* @return
* @throws SQLException
*/
public Map executeForMap(String query, Class cls, Object[] params)
throws SQLException {
RowProcessor rowprocessor = new BasicRowProcessor();
QueryRunner run = new QueryRunner(getDataSource());
//MapHandler将一条记录转换为Map对象
ResultSetHandler rshandler = new MapListHandler(rowprocessor);
return (Map) run.query(query, params, rshandler);
}
/**
* 查询单个对象转换为Map
*
* @param sql
* @param clsName
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Map findByKey(String sql, String clsName,Object[] params) throws SQLException,
ClassNotFoundException {
RowProcessor rowprocessor = new BasicRowProcessor();
//MapHandler将一条记录转换为Map对象
ResultSetHandler rshandler = new MapHandler(rowprocessor);
QueryRunner run = new QueryRunner(getDataSource());
return (Map) run.query(sql, params,rshandler);
}
/**
* 查询通过主键查询单个对象
* @param keyField
* @param sql
* @return 返回一个Map其中列名称为键值
* @throws SQLException
*/
public Map findByKey(String keyField,String sql) throws SQLException
{
ResultSetHandler rsh= new KeyedHandler(keyField);
QueryRunner queryRunner=new QueryRunner();
return (Map) queryRunner.query(sql, rsh);
}
/**
* 查询单个对象转换为Object
*
* @param sql
* @param clsName
* @param params
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
public Object findByKey(String query, Class cls, Object[] params)
throws SQLException {
QueryRunner run = new QueryRunner(getDataSource());
RowProcessor rowprocessor = new BasicRowProcessor();
ResultSetHandler rshandler = new BeanHandler(cls, rowprocessor);
return run.query(query, params, rshandler);
}
/**
* 批处理操作的应用
*
* @param sql
* @param params
* @param cls
* @return
* @throws SQLException
*/
public int[] executeUpdateBatch(String sql, Object[][] params, Class cls)
throws SQLException {
QueryRunner run = new QueryRunner(getDataSource());
return run.batch(sql, params);
}
public String getDriverClass() {
return driverClass;
}
public void setDriverClass(String driverClass) {
this.driverClass = driverClass;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}