alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (SimpleJdbcCall.java)

This example Spring Framework source code file (SimpleJdbcCall.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

abstractjdbccall, hashmap, map, map, mapsqlparametersource, object, object, parameterizedrowmapper, simplejdbccall, simplejdbccall, simplejdbccalloperations, sql, suppresswarnings, t, t, util

The Spring Framework SimpleJdbcCall.java source code

/*
 * Copyright 2002-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.jdbc.core.simple;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

/**
 * A SimpleJdbcCall is a multi-threaded, reusable object representing a call
 * to a stored procedure or a stored function. It provides meta data processing
 * to simplify the code needed to access basic stored procedures/functions.
 * All you need to provide is the name of the procedure/function and a Map
 * containing the parameters when you execute the call. The names of the
 * supplied parameters will be matched up with in and out parameters declared
 * when the stored procedure was created.
 *
 * <p>The meta data processing is based on the DatabaseMetaData provided by
 * the JDBC driver. Since we rely on the JDBC driver this "auto-detection"
 * can only be used for databases that are known to provide accurate meta data.
 * These currently include Derby, MySQL, Microsoft SQL Server, Oracle and DB2.
 * For any other databases you are required to declare all parameters explicitly.
 * You can of course declare all parameters explicitly even if the database provides
 * the necessary meta data. In that case your declared parameters will take precedence.
 * You can also turn off any mete data processing if you want to use parameter names
 * that do not match what is declared during the stored procedure compilation.
 *
 * <p>The actual insert is being handled using Spring's
 * {@link org.springframework.jdbc.core.JdbcTemplate}.
 *
 * <p>Many of the configuration methods return the current instance of the SimpleJdbcCall
 * to provide the ability to string multiple ones together in a "fluid" interface style.
 *
 * @author Thomas Risberg
 * @since 2.5
 * @see java.sql.DatabaseMetaData
 * @see org.springframework.jdbc.core.JdbcTemplate
 */
public class SimpleJdbcCall extends AbstractJdbcCall implements SimpleJdbcCallOperations {

	/**
	 * Constructor that takes one parameter with the JDBC DataSource to use when creating the
	 * JdbcTemplate.
	 * @param dataSource the <code>DataSource to use
	 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource
	 */
	public SimpleJdbcCall(DataSource dataSource) {
		super(dataSource);
	}

	/**
	 * Alternative Constructor that takes one parameter with the JdbcTemplate to be used.
	 * @param jdbcTemplate the <code>JdbcTemplate to use
	 * @see org.springframework.jdbc.core.JdbcTemplate#setDataSource
	 */
	public SimpleJdbcCall(JdbcTemplate jdbcTemplate) {
		super(jdbcTemplate);
	}


	public SimpleJdbcCall withProcedureName(String procedureName) {
		setProcedureName(procedureName);
		setFunction(false);
		return this;
	}

	public SimpleJdbcCall withFunctionName(String functionName) {
		setProcedureName(functionName);
		setFunction(true);
		return this;
	}

	public SimpleJdbcCall withSchemaName(String schemaName) {
		setSchemaName(schemaName);
		return this;
	}

	public SimpleJdbcCall withCatalogName(String catalogName) {
		setCatalogName(catalogName);
		return this;
	}

	public SimpleJdbcCall withReturnValue() {
		setReturnValueRequired(true);
		return this;
	}

	public SimpleJdbcCall declareParameters(SqlParameter... sqlParameters) {
		for (SqlParameter sqlParameter : sqlParameters) {
			if (sqlParameter != null) {
				addDeclaredParameter(sqlParameter);
			}
		}
		return this;
	}

	public SimpleJdbcCall useInParameterNames(String... inParameterNames) {
		setInParameterNames(new HashSet<String>(Arrays.asList(inParameterNames)));
		return this;
	}

	public SimpleJdbcCall returningResultSet(String parameterName, ParameterizedRowMapper rowMapper) {
		addDeclaredRowMapper(parameterName, rowMapper);
		return this;
	}

	public SimpleJdbcCall withoutProcedureColumnMetaDataAccess() {
		setAccessCallParameterMetaData(false);
		return this;
	}


	@SuppressWarnings("unchecked")
	public <T> T executeFunction(Class returnType, Map args) {
		return (T) doExecute(args).get(getScalarOutParameterName());
	}

	@SuppressWarnings("unchecked")
	public <T> T executeFunction(Class returnType, MapSqlParameterSource args) {
		return (T) doExecute(args).get(getScalarOutParameterName());
	}

	@SuppressWarnings("unchecked")
	public <T> T executeObject(Class returnType, Map args) {
		return (T) doExecute(args).get(getScalarOutParameterName());
	}

	@SuppressWarnings("unchecked")
	public <T> T executeObject(Class returnType, MapSqlParameterSource args) {
		return (T) doExecute(args).get(getScalarOutParameterName());
	}

	public Map<String, Object> execute() {
		return doExecute(new HashMap<String, Object>());
	}

	public Map<String, Object> execute(Map args) {
		return doExecute(args);
	}

	public Map<String, Object> execute(SqlParameterSource parameterSource) {
		return doExecute(parameterSource);
	}

}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework SimpleJdbcCall.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.