DBTools

package com.huawei.db;

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

import org.apache.log4j.Logger;

/**
* 针对Connection的单次调用,然后马上释放的原则
* @author z00133176
*
*/
public class DBTools
{
/**
* Colse Connection
* @param p
* @return
*/
public static boolean close(Connection c)
{
        return close(c, null);
    }

/**
* Colse PreparedStatement
* @param p
* @return
*/
public static boolean close(PreparedStatement p)
{
        return close(p, null);
    }

/**
* Colse ResultSet
* @param p
* @return
*/
public static boolean close(ResultSet rs)
{
   return close(rs, null);
}

public static boolean close(Connection conn, Logger log)
{
    try
    {
        if (conn != null)
        {
        conn.close();
        }
        conn = null;
       
        return true;
    }
    catch (SQLException ex)
    {
        if (null != log)
        {
            log.error("Colse Connection failed");
        }
        return false;
    }
}

public static boolean close(PreparedStatement p, Logger log)
{
    try
    {
        if (p != null)
        {
            p.close();
        }
        p = null;
       
        return true;
    }
    catch (SQLException ex)
    {
        if (null != log)
        {
            log.error("Colse PreparedStatement failed");
        }
        return false;
    }
}

public static boolean close(ResultSet rs, Logger log)
{
    try
    {
        if (rs != null)
        {
        rs.close();
        }
       
        rs = null;
       
        return true;
    }
    catch (SQLException ex)
    {
        if (null != log)
        {
            log.error("Colse ResultSet failed");
        }
        return false;
    }
}
}

你可能感兴趣的:(DBTools)