java jdbc数据库连接代码封装

一个很好的封装jdbc数据库类

package cn.com.util;

import java.sql.*;
import java.util.*;

/**
 * 数据库操作工具类
 * 
 * @author Administrator
 * 
 */
public class DBUtil {
	private static Connection con = null;// 连接对象
	private static PreparedStatement psmt = null;// 预编译对象
	private static ResultSet rs = null;// 结果集对象
	private static CallableStatement csmt = null;// 过程对象
/**
 *  获得连接对象
 * @return
 */
	public static Connection getConnetion() {
		try {

			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager
					.getConnection("jdbc:mysql://localhost:3306/mytest","root","gc7923959");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println("找不到驱动");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("数据库连接失败");
		}
		return con;
	}

	/**
	 * 
	 * @param sql
	 * @return
	 */
	public static PreparedStatement getPreparedStatement(String sql) {
		con = getConnetion();
		try {
			psmt = con.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return psmt;
	}
/**
 * 查询获得所有
 * @param sql
 * @return 结果集
 */
	public static ResultSet getResultSet(String sql) {
		psmt = getPreparedStatement(sql);
		try {
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 更新操作
	 * @param sql
	 * @param params
	 * @return   受影响的行数
	 */
	public static int executeUpdate(String sql, List<Object> params) {
		int count = 0;
		
		psmt = getPreparedStatement(sql);
		bindPramas(psmt, params);
		try {
			count = psmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			CloseAll();
		}

		return count;
	}

	/**
	 * 查询  带参数
	 * @param sql
	 * @param params
	 * @return  结果集
	 */
	public static ResultSet executeQuery(String sql, List<Object> params) {
		psmt = getPreparedStatement(sql);
		if (params != null) {
			bindPramas(psmt, params);
		}
		try {
			rs = psmt.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(psmt);
		return rs;
	}

	public static CallableStatement getCallableStatement(String sql) {
		try {
			csmt = getConnetion().prepareCall(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return csmt;

	}


	public static Object executeCallObject(String sql,
			Map<Integer, Object> params) {
		Object obj = null;
		try {
			int index = 0;// 输入参数下标
			csmt = getCallableStatement(sql);
			for (int i = 1; i <= params.size(); i++) {
				if (params.get(i).toString().equals("Integer")) {
					csmt.registerOutParameter(i, Types.INTEGER);
					index = i;
				} else {
					csmt.setObject(i, params.get(i));
				}

			}

			csmt.execute();
			obj = csmt.getObject(index);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}

	public static void bindPramas(PreparedStatement psmt, List<Object> params) {
		int index = 0;
		if (params != null) {
			for (Object p : params) {
				try {
					psmt.setObject(index + 1, p);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				index++;

			}

		}
	}

	public static void CloseAll() {
		try {
			if (con != null) {

				con.close();

			}
			if (psmt != null) {
				psmt.close();
			}
			if (rs != null) {
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值