自定义异常

 

1、异常工具类

    

public abstract class JDBCExceptionUtils {

	public JDBCExceptionUtils() {
	}

	public static String buildMessage(String message, Throwable cause) {

		if (null != cause) {

			StringBuffer buf = new StringBuffer();

			if (null != message && !"".equals(message)) {
				
				buf.append(message).append("; ");
			}
			
			buf.append("jdbc exception is ").append(cause);

			return buf.toString();

		} else {
			return message;
		}
	}
}

 

2、异常类

  

public class JDBCException extends Exception {

	/**
	 * @Fields serialVersionUID : TODO
	 */
	private static final long serialVersionUID = 4520101166979175689L;

	public JDBCException(String msg) {
		super(msg);
	}

	public JDBCException(String msg, Throwable cause) {
		super(msg, cause);
	}

	public String getMessage() {
		return JDBCExceptionUtils.buildMessage(super.getMessage(), getCause());
	}

	public Throwable getRootCause() {

		Throwable rootCause = null;

		for (Throwable cause = getCause(); cause != null && cause != rootCause; 
				cause = cause.getCause()) {
			rootCause = cause;
		}

		return rootCause;
	}
	
	public Throwable getMostSpecificCause()
    {
        Throwable rootCause = getRootCause();
        return ((Throwable) (rootCause == null ? this : rootCause));
    }

    @SuppressWarnings("rawtypes")
	public boolean contains(Class exType)
    {
        if(exType == null)
            return false;
        if(exType.isInstance(this))
            return true;
        Throwable cause = getCause();
        if(cause == this)
            return false;
        if(cause instanceof JDBCException)
            return ((JDBCException)cause).contains(exType);
        do
        {
            if(cause == null)
                break;
            if(exType.isInstance(cause))
                return true;
            if(cause.getCause() == cause)
                break;
            cause = cause.getCause();
        } while(true);
        return false;
    }
}

 

3、使用自定义异常

    

    

/**
	 * @Title: getConnection
	 * @Description: 取得与数据库的连接
	 * @return
	 * @author 
	 */
	public Connection getConnection() throws JDBCException {

		// 数据库连接
		Connection conn = null;

		try {
			// 取得与数据库的连接
			conn = DriverManager.getConnection(url, userName, password);

			if (null != conn) {
				// 设置事务隔离级别
				conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
			}
		} catch (SQLException e) {
			throw new JDBCException("Get database connection failure.", e);
		} catch (Exception e) {
			throw new JDBCException("others exception.",e);
		}

		return conn;
	}

	/**
	 * @Title: closeConnection
	 * @Description: 关闭数据库连接
	 * @author 
	 */
	public void closeConnection() throws JDBCException {
		// 取得与数据库的连接
		Connection conn = getConnection();

		if (null != conn) {
			try {
				// 关闭连接
				conn.close();
			} catch (SQLException e) {
				throw new JDBCException("close database connection failure.",e);
			} catch (Exception e) {
				throw new JDBCException("others exception.",e);
			}
		}
	}

 

 

 

4、测试

    

     

public static void main(String[] args) {

		

		try {
			Connection conn = JDBCTools.getInstance().getConnection();
			
			boolean bool = false;

			if (null != conn) {
				bool = conn.isClosed();
			} else {
				bool = true;
			}

			if (!bool) {
				System.out.println("connect successfully");
			} else {
				System.out.println("connect failure");
			}
			JDBCTools.getInstance().closeConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (JDBCException e){
			System.out.println(e);
		}
	}

  

 

5、测试结果

    

com.java.common.exception.JDBCException: Get database connection failure.; jdbc exception is java.sql.SQLException: Listener refused the connection with the following error:
ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
The Connection descriptor used by the client was:
localhost:1521:orcl

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值