Share on Google+Share on Google+

SQLExceptionTranslator example

This section is about the handling or translating errors using SQLExceptionTranslator interface.

SQLExceptionTranslator example

The SQLExceptionTranslator interface is implemented by  classes which are doubtful in term of data access. In other words we can say that it is a Strategy interface for translating between SQLExceptions and Spring's data access strategy-agnostic DataAccessException hierarchy.

The 'SQLExceptionTranslator'  implements 'SQLErrorCodeSQLExceptionTranslator' for mapping error code that comes out of the database to catch  the SQLException to meaningful application-level exceptions. The 'SQLErrorCodes' JavaBean class translates error code. The 'SQLErrorCodesFactory'  creates  'SQLErrorCodes' which is based on the 'sql-error-codes.xml' .


The given below example implements 'SQLExceptionTranslator' interface using 'SQLErrorCodeSQLExceptionTranslator' to  translate exceptions to meaningful application-level exceptions. In the class "CustomSQLErrorCodesTranslator" , the error with error code -12345 is translated  and all the remaining  errors are left for translation by the SQLExceptionTranslator interface. We pass the method setExceptionTranslator to JdbcTemplate because it is nesessary to implement custom translator. Given below is the  demonstrate of this custom translator can be used :

package net.roseindia;

import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.DeadlockLoserDataAccessException;

public class CustomSQLErrorCodesTranslator extends
		SQLErrorCodeSQLExceptionTranslator {
	protected DataAccessException customTranslate(String task, String sql,
			SQLException sqlex) {
		if (sqlex.getErrorCode() == -12345) {
			return new DeadlockLoserDataAccessException(task, sqlex);
		return null;


package net.roseindia;

import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;

public class ExceptionTranslator {
	private JdbcTemplate jdbcTemplate;

	public void setDataSource(DataSource dataSource) {
	// create a JdbcTemplate and set data source
	this.jdbcTemplate = new JdbcTemplate();
	// create a custom translator and set the DataSource for the default
	// translation lookup
	CustomSQLErrorCodesTranslator tr = new CustomSQLErrorCodesTranslator();

	public void updateRecords(String fn, long id) {
	// use the prepared JdbcTemplate for this update
			"update skills set first_name = ? where id = ?", fn, id);


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<bean id="ET" class="net.roseindia.ExceptionTranslator">
<property name="dataSource" ref="dataSource" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://" />
<property name="user" value="root" />
<property name="password" value="root" />
<context:property-placeholder location="" />

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;


public class ExceptionTranslatorMain {
	public static void main(String[] args) {
	XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

	ExceptionTranslator myBean = (ExceptionTranslator) beanFactory

	myBean.updateRecords("Ankit Kumar", 1101);


Before execution of code (database table-skills):

See the name of id 1101 , it  will change after execution .

See the selected line , it is prompting for loading of SQLErrorCodes (after execution of code):

After execution of code , the database table "skills" :

Download Source Code



Posted on: August 28, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null