【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-CallableStatement.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
/*
Copyright (C) 2002-2004 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
There are special exceptions to the terms and conditions of the GPL
as it is applied to this software. View the full text of the
exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
software distribution.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.mysql.jdbc;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.Ref;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
/**
* Representation of stored procedures for JDBC
*
* @author Mark Matthews
* @version $Id: CallableStatement.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
* Exp $
*/
public class CallableStatement extends PreparedStatement implements
java.sql.CallableStatement {
class CallableStatementParam {
int desiredJdbcType;
int index;
int inOutModifier;
boolean isIn;
boolean isOut;
int jdbcType;
short nullability;
String paramName;
int precision;
int scale;
String typeName;
CallableStatementParam(String name, int idx, boolean in,
boolean out, int jdbcType, String typeName,
int precision, int scale, short nullability,
int inOutModifier) {
this.paramName = name;
this.isIn = in;
this.isOut = out;
this.index = idx;
this.jdbcType = jdbcType;
this.typeName = typeName;
this.precision = precision;
this.scale = scale;
this.nullability = nullability;
this.inOutModifier = inOutModifier;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class CallableStatementParamInfo {
String catalogInUse;
boolean isFunctionCall;
String nativeSql;
int numParameters;
List parameterList;
Map parameterMap;
CallableStatementParamInfo(java.sql.ResultSet paramTypesRs)
throws SQLException {
boolean hadRows = paramTypesRs.last();
this.nativeSql = originalSql;
this.catalogInUse = currentCatalog;
isFunctionCall = callingStoredFunction;
if (hadRows) {
this.numParameters = paramTypesRs.getRow();
this.parameterList = new ArrayList(this.numParameters);
this.parameterMap = new HashMap(this.numParameters);
paramTypesRs.beforeFirst();
addParametersFromDBMD(paramTypesRs);
} else {
this.numParameters = 0;
}
}
private void addParametersFromDBMD(java.sql.ResultSet paramTypesRs)
throws SQLException {
int i = 0;
if (isFunctionCall) {
// first row will be return value parameter
paramTypesRs.next();
}
while (paramTypesRs.next()) {
String paramName = paramTypesRs.getString(4);
int inOutModifier = paramTypesRs.getInt(5);
boolean isOutParameter = false;
boolean isInParameter = false;
if (inOutModifier == DatabaseMetaData.procedureColumnInOut) {
isOutParameter = true;
isInParameter = true;
} else if (inOutModifier == DatabaseMetaData.procedureColumnIn) {
isOutParameter = false;
isInParameter = true;
} else if (inOutModifier == DatabaseMetaData.procedureColumnOut) {
isOutParameter = true;
isInParameter = false;
}
int jdbcType = paramTypesRs.getInt(6);
String typeName = paramTypesRs.getString(7);
int precision = paramTypesRs.getInt(8);
int scale = paramTypesRs.getInt(10);
short nullability = paramTypesRs.getShort(12);
CallableStatementParam paramInfoToAdd = new CallableStatementParam(
paramName, i++, isInParameter, isOutParameter,
jdbcType, typeName, precision, scale, nullability,
inOutModifier);
this.parameterList.add(paramInfoToAdd);
this.parameterMap.put(paramName, paramInfoToAdd);
}
}
protected void checkBounds(int paramIndex) throws SQLException {
int localParamIndex = paramIndex - 1;
if ((paramIndex < 0) || (localParamIndex >= this.numParameters)) {
throw new SQLException (
Messages.getString("CallableStatement.11") + paramIndex //$NON-NLS-1$
+ Messages.getString("CallableStatement.12") + numParameters //$NON-NLS-1$
+ Messages.getString("CallableStatement.13"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$
}
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#clone()
*/
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
CallableStatementParam getParameter(int index) {
return (CallableStatementParam) this.parameterList.get(index);
}
CallableStatementParam getParameter(String name) {
return (CallableStatementParam) this.parameterMap.get(name);
}
public String getParameterClassName(int arg0) throws SQLException {
String mysqlTypeName = getParameterTypeName(arg0);
boolean isBinaryOrBlob = StringUtils.indexOfIgnoreCase(mysqlTypeName, "BLOB") != -1 ||
StringUtils.indexOfIgnoreCase(mysqlTypeName, "BINARY") != -1;
boolean isUnsigned = StringUtils.indexOfIgnoreCase(mysqlTypeName, "UNSIGNED") != -1;
int mysqlTypeIfKnown = 0;
if (StringUtils.startsWithIgnoreCase(mysqlTypeName, "MEDIUMINT")) {
mysqlTypeIfKnown = MysqlDefs.FIELD_TYPE_INT24;
}
return ResultSetMetaData.getClassNameForJavaType(getParameterType(arg0),
isUnsigned, mysqlTypeIfKnown, isBinaryOrBlob, false);
}
public int getParameterCount() throws SQLException {
return this.parameterList.size();
}
public int getParameterMode(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).inOutModifier;
}
public int getParameterType(int arg0) throws SQLException {
checkBounds(arg0);
return getParameter(arg0 - 1).jdbcType;
}
public String getParameterTypeName(int arg0) throws SQLException {
checkBounds(arg0);
r
上一篇:
CallableAndFuture.java
下一篇:
Teacher''s role and autonomic learning