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();
			}
		}
	}	

}

發佈了32 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章