日期:2014-05-16  浏览次数:20423 次

数据库操作模板类及配置

这个模板类我已用过几次了,非常好用,基本不需要做修改,分享给大家.详细使用方法请参照我的另一篇笔记http://ericblog.iteye.com/blog/1014364

?

用到了 c3p0 链接池

请下载 c3p0-0.9.1.2.jar mysql-connector-java-5.1.15-bin.jar 两个 jar

?

一、JDBCTemplate类

?

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import com.ccit.dao.inter.RowMapper;
import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCTemplate {

	private static JDBCTemplate template;
	private DataSource ds;
	private Connection conn;
	private Statement state;
	private PreparedStatement ps;
	
	public JDBCTemplate() {
		ds = new ComboPooledDataSource();
	}
	
	public static JDBCTemplate getJDBCTemplate(){
		if(null == template){
			template = new JDBCTemplate();
		}
		return template;
	}
	
	public Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	
	/**
	 * 更新数据表,update,delete,insert
	 * @param sql
	 * @return
	 */
	public int ExecuteUpdate(String sql){
		int flag = 0;
		try {
			conn = getConnection();
			state = conn.createStatement();
			flag = state.executeUpdate(sql);
		} catch (SQLException e) {
			System.out.println("ERROR001:更新数据失败");
			e.printStackTrace();
		}finally{
			try {
				if(null != ps) ps.close();
				if(null != state) state.close();
				if(null != conn) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return flag;
	}
	
	/**
	 * 用于更新数据表 update,delete,insert
	 * @param sql
	 * @param param
	 * @return
	 */
	public int ExecuteUpdate(String sql,Object ...param ){
		int flag = 0;
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			 for(int i=0;i<param.length;i++){
				 ps.setObject(i+1, param[i]);
			 }
			 flag = ps.executeUpdate();
		} catch (SQLException e) {
			System.out.println("ERROR001:更新数据失败");
			e.printStackTrace();
		}finally{
			try {
				if(null != ps) ps.close();
				if(null != state) state.close();
				if(null != conn) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}

	/**
	 * 用于相同sql语句的批量处理
	 * @param sql
	 * @param params
	 * @return
	 */
	public int[] executeBatch(String sql,Object[]...params){
		int flag[] = new int[0];
		try {
			conn = getConnection();
			ps = conn.prepareStatement(sql);
			for(int i=0;i<params.length;i++){
				for(int j=0;j<params[i].length;j++){
					ps.setObject(j+1, params[i][j]);
				}
				ps.addBatch();
			}
			flag = ps.executeBatch();
		} catch (SQLException e) {
			System.out.println("ERROR001:更新数据失败");
			e.printStackTrace();
		}finally{
			try {
				if(null != ps) ps.close();
				if(null != state) state.close();
				if(null != conn) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return flag;
	}
	
	/**
	 * 返回表里的所有数据
	 * @param sql
	 * @param rm
	 * @return
	 */
	public List executeQuery(String sql,RowMapper rm){
		List list=new ArrayList(0);
		try {
			conn = getConnection();
			state = conn.createStatement();
			ResultSet rs=state.executeQuery(sql);
			while(rs.next()){
				list.add(rm.RowMap(rs));
			}
		} catch (SQLException e) {
			System.out.println("ERROR002:查询数据失败");
			e.printStackTrace();
		}finally{
			try {
				if(null != ps) ps.close();
				if(null != state) state.close();
				if(null != conn) conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 返回表里的所有数据
	 * @param sql
	 * @param rm
	 * @param params
	 * @return
	 */
	public List executeQuery(String sql,RowMapper rm,Object ...params){
		List l