mysql数据源_BaseDao

来源:互联网 发布:纽约跑跑美国代购 知乎 编辑:程序博客网 时间:2024/06/05 00:38
package msgboard.zt.dao;


import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.sql.Connection;
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 javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;






/**
 * 数据库操作类
 * @author daokoushuai
 *
 */
public class BaseDao {


public BaseDao(Connection conn) {
super();
this.conn = conn;
}




/**
* 数据库连接,通过构造方法传递,子类调用
*/
protected Connection  conn= null;

/**
* 集合查询方法
* @param cls class对象
* @param sql sql语句
* @param obj 动态参数
* @return 泛型集合对象
*/
protected <T>List<T> executeQuery(Class<T> cls,String sql,Object...obj) {
if (cls==null||sql==null||this.conn==null) {
return null;
}
List<T> list = new ArrayList<>();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
loadParam(ps, obj);
rs = ps.executeQuery();
ResultSetMetaData data = rs.getMetaData();
int count = data.getColumnCount();
while (rs.next()) {
T t = cls.newInstance();
for (int i = 0; i < count; i++) {
String name = data.getColumnLabel(i+1);
Object value = rs.getObject(name);
Field filed = cls.getDeclaredField(name);
if (!Modifier.isPublic(filed.getModifiers())) {
filed.setAccessible(true);
}
filed.set(t, value);
}
list.add(t);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDaoUtil.CloseAll(rs,ps);
}
return list;
}
/**
* 执行增删改
* @param sql 语句
* @param obj 动态参数
* @return 受影响的行数
*/
protected int executeUpdate(String sql,Object...obj) {
int result = -1;
if (sql==null||this.conn==null) {
return result;
}
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
loadParam(ps,obj);
result = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
BaseDaoUtil.CloseAll(ps);
}
return result;
}

/**
* 设置参数
* @param ps 预编译对象
* @param obj 需设置的动态参数
*/
private void loadParam(PreparedStatement ps, Object...obj) {
if (ps==null||obj==null) {
return;
}
for (int i = 0; i < obj.length; i++) {
try {
ps.setObject(i+1, obj[i]);
} catch (SQLException e) {
e.printStackTrace();
}
}
}






/**
* 数据源静态工具类,获取连接对象,关闭连接
* @author daokoushuai
*
*/
static class BaseDaoUtil{
/**
* 数据源对象
*/
private static DataSource dataSources = null;
/**
* 初始化
*/
static{
init();
}

/**
* 初始化获取DataSource对象
*/
private static void init() {
try {
Context context = new InitialContext();
Object dataSource = context.lookup("java:comp/env/java/msgboard");
if (dataSource instanceof DataSource) {
dataSources = (DataSource)dataSource;
}
} catch (NamingException e) {
e.printStackTrace();
}
}
/**
* 获取连接
* @return 数据库连接对象
*/
public static Connection getConnection() {
Connection conn = null;
try {
if(dataSources!=null) conn = dataSources.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}

/**
* 关闭连接
* @param obj 关闭连接方法
*/
public static void CloseAll(Object...obj) {
if (obj==null||obj.length==0) {
return;
}
for (int i = 0; i < obj.length; i++) {
if (obj[i]!=null) {
if (obj[i] instanceof ResultSet) {
try {
((ResultSet)obj[i]).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (obj[i] instanceof PreparedStatement) {
try {
((PreparedStatement)obj[i]).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (obj[i] instanceof Connection) {
try {
((Connection)obj[i]).close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}