本身封裝的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;
java

import org.apache.log4j.Logger;sql

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);  
  }  
 }
apache

測試: 
  String sql = "select * from s_user ";
oracle

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

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

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

map = lists.get(i);debug

System.out.println(map.get("你所要查詢的字段名(大寫)"));對象

} 總結:與basedao一 相比 這種方式擺脫了 Javabean的限制,沒必要由於數據庫增長或刪除字段而對代碼進行改動,數據庫能夠隨時增刪字段。代碼也量減小了! 若是有須要改進的地方請你們留下寶貴意見,謝謝!

相關文章
相關標籤/搜索