📄 jtdscallablestatement.java
字号:
// jTDS JDBC Driver for Microsoft SQL Server and Sybase
// Copyright (C) 2004 The jTDS Project
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
package net.sourceforge.jtds.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.net.MalformedURLException;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Ref;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
/**
* jTDS implementation of the java.sql.CallableStatement interface.
*<p>
* Implementation note:
* <ol>
* <li>This class is a simple subclass of PreparedStatement and mainly adds support for
* setting parameters.
* <li>The class supports named parameters in a similar way to the
* patch supplied by Tommy Sandstrom to the original jTDS code.
* </ol>
*
* @author Mike Hutchinson
* @version $Id: JtdsCallableStatement.java,v 1.23 2007/07/12 21:03:23 bheineman Exp $
*/
public class JtdsCallableStatement extends JtdsPreparedStatement implements CallableStatement {
/** Last parameter retrieved was null. */
protected boolean paramWasNull;
/**
* Construct a CallableStatement object.
*
* @param connection The connection owning this statement.
* @param sql The SQL statement specifying the procedure to call.
* @param resultSetType The result set type eg FORWARD_ONLY.
* @param concurrency The result set concurrency eg READ_ONLY.
* @throws SQLException
*/
JtdsCallableStatement(ConnectionJDBC2 connection, String sql, int resultSetType, int concurrency)
throws SQLException {
super(connection, sql, resultSetType, concurrency, false);
}
/**
* Find a parameter by name.
*
* @param name The name of the parameter to locate.
* @param set True if function is called from a set / register method.
* @return The parameter index as an <code>int</code>.
* @throws SQLException
*/
int findParameter(String name, boolean set)
throws SQLException {
checkOpen();
for (int i = 0; i < parameters.length; i++){
if (parameters[i].name != null && parameters[i].name.equalsIgnoreCase(name))
return i + 1;
}
if (set && !name.equalsIgnoreCase("@return_status")) {
for (int i = 0; i < parameters.length; i++){
if (parameters[i].name == null) {
parameters[i].name = name;
return i + 1;
}
}
}
throw new SQLException(Messages.get("error.callable.noparam", name), "07000");
}
/**
* Retrieve the value of an output parameter.
*
* @param parameterIndex the ordinal position of the parameter
* @return the parameter value as an <code>Object</code>
* @throws SQLException if the parameter has not been set
*/
protected Object getOutputValue(int parameterIndex)
throws SQLException {
checkOpen();
ParamInfo parameter = getParameter(parameterIndex);
if (!parameter.isOutput) {
throw new SQLException(
Messages.get("error.callable.notoutput",
new Integer(parameterIndex)),
"07000");
}
Object value = parameter.getOutValue();
paramWasNull = (value == null);
return value;
}
/**
* Check that this statement is still open.
*
* @throws SQLException if statement closed.
*/
protected void checkOpen() throws SQLException {
if (closed) {
throw new SQLException(
Messages.get("error.generic.closed", "CallableStatement"), "HY010");
}
}
/**
* Execute the SQL batch on a MS server.
* @param size the total size of the batch.
* @param executeSize the maximum number of statements to send in one request.
* @param counts the returned update counts.
* @return Chained exceptions linked to a <code>SQLException</code>.
* @throws SQLException
*/
protected SQLException executeMSBatch(int size, int executeSize, ArrayList counts)
throws SQLException {
if (parameters.length == 0) {
// No parameters so we can execute as a simple batch
return super.executeMSBatch(size, executeSize, counts);
}
SQLException sqlEx = null;
for (int i = 0; i < size;) {
Object value = batchValues.get(i);
++i;
// Execute batch now if max size reached or end of batch
boolean executeNow = (i % executeSize == 0) || i == size;
tds.startBatch();
tds.executeSQL(sql, procName, (ParamInfo[]) value, false, 0, -1, -1, executeNow);
// If the batch has been sent, process the results
if (executeNow) {
sqlEx = tds.getBatchCounts(counts, sqlEx);
// If a serious error then we stop execution now as count
// is too small.
if (sqlEx != null && counts.size() != i) {
break;
}
}
}
return sqlEx;
}
/**
* Execute the SQL batch on a Sybase server.
* <p/>
* For the rare case of CallableStatement batches each statement is executed individually. This ensures that
* problems with the server reading into the middle of a statement are avoided. See bug report [1374518] for more
* details.
*
* @param size the total size of the batch
* @param executeSize the maximum number of statements to send in one request (ignored for this version of the
* method as only one statement will be sent at a time)
* @param counts the returned update counts
* @return chained exceptions linked to a <code>SQLException</code>
* @throws SQLException if a serious error occurs during execution
*/
protected SQLException executeSybaseBatch(int size, int executeSize, ArrayList counts)
throws SQLException
{
if (parameters.length == 0) {
// No parameters so we can execute as a simple batch
return super.executeSybaseBatch(size, executeSize, counts);
}
SQLException sqlEx = null;
for (int i = 0; i < size;) {
Object value = batchValues.get(i);
++i;
tds.executeSQL(sql, procName, (ParamInfo[]) value, false, 0, -1, -1, true);
// If the batch has been sent, process the results
sqlEx = tds.getBatchCounts(counts, sqlEx);
// If a serious error then we stop execution now as count
// is too small.
if (sqlEx != null && counts.size() != i) {
break;
}
}
return sqlEx;
}
// ---------- java.sql.CallableStatement methods follow ----------
public boolean wasNull() throws SQLException {
checkOpen();
return paramWasNull;
}
public byte getByte(int parameterIndex) throws SQLException {
return ((Integer) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.TINYINT, null)).byteValue();
}
public double getDouble(int parameterIndex) throws SQLException {
return ((Double) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.DOUBLE, null)).doubleValue();
}
public float getFloat(int parameterIndex) throws SQLException {
return ((Float) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.REAL, null)).floatValue();
}
public int getInt(int parameterIndex) throws SQLException {
return ((Integer) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.INTEGER, null)).intValue();
}
public long getLong(int parameterIndex) throws SQLException {
return ((Long) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.BIGINT, null)).longValue();
}
public short getShort(int parameterIndex) throws SQLException {
return ((Integer) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.SMALLINT, null)).shortValue();
}
public boolean getBoolean(int parameterIndex) throws SQLException {
return ((Boolean) Support.convert(this, getOutputValue(parameterIndex), BOOLEAN, null)).booleanValue();
}
public byte[] getBytes(int parameterIndex) throws SQLException {
checkOpen();
return ((byte[]) Support.convert(this, getOutputValue(parameterIndex), java.sql.Types.VARBINARY, connection.getCharset()));
}
public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
if (sqlType == java.sql.Types.DECIMAL
|| sqlType == java.sql.Types.NUMERIC) {
registerOutParameter(parameterIndex, sqlType, TdsData.DEFAULT_SCALE);
} else {
registerOutParameter(parameterIndex, sqlType, 0);
}
}
public void registerOutParameter(int parameterIndex, int sqlType, int scale)
throws SQLException {
checkOpen();
if (scale < 0 || scale > connection.getMaxPrecision()) {
throw new SQLException(Messages.get("error.generic.badscale"), "HY092");
}
ParamInfo pi = getParameter(parameterIndex);
pi.isOutput = true;
if ("ERROR".equals(Support.getJdbcTypeName(sqlType))) {
throw new SQLException(Messages.get("error.generic.badtype",
Integer.toString(sqlType)), "HY092");
}
if (sqlType == java.sql.Types.CLOB) {
pi.jdbcType = java.sql.Types.LONGVARCHAR;
} else if (sqlType == java.sql.Types.BLOB) {
pi.jdbcType = java.sql.Types.LONGVARBINARY;
} else {
pi.jdbcType = sqlType;
}
pi.scale = scale;
}
public Object getObject(int parameterIndex) throws SQLException {
Object value = getOutputValue(parameterIndex);
// Don't return UniqueIdentifier objects as the user won't know how to
// handle them
if (value instanceof UniqueIdentifier) {
return value.toString();
}
// If the user requested String/byte[] instead of LOBs, do the conversion
if (!connection.getUseLOBs()) {
value = Support.convertLOB(value);
}
return value;
}
public String getString(int parameterIndex) throws SQLException {
checkOpen();
return (String) Support.convert(this, getOutputValue(parameterIndex),
java.sql.Types.VARCHAR, connection.getCharset());
}
public void registerOutParameter(int parameterIndex, int sqlType, String typeName)
throws SQLException {
notImplemented("CallableStatement.registerOutParameter(int, int, String");
}
public byte getByte(String parameterName) throws SQLException {
return getByte(findParameter(parameterName, false));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -