org.apache.commons.dbutils.OutParameter Maven / Gradle / Ivy
Show all versions of jsqlbox Show documentation
/*
* Copyright 2014 The Apache Software Foundation.
*
* 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.apache.commons.dbutils;
import java.sql.CallableStatement;
import java.sql.SQLException;
/**
* Represents an OUT parameter for a stored procedure. When running a stored
* procedure with {@link QueryRunner}, pass an instance of
* OutParameter
to indicate that the parameter at that index is an
* OUT parameter. The value of the parameter may be obtained from the
* OutParameter
instance via {@link #getValue() }.
*
* INOUT parameters are also supported by setting the value
of
* the OutParameter
instance before invoking the stored procedure.
*
* @param the class of the parameter; should be compatible via cast with the
* class returned by the CallableStatement.getObject(int)
method.
*/
public class OutParameter {
private final int sqlType;
private final Class javaType;
private T value = null;
/**
* Construct an OutParameter
for the given JDBC SQL type and
* Java type.
* @param sqlType the JDBC SQL type of the parameter as in
* java.sql.Types
.
* @param javaType the Java class of the parameter value, cast compatible
* with the type returned by CallableStatement.getObject(int)
* for the JDBC type given by sqlType
.
*/
public OutParameter(int sqlType, Class javaType) {
this.sqlType = sqlType;
this.javaType = javaType;
}
/**
* Construct an OutParameter
for the given JDBC SQL type and
* Java type and with the given value. The parameter will be treated as an
* INOUT parameter if the value is null.
* @param sqlType the JDBC SQL type of the parameter as in
* java.sql.Types
.
* @param javaType the Java class of the parameter value, cast compatible
* with the type returned by CallableStatement.getObject(int)
* for the JDBC type given by sqlType
.
* @param value the IN value of the parameter
*/
public OutParameter(int sqlType, Class javaType, T value) {
this.sqlType = sqlType;
this.javaType = javaType;
this.value = value;
}
/**
* Get the JDBC SQL type for this OUT parameter.
* @return the JDBC SQL type for this OUT parameter.
*/
public int getSqlType() {
return sqlType;
}
/**
* Get the Java class for this OUT parameter.
* @return the Java class for this OUT parameter.
*/
public Class getJavaType()
{
return javaType;
}
/**
* Get the value of the OUT parameter. After the stored procedure has
* been executed, the value is the value returned via this parameter.
* @return the value of the OUT parameter.
*/
public T getValue() {
return value;
}
/**
* Set the value of the OUT parameter. If the value is not null when the
* stored procedure is executed, then the parameter will be treated like an
* INOUT parameter.
* @param value the new value for the parameter.
*/
public void setValue(T value) {
this.value = value;
}
/**
* Set the value using the return value of the parameter an the given index
* from the given CallableStatement
.
* @param stmt the already executed statement
* @param index the (1-based) index of the parameter
* @throws SQLException when the value could not be retrieved from the
* statement.
*/
void setValue(CallableStatement stmt, int index) throws SQLException {
Object object = stmt.getObject(index);
value = javaType.cast(object);
}
/**
* Set up the given statement by registering an OUT parameter at the given
* index using the sqlType
and value
of this
* OutParameter
. If the value is not null, the parameter is
* treated like an INOUT parameter and the value is set on the statement.
* @param stmt the statement the parameter should register on.
* @param index the (1-based) index of the parameter.
* @throws SQLException if the parameter could not be registered, or if the
* value of the parameter could not be set.
*/
void register(CallableStatement stmt, int index) throws SQLException {
stmt.registerOutParameter(index, sqlType);
if (value != null) {
stmt.setObject(index, value);
}
}
@Override
public String toString()
{
return "OutParameter{" + "sqlType=" + sqlType + ", javaType="
+ javaType + ", value=" + value + '}';
}
}