利用java反射技术进行数据增删改查操作

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。


import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.sql.DataSource;

import mons.beanutils.BeanUtils;
import mons.beanutils.PropertyUtils;
import mons.dbcp.BasicDataSource;
import mons.dbcp.BasicDataSourceFactory;

/**
* 数据库操作封装类
* @author SayKiss
*
*/
public class BaseDao2 {
public static DataSource ds = null;
public static Properties properties = null;

static{
InputStream is = BaseDao2.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties = new Properties();
try {
properties.load(is);
} catch (IOException e) {
throw new RuntimeException("加载XML文件时出错了",e);
}
}

/**
* 得到数据源
*
*/
public void getDateSource(){
if (ds == null) {
synchronized (BasicDataSource.class) {
if (ds == null) {
try {
ds= BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException("创建数据源时出错了",e);
}
}
}
}
}

/**
* 返回数据库连接
* @return connection对象
*/
public Connection getConnection(){
Connection connection = null;
try {
getDateSource();
connection = ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException("得到链接时出错了",e);
}
return connection;
}

/**
* 执行数据库的非查询操作
* @param sql 要执行的Sql语句
* @param param 参数列表
* @return 受影响的行数
*/
public int executeUpdate(String sql,Object[]param) {
int num = 0;
Connection connection = getConnection();
PreparedStatement pStatement = null;
try {
pStatement = connection.prepareStatement(sql);
addParamters(param, pStatement);
num = pStatement.executeUpdate();
} catch (SQLException e) {
throw new DaoException("执行非查询操作时出错了!",e);
}
return num;
}

/**
* 为Sql语句的占位符赋值
* @param param 参数列表
* @param pStatement pStatement对象
* @throws SQLException
*/
protected void addParamters(Object[] param, PreparedStatement pStatement)

throws SQLException {
if (param != null) {
for (int i = 0; i < param.length; i++) {
pStatement.setObject(i + 1, param[i]);
}
}
}

/**
* 执行查询工作
* @param sql 执行的Sql语句
* @param param 参数列表
* @param rp ResultParse接口对象
* @return 对象的List集合
*/
@SuppressWarnings("unchecked")
public List executdQuery(String sql,Object[]param,Class clazz){
List ls = null;
Connection connection = getConnection();
PreparedStatement pStatement = null;
ResultSet resultSet = null;
try {
pStatement = connection.prepareStatement(sql);
addParamters(param, pStatement);
resultSet = pStatement.executeQuery();
//获取结果集中的原始数据源
ResultSetMetaData rsd = resultSet.getMetaData();
int columnCount = rsd.getColumnCount();
String[] columnName =new String [columnCount];
for (int i = 0; i < columnName.length; i++) {
columnName[i]=rsd.getColumnName(i+1).toString();
System.out.println(columnName[i]);
}
ls = new ArrayList();
//使用 beanUtils组件
PropertyDescriptor[] pd = PropertyUtils.getPropertyDescriptors(clazz);
while (resultSet.next()) {
T obj = clazz.newInstance();
for (int i = 0; i < columnName.length; i++) {
for (int j = 0; j < pd.length; j++) {
if (columnName[i].equalsIgnoreCase(pd[j].getName())) {
Object value = resultSet.getObject(columnName[i]);
if (value != null) {
BeanUtils.setProperty(obj, columnName[i], value);
}
break;
}
}
}
ls.add(obj);
}
return ls;
} catch (Exception e) {
throw new DaoException("执行查询操作反射复制Bean时出错了!",e);
}finally{
closeAll(connection, pStatement, resultSet);
}
}



/**
* 关闭数据库所有链接
* @param connection Connection链接对象
* @param pStatement pStatement链接对象
* @param resultSet resultSet对象
*/
protected void closeAll(Connection connection,PreparedStatement pStatement,ResultSet resultSet){
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
throw new DaoException("关闭resultSet时出错了",e);
}finally{
try {
if (pStatement != null) {
pStatement

.close();
}
} catch (SQLException e) {
throw new DaoException("关闭pStatement时出错了!",e);
}finally{
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new DaoException("关闭connection链接时出错了!",e);
}
}
}
}
}
commons-beanutils-1.8.2.jar------这个jar包

相关文档
最新文档