一个简单的数据库工具类

接上面一片博文,自己弄了一个简单的数据库操作工具类:

/*
 * 创建日期 2014-6-5
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package com.excellence.explatform.proxy.util;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/**
 * @author pengsy
 * 
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public final class DBUtil {

	/**
	 * 获取数据库连接
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() throws Exception {
		Context context = null;
		DataSource dataSource = null;
		try {
			context = new InitialContext();
			try{
				//TOMCAT JDK1.6
				dataSource = (DataSource) context.lookup("java:comp/env/jdbc/exoa");
			}catch(Exception E){
				E.printStackTrace();
			}
			if(dataSource == null){
				//WAS JDK1.4
				dataSource = (DataSource) context.lookup("jdbc/exoa");
			}
			
			if (dataSource == null) {
				System.err.println("打开数据库连接出错!!!");
				return null;
			}
			return dataSource.getConnection();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 执行sql语句
	 * @param sql
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static boolean executeSql(String sql, ArrayList params)
			throws Exception {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		if(conn == null){
			return false;
		}
		try{
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < params.size(); i++) {
				Object param = (Object) params.get(i);
				if(param instanceof String){
					ps.setString((i+1),(String) param);
				}else if(param instanceof Date){
					ps.setDate((i+1),(Date) param);
				}else{
					ps.setString((i+1),param == null ? "" : param.toString());
				}
			}
			return ps.execute();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			if(ps != null ){
				ps.close();
			}
			conn.close();
		}
	}
	
	public static Map queryRecordInfo(String sql, ArrayList params)throws Exception {
		Connection conn = getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		Map resultMap = null;
		if(conn == null){
			return null;
		}
		try{
			ps = conn.prepareStatement(sql);
			for (int i = 0; i < params.size(); i++) {
				Object param = (Object) params.get(i);
				if(param instanceof String){
					ps.setString((i+1),(String) param);
				}else if(param instanceof Date){
					ps.setDate((i+1),(Date) param);
				}else{
					ps.setString((i+1),param == null ? null : param.toString());
				}
			}
			rs = ps.executeQuery();
			ResultSetMetaData metaData = ps.getMetaData();
			
			if(rs != null){
				resultMap = new HashMap();
				while(rs.next()){
					for(int i = 0 ; i < metaData.getColumnCount();i++){
						resultMap.put(metaData.getColumnName(i+1),rs.getObject(i+1));
					}
					break;
				}
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(rs != null ){
				rs.close();
			}
			
			if(ps != null ){
				ps.close();
			}
			conn.close();
		}
		return resultMap;
	}
}

 

你可能感兴趣的:(一个简单的数据库工具类)