1 16 package scriptella.jdbc; 17 18 import scriptella.spi.ProviderException; 19 20 import java.sql.SQLException ; 21 import java.util.List ; 22 23 29 public class JdbcException extends ProviderException { 30 31 public JdbcException(String message) { 32 super(message); 33 } 34 35 public JdbcException(String message, Throwable cause) { 36 super(message, cause); 37 initVendorCodes(cause); 38 } 39 40 public JdbcException(String message, Throwable cause, String sql, List <?> parameters) { 41 super(message, cause); 42 initVendorCodes(cause); 43 setErrorStatement(sql, parameters); 44 } 45 46 public JdbcException(String message, Throwable cause, String sql) { 47 super(message, cause); 48 initVendorCodes(cause); 49 setErrorStatement(sql, null); 50 } 51 public JdbcException(String message, String sql) { 52 super(message); 53 setErrorStatement(sql, null); 54 } 55 56 57 ProviderException setErrorStatement(String sql, List <?> params) { 58 return super.setErrorStatement(sql + ((params == null || params.isEmpty()) ? "" : (". Parameters: " + params))); 59 } 60 61 62 protected void initVendorCodes(Throwable t) { 63 if (t instanceof SQLException ) { 64 SQLException sqlEx = (SQLException ) t; 65 addErrorCode(sqlEx.getSQLState()).addErrorCode(String.valueOf(sqlEx.getErrorCode())); 66 } 67 68 } 69 70 public Throwable getNativeException() { 71 for (Throwable e = getCause(); e != null; e = e.getCause()) { 73 if (e instanceof SQLException ) { 74 return e; 75 } 76 } 77 return null; 78 } 79 80 public String getProviderName() { 81 return "JDBC"; 82 } 83 84 } 85 | Popular Tags |