package com.seeyon.apps.src_gettaxrate.util;

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



public class BaseUtil {

	
	PreparedStatement ps = null;
	ResultSet res = null;
	Connection conn =null;
	/**
	 * sql查询工具
	 * @param sql sql语句
	 * @param params sql参数  
	 * @return 结果集
	 */
	public ResultSet executeQuery(String sql, Object[] params)
	{
		conn = JdbcUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			for(int i = 0; i < params.length; i++){
				ps.setObject(i+1, params[i]);
			}
			res = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	
	public ResultSet executeQuery2(String sql)
	{
		conn = JdbcUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			res = ps.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	

	/**
	 * sql更新工具
	 * @param sql
	 * @param params
	 * @return
	 */
	public int executeUpdate(String sql, Object[] params)
	{
		int res = 0;
		conn = JdbcUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			for(int i = 0; i < params.length; i++)
			{
				ps.setObject(i+1, params[i]);
			}
			res = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	public int executeUpdate2(String sql)
	{
		int res = 0;
		conn = JdbcUtil.getConnection();
		try {
			ps = conn.prepareStatement(sql);
			res = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}
	

	
	
	public void close() {
		release(res,null,conn,ps);
	}
	//定义一个释放资源的方法;  
		/*
		 * 定义一个方法： 释放资源： 直接将rs stmt conn 全部释放： 
		 */
		public static void release(ResultSet rs ,Statement stmt , Connection conn,PreparedStatement ps){
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				rs=null;
			}
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				stmt=null;
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				conn=null;
			}
			if(ps!=null){
				try {
					ps.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
				ps=null;
			}
			
		}
}
