DBHelper高级封装

package com.news.dao;

import java.sql.*;
import java.util.SortedMap;
import java.util.ResourceBundle;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
import com.news.po.PageInfo;



public final class DbHelper {
	static BasicDataSource ds = new BasicDataSource();
	static{
		ResourceBundle res = ResourceBundle.getBundle("jdbc");
		ds.setDriverClassName(res.getString("driver"));
		ds.setUsername(res.getString("username"));
		ds.setPassword(res.getString("password"));
		ds.setUrl(res.getString("url"));
	}
	/**
	 * 打开连接
	 * @return --打开成功返回连接,否则返回null
	 */
	private static Connection getConnection(){
		try {
			return ds.getConnection();
		}  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 执行增删改
	 * @param sql --delete from Users where UserId=?
	 * @param params
	 * @return
	 */
	public static int executeNonQuery(String sql,Object...params){
		Connection c = null;
		PreparedStatement ps = null;
		try {
			c = getConnection();
			ps = c.prepareStatement(sql);
			if(params!=null && params.length>0){
				for (int i = 0; i < params.length; i++) {
					ps.setObject(i+1, params[i]);
				}
			}
			return ps.executeUpdate();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			try {
				ps.close();
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 执行查询(适合返回多行多列的情况)
	 * @param sql
	 * @param params
	 * @return
	 */
	public static Result executeQuery(String sql,Object...params){
		Connection c = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		Result r = null;
		try {
			c = getConnection();
			ps = c.prepareStatement(sql);
			if(params!=null && params.length>0){
				for (int i = 0; i < params.length; i++) {
					ps.setObject(i+1, params[i]);
				}
			}
			rs = ps.executeQuery();
			r = ResultSupport.toResult(rs);//将ResultSet封装成Result
			return r;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			try {
				rs.close();
				ps.close();
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 执行查询(适合返回单行单列的情况)
	 * @param sql
	 * @param params
	 * @return
	 */
	public static Object executeScaler(String sql,Object...params){
		Connection c = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			c = getConnection();
			ps = c.prepareStatement(sql);
			if(params!=null && params.length>0){
				for (int i = 0; i < params.length; i++) {
					ps.setObject(i+1, params[i]);
				}
			}
			rs = ps.executeQuery();
			if(rs!=null && rs.next()){
				return rs.getObject(1);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			try {
				rs.close();
				ps.close();
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static void pager(PageInfo pi){
		Connection c = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			c = getConnection();
			//查询总条数			
			String sql = "select count(*) from "+pi.getTableName();
			if(null!=pi.getWhere() && !"".equals(pi.getWhere())){
				sql+=" where "+pi.getWhere();
			}
			ps = c.prepareStatement(sql);
			rs = ps.executeQuery();
			if(rs!=null && rs.next()){
				pi.setRecordCount(rs.getInt(1));
			}
			
			sql = "select * from "+pi.getTableName();
			if(null!=pi.getWhere() && !"".equals(pi.getWhere())){
				sql+=" where "+pi.getWhere();
			}
			sql+=" limit "+(pi.getPageIndex()*pi.getPageSize());
			sql+=","+pi.getPageSize();			
			rs.close();
			ps.close();
			ps = c.prepareStatement(sql);
			rs = ps.executeQuery();
			Result r = ResultSupport.toResult(rs);
			pi.setResult(r);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally{
			try {
				rs.close();
				ps.close();
				c.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}	

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值