自己封装的BaseDao--更加灵活方便--hashmap


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.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

public class BaseControl {
 static Logger log = Logger.getLogger(BaseControl.class);
 
 
 /**
  * 查询
  * @param sql 查询Sql语句
  * @param params 参数集合
  * @return
  */
 public static List<HashMap<String, Object>> executeQuery(String sql,Object...params){
  List<HashMap<String, Object>> lists = new ArrayList<HashMap<String,Object>>();
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  try {
   conn = DBUtil.getConn();
   ps = conn.prepareStatement(sql);
   //给SQL参数进行赋值 
   int temp = 1;
   for (int i = 0; i < params.length; i++) {
    ps.setObject(temp++, params[i]);
   }
   rs = ps.executeQuery();
   ResultSetMetaData rss = rs.getMetaData();
   int columnCount = rss.getColumnCount();
   String[] columnName = new String[columnCount];//获取字段名称集合
   while(rs.next()){
    HashMap<String, Object> map = new HashMap<String, Object>();
    for (int i = 0; i < columnCount; i++) {
      columnName[i] = rss.getColumnName(i+1);
    }
    for (int j = 1; j <= columnCount; j++) {
     Object columnValue = rs.getObject(j);
     map.put(columnName[j-1], columnValue);
    }
    lists.add(map);
   }
   return lists;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   log.error("查询SQL异常", e);
  }finally{
   DBUtil.closeConn(conn, ps, rs);
  }
  return null;
  }
 /**
  * 以数据库字段名为键,该字段值为值返回一个map
  * @param sql查询语句
  * @param param单条件查询
  * @return
  */
 public static HashMap<String, Object>executeQueryByColumn(String sql,Object...params){
  HashMap<String, Object> map = new HashMap<String, Object>();
  Connection conn = null;
  PreparedStatement ps = null;
  ResultSet rs = null;
  conn = DBUtil.getConn();
  try {
   ps = conn.prepareStatement(sql);
   int temp = 1;
   for (int i = 0; i < params.length; i++) {
    ps.setObject(temp++, params[i]);
   }
   rs = ps.executeQuery();
   ResultSetMetaData rss = rs.getMetaData();
   int columnCount = rss.getColumnCount();
   String[] columnName = new String[columnCount];
   while(rs.next()){
    for (int i = 0; i <columnName.length; i++) {
     columnName[i] = rss.getColumnName(i+1);
    }
    for (int i = 1; i <=columnCount; i++) {
     Object value = rs.getObject(i);
     String Name = columnName[i-1];
     map.put(Name, value);
    }
   }
   return map;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
  
 };
 /**
  * 增加,修改,或删除
  * @param sql
  * @param params
  * @return
  */
 public static int executeSaveOrUpdate(String sql,List<?> params){
  Connection conn = null;
  PreparedStatement ps = null;
  try {
   conn = DBUtil.getConn();
      ps = conn.prepareStatement(sql);
   int temp = 1;
   for (Iterator<?> p = params.iterator(); p.hasNext();) {
    ps.setObject(temp++, p.next());
   }
   ps.addBatch();
   
   return ps.executeBatch().length;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   log.error("插入SQL异常", e);
  }finally{
   DBUtil.closeConn(conn, ps, null);
  }
  return 0;
  }
 /**
  * 少参数插入
  * @param sql
  * @param params
  * @return
  */
 public static int executeSaveOrUpdate(String sql,Object... params){
  Connection conn = null;
  PreparedStatement ps = null;
  try {
   conn = DBUtil.getConn();
      ps = conn.prepareStatement(sql);
   int temp = 1;
    for (int i = 0; i < params.length; i++) {     
     ps.setObject(temp++, params[i]);
    }
   
   ps.addBatch();
   
   return ps.executeBatch().length;
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   log.error("插入SQL异常", e);
  }finally{
   DBUtil.closeConn(conn, ps, null);
  }
  return 0;
  }
 DBUtil类

public class DBUtil {  
   
   
 static Logger logger=Logger.getLogger(DBUtil.class);  
   
 static Properties properties = null;  
   
   
 /**  
  * 返回数库链接对象  
  * @return  
  */  
 public static Properties getProperInfo() {  
  InputStream is = Thread.currentThread().getContextClassLoader()  
    .getResourceAsStream("oracle.properties");  
  properties = new Properties();  
  try {  
   properties.load(is);  
  } catch (IOException e) {  
   e.printStackTrace();  
   logger.error("数据库配置文件异常");  
  }  
  return properties;  
 }  
   
   
 /**  
  * 定义链接数据库方法  
  * @return  
  * @throws ClassNotFoundException  
  * @throws SQLException  
  */  
 public static Connection getConn() {  
   if (properties == null) {  
    properties = getProperInfo();  
   }  
   String url = properties.getProperty("url");  
   String user = properties.getProperty("user");  
   String password = properties.getProperty("password");  
     
   //加载数据库驱动  
   try {  
    logger.debug("加载数据库驱动");  
    Class.forName(properties.getProperty("driver"));  
   } catch (ClassNotFoundException e) {  
    e.printStackTrace();  
    logger.error("数据驱动加载异常", e);  
   }  
   //建立数据连接  
   try {  
    logger.debug("建立数据库连接");  
    return DriverManager.getConnection(url, user, password);  
   } catch (SQLException e) {  
    // TODO Auto-generated catch block  
    e.printStackTrace();  
    logger.error("建立数据库连接异常", e);  
   }  
   logger.debug("数据连接成功");  
   return null;  
 }  
   
   
 /**  
  * 关闭链接  
  * @param conn  
  * @param ps  
  * @param rs  
  */  
 public static void closeConn(Connection conn, PreparedStatement ps,ResultSet rs) {  
  try {  
   if (rs != null) {  
    rs.close();  
   }  
  } catch (SQLException e) {  
   e.printStackTrace();  
   logger.error("关闭ResultSet异常", e);  
  }  
  try {  
   if (ps != null) {  
    ps.close();  
   }  
  } catch (SQLException e) {  
   e.printStackTrace();  
   logger.debug("关闭PreparedStatement异常",e);  
  }  
  try {  
   if (conn != null) {  
    conn.close();  
   }  
  } catch (SQLException e) {  
   e.printStackTrace();  
   logger.error("关闭Connection异常", e);  
  }  
 }

测试: 
  String sql = "select * from s_user ";

List<HashMap<String, Object>> lists = BaseControl.executeQuery(sql, null);

HashMap<String, Object> map = new HashMap<String, Object>();

for (int i = 0; i < lists.size(); i++) {

map = lists.get(i);

System.out.println(map.get("你所要查询的字段名(大写)"));

}
总结:与basedao一 相比 这种方式摆脱了 Javabean的限制,不必因为数据库增加或删除字段而对代码进行改动,数据库可以随时增删字段。代码也量减少了! 如果有需要改进的地方请大家留下宝贵意见,谢谢!

你可能感兴趣的:(java,baseDao)