123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package com.sinosoft.em.evaluate;
- 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.List;
- import java.util.Map;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import com.persistence.DbConnection;
- /**
- * 用于数据库操作
- * @author len
- *
- */
- public class DbHander {
-
- private static Log log = LogFactory.getLog(DbHander.class);
- private DbConnection dbConn = new DbConnection();
- private Connection conn = null;
- private PreparedStatement prst = null;
-
- public DbHander(){
- //createDbConn();
-
- }
- public void createDbConn(){
- try {
- if(conn==null || conn.isClosed())
- conn = dbConn.getConnection();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- log.error("DbHander 连接数据失败", e);
- }
- }
-
- public void destroyDbConn() {
- try {
- if(prst!=null)
- prst.close();
- if(conn!=null && !conn.isClosed())
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- log.error("DbHander 连接关闭失败", e);
- }
- }
- public List<Map<String,String>> queryForList(String sql,Object [] args0){
- createDbConn();
- List<Map<String,String>> result = new ArrayList<Map<String,String>>();
- try {
- prst = conn.prepareStatement(sql);
- for(int i=0;i<args0.length;i++){
- prst.setString(i+1,String.valueOf(args0[i]));
- }
- ResultSet rs = prst.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int count = rsmd.getColumnCount();
- //String field = rsmd.getColumnName(1);
- while(rs.next()){
- Map<String,String> map = new HashMap<String, String>();
- for(int i=0;i<count;i++){
- map.put(rsmd.getColumnName(i+1), rs.getString(rsmd.getColumnName(i+1)));
- }
- result.add(map);
- }
- rs.close();
- destroyDbConn();
- } catch (SQLException e) {
- log.error("数据库查询失败!");
- e.printStackTrace();
- }
- return result;
- }
-
- public List<Map<String,String>> queryForList(String sql){
- createDbConn();
- List<Map<String,String>> result = new ArrayList<Map<String,String>>();
- try {
- prst = conn.prepareStatement(sql);
- ResultSet rs = prst.executeQuery();
- ResultSetMetaData rsmd = rs.getMetaData();
- int count = rsmd.getColumnCount();
- //String field = rsmd.getColumnName(1);
- while(rs.next()){
- Map<String,String> map = new HashMap<String, String>();
- for(int i=0;i<count;i++){
- map.put(rsmd.getColumnName(i+1), rs.getString(rsmd.getColumnName(i+1)));
- }
- result.add(map);
- }
- rs.close();
- destroyDbConn();
- } catch (SQLException e) {
- log.error("数据库查询失败!");
- e.printStackTrace();
- }
- return result;
- }
-
- public ResultSet query(String sql,Object [] args){
- createDbConn();
- ResultSet rs = null;
- try {
- prst = conn.prepareStatement(sql);
- for(int i=0;i<args.length;i++){
- prst.setString(i+1,String.valueOf(args[i]));
- }
- rs = prst.executeQuery();
- } catch (SQLException e) {
- log.error("数据库查询失败!");
- e.printStackTrace();
- }
- return rs;
- }
- public ResultSet query(String sql){
- createDbConn();
- ResultSet rs = null;
- try {
- prst = conn.prepareStatement(sql);
- rs = prst.executeQuery();
- } catch (SQLException e) {
- log.error("数据库查询失败!");
- e.printStackTrace();
- }
- return rs;
- }
-
- public int update(String sql,Object [] args){
- createDbConn();
- int result = 0;
- try {
- prst = conn.prepareStatement(sql);
- for(int i=0;i<args.length;i++){
- prst.setString(i+1,String.valueOf(args[i]));
- }
- result = prst.executeUpdate();
- } catch (SQLException e) {
- log.error("数据库更新失败!");
- e.printStackTrace();
- }finally{
- destroyDbConn();
- }
- return result;
- }
- public int update(String sql){
- createDbConn();
- int result = 0;
- try {
- prst = conn.prepareStatement(sql);
- result = prst.executeUpdate();
- } catch (SQLException e) {
- log.error("数据库更新失败!");
- e.printStackTrace();
- }finally{
- destroyDbConn();
- }
- return result;
- }
-
- public int insert(String sql,Object [] args){
- createDbConn();
- int result = 0;
- try {
- prst = conn.prepareStatement(sql);
- for(int i=0;i<args.length;i++){
- prst.setString(i+1,String.valueOf(args[i]));
- }
- result = prst.executeUpdate();
- } catch (SQLException e) {
- log.error("数据库更新失败!");
- e.printStackTrace();
- }finally{
- destroyDbConn();
- }
- return result;
- }
- public int insert(String sql){
- createDbConn();
- int result = 0;
- try {
- prst = conn.prepareStatement(sql);
- result = prst.executeUpdate();
- } catch (SQLException e) {
- log.error("数据库更新失败!");
- e.printStackTrace();
- }finally{
- destroyDbConn();
- }
- return result;
- }
-
- }
|