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