Code example for SQLException

Methods: getErrorCode, getLocalizedMessage, getMessage, getSQLState

0
	} 
 
	public BusinessException interpret(java.sql.SQLException sqle) {
		// DBType dbType = detectDatabaseType(); 
 
		int errorCode = sqle.getErrorCode();
		String sqlState = sqle.getSQLState();
		String message = sqle.getMessage();
 
		// if (dbType == DBType.HSQLDB) 
 
		if (errorCode == SQL_ERRORCODE_HSQLDB_UNIQUE_CONSTRAINT_VIOLATION) {
			// This will be a message like this: 
			// Violation of unique constraint SYS_CT_63: duplicate value(s) for column $$ in statement [insert into 
			// ... 
			if (message.startsWith("Violation of unique constraint")) {
				String[] chunks = sqle.getMessage().split(" ");
				String technicalMessage = sqle.getLocalizedMessage();
 
				if (chunks[4].startsWith("SYS_CT") || chunks[2].startsWith("unique")) {
					BusinessException be = new DuplicateAlternateKeyException(technicalMessage);
					return be;
				} 
			} 
		} 
 
		else if (errorCode == SQL_ERRORCODE_HSQLDB_INTEGRITY_CONSTRAINT_VIOLATION) {
			String[] chunks = sqle.getMessage().split(" ");
			if (chunks[4].equals("table:") && chunks[6].equals("in")) {
				String entityName = chunks[5];
				// String constraintName = chunks[3]; 
				BusinessException be = new CannotDeleteIsReferencedException(entityName);
				return be;
			} 
		} 
 
		// else if (dbType == DBType.MYSQL) 
 
		else if (errorCode == SQL_ERRORCODE_MYSQL_DUPLICATE_ENTRY) {
			// Expect message like "Duplicate entry 'john' for key 'name'" 
			String technicalMessage = sqle.getLocalizedMessage();
			String[] chunks = message.split("\'");
 
			String value = chunks[1];
			String field = chunks[3];
 
			if (field.equals("PRIMARY")) {
				BusinessException be = new DuplicatePrimaryKeyException(value);
				return be;
			} 
			else { 
				BusinessException be = new DuplicateAlternateKeyException(field, technicalMessage);
				return be;
			} 
		} 
 
		else if (errorCode == SQL_ERRORCODE_MYSQL_CANNOT_DELETE_OR_UPDATE_A_PARENT_ROW) {
			// Expect message starting with "Cannot delete" 
			String[] chunks = message.split("`");
 
			if (message.contains("foreign key constraint fails")) {
				String referencedByTableName = chunks[3];
				BusinessException be = new CannotDeleteIsReferencedException(referencedByTableName);
				return be;
			} 
		} 
 
		else if (errorCode == SQL_ERRORCODE_MYSQL_CANNOT_ADD_OR_UPDATE_A_CHILD_ROW) {
			// Expect message starting with "Cannot delete" 
			String[] chunks = message.split("`");
 
			if (message.contains("foreign key constraint fails")) {
				@SuppressWarnings("unused") 
				String referencedByTableName = chunks[3];
				// BusinessException be = new CannotDeleteIsReferencedException(referencedByTableName); 
				// return be; 
				LOGGER.error("Cannot currently handle SQLException " + sqle);
				LOGGER.error("  ErrorCode = " + errorCode);
				LOGGER.error("  SQLState = " + sqlState);
				LOGGER.error("  Message = " + message);
				throw new UnexpectedException(sqle);
			} 
		} 
 
		// else if (dbType == DBType.DERBY) 
 
		else if (sqlState.equals(SQL_STATE_DERBY_DUPLICATE_ENTRY)) {
			// Expect message containing "duplicate key value" 
			String technicalMessage = sqle.getLocalizedMessage();
			String[] chunks = message.split(" ");
			if (technicalMessage.contains("duplicate key value in a unique or primary key constraint")
					&& chunks[27].equals("on")) {
				String tableName = chunks[28].replace("'", "").replace(".", "");
				BusinessException be = new DuplicatePrimaryOrAlternateKeyException(tableName);
				return be;
			} 
		} 
 
		else if (sqlState.equals(SQL_STATE_DERBY_INTEGRITY_CONSTRAINT_VIOLATION)) {
			String[] chunks = sqle.getMessage().split(" ");
			if (chunks[0].equals("DELETE") && chunks[2].equals("table")) {
				String tableName = chunks[3].replace("'", "");
				String id = chunks[14].replace("(", "").replace(")", "").replace(".", "");
				// String constraintName = chunks[11]; 
				BusinessException be = new CannotDeleteIsReferencedException(tableName, id);
				return be;
			} 
		} 
 
		// else { 
		// throw new IllegalStateException("Should never get here."); 
		// } 
 
		LOGGER.error("Cannot interpret SQLException " + sqle);
		LOGGER.error("  ErrorCode = " + errorCode);
		LOGGER.error("  SQLState = " + sqlState);
		LOGGER.error("  Message = " + message);
		throw new UnexpectedException(sqle);
	} 
 
	// 
	// This is unreliable because hibernate.dialect is often not ready in time for us. 
	//