package Dao;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BaseDao {
public final static String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; // 数据库驱动
public final static String URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=contactbook"; // url
public final static String DBNAME = "sa"; // 数据库用户名
public final static String DBPASS = "123456"; // 数据库密码
/**
* 得到数据库连接
* @throws ClassNotFoundException
* @throws SQLException
* @return 数据库连接
*/
public Connection getConn() throws ClassNotFoundException, SQLException{
Class.forName(DRIVER); //注册驱动
Connection conn = DriverManager.getConnection(URL,DBNAME,DBPASS); //获得数据库连接
return conn ; //返回连接
}
/**
* 释放资源
* @param conn 数据库连接
* @param pstmt PreparedStatement对象
* @param rs 结果集
*/
public void closeAll( Connection conn, PreparedStatement pstmt, ResultSet rs ) {
/* 如果rs不空,关闭rs */
if(rs != null){
try { rs.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果pstmt不空,关闭pstmt */
if(pstmt != null){
try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
}
/* 如果conn不空,关闭conn */
if(conn != null){
try { conn.close();} catch (SQLException e) {e.printStackTrace();}
}
} /**
* 关闭查询连接
* @param rs
* @param st
* @param c
*/
protected void closeResource(ResultSet rs) {
try {
Statement st = rs.getStatement();
Connection c = st.getConnection();
rs.close();
st.close();
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 执行SQL语句,可以进行增、删、改的操作,不能执行查询
* @param sql 预编译的 SQL 语句
* @param param 预编译的 SQL 语句中的‘?’参数的字符串数组
* @return 影响的条数
*/
public int executeSQL(String preparedSql,Object...param) {
Connection conn = null;
PreparedStatement pstmt = null;
int num = 0;
/* 处理SQL,执行SQL */
try {
conn = getConn(); // 得到数据库连接
pstmt = conn.prepareStatement(preparedSql); // 得到PreparedStatement对象
if( param != null ) {
for( int i = 0; i < param.length; i++ ) {
pstmt.setObject(i+1, param[i]); // 为预编译sql设置参数
}
}
num = pstmt.executeUpdate(); // 执行SQL语句
} catch (ClassNotFoundException e) {
e.printStackTrace(); // 处理ClassNotFoundException异常
} catch (SQLException e) {
e.printStackTrace(); // 处理SQLException异常
} finally {
closeAll(conn,pstmt,null); // 释放资源
}
return num;
}/**
* 执行查询
* @param sql
* @param params
* @return
*/
public ResultSet executeQuery(String sql,Object...params){
Connection c = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
c = getConn();
pst = c.prepareStatement(sql);
if (params!=null && params.length>0){
for (int i = 0; i < params.length; i++) {
pst.setObject(i+1, params[i]);
}
}
rs = pst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* @param entityClass实体类 (类.class)
* @param sqlSQL语句 (String)
* @param params替换参数
* @return
*/
public List<Object> executeQuery(Class entityClass,String sql,Object...params){
ResultSet rs = null;
List<Object> list = null;
try {
rs = executeQuery(sql,params);
//通过反射封装对象
if (rs!=null){
list = new ArrayList<Object>();
while(rs.next()){
Object entity = entityClass.newInstance();//执行该方法默认会调用类的无参构造方法
/*Field应该是字段*/
for(Field f: entityClass.getDeclaredFields()){
f.setAccessible(true);/*所有字段包括私有的*/
for(int i=1; i<=rs.getMetaData().getColumnCount();i++){/*循环查询得到的数据,得到列名*/
/*System.out.println("/r/t------------------"+rs.getMetaData().getColumnName(i).toLowerCase()+"/r/t------------------");
*/if (f.getName().toLowerCase().equals(rs.getMetaData().getColumnName(i).toLowerCase())){
/*列名如果与实体的属性相同,就赋值*/
f.set(entity, rs.getObject(i));
/*System.out.print(rs.getObject(i));*/
}
}
}
list.add(entity); /*添加实体到集合*/
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeResource(rs);
}
return list;
}
}