简单的jdbc封装

工作中经常用到jdbc,嫌每次创建连接麻烦。自己简单的封装了下,也没有测试,不知道能不能用。


package com.geap.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;

import com.sun.rowset.CachedRowSetImpl; //com.sun.rowset.jar包


/**
 * jdbc工具类,提供了三个方法
 * <ul>
 * <li>execute(sql)</li>
 * <li>executeQuery(sql)</li>
 * <li>executeUpdate(sql)</li>
 * </ul>
 * @author QiuLu
 */
public class JdbcUtil {
	
	/**
	 * 查询
	 * @param sql sql语句
	 * @return RowSet 离线结果集(CachedRowSet接口,CachedRowSetImpl实现)
	 * @throws Exception
	 */
	public static RowSet executeQuery(String sql) throws Exception{

		Context ctx = null;
		DataSource ds = null;
		Connection cn = null;
		Statement st = null;
		ResultSet rs = null;
		CachedRowSet crs = null;
		
		try {
			ctx = new InitialContext();
			crs = new CachedRowSetImpl();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
			cn = ds.getConnection();
			st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			rs = st.executeQuery(sql);
			crs.populate(rs);
			return crs;
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if(rs != null)
				try {
					rs.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(st != null)
				try {
					st.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(cn != null)
				try {
					cn.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(ctx != null)
				try {
					ctx.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 执行sql
	 * @param sql sql语句
	 * @return 如果第一个结果为 ResultSet 对象,则返回 true;如果其为更新计数或者不存在任何结果,则返回 false 
	 * @throws SQLException
	 */
	public static boolean execute(String sql) throws SQLException{
		Context ctx = null;
		DataSource ds = null;
		Connection cn = null;
		Statement st = null;
		ResultSet rs = null;
		
		try {
			ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
			cn = ds.getConnection();
			st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			return st.execute(sql);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if(rs != null)
				try {
					rs.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(st != null)
				try {
					st.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(cn != null)
				try {
					cn.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(ctx != null)
				try {
					ctx.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * 执行更新
	 * @param sql sql语句
	 * @return (1) 对于 SQL 数据操作语言 (DML) 语句,返回行计数 (2) 对于什么都不返回的 SQL 语句,返回 0 
	 * @throws SQLException
	 */
	public static int executeUpdate(String sql) throws SQLException{
		Context ctx = null;
		DataSource ds = null;
		Connection cn = null;
		Statement st = null;
		ResultSet rs = null;
		
		try {
			ctx = new InitialContext();
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/OracleDS");
			cn = ds.getConnection();
			st = cn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
			return st.executeUpdate(sql);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if(rs != null)
				try {
					rs.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(st != null)
				try {
					st.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(cn != null)
				try {
					cn.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
			if(ctx != null)
				try {
					ctx.close();
				} catch ( Exception e) {
					e.printStackTrace();
				}
		}
	}
	
}



// 调用JdbcUtil.executeQuery()

public List getList() {
	RowSet crs = null;
	List<E> list = new ArrayList<E>();
	try {
		crs = JdbcUtil.executeQuery("select * from user");
		while(crs.next()){
			// todo
			list.add();
		}
		return list;
	} catch (Exception e) {
		e.printStackTrace();
	} finally{
		if(crs != null)
			try {
				crs.close();
			} catch ( Exception e) {
				e.printStackTrace();
			}
	}
	return null;
}

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