📄 jtdspreparedstatement.java
字号:
public void addBatch() throws SQLException { checkOpen(); if (batchValues == null) { batchValues = new ArrayList(); } if (parameters.length == 0) { // This is likely to be an error. Batch execution // of a prepared statement with no parameters means // exactly the same SQL will be executed each time! batchValues.add(sql); } else { batchValues.add(parameters); ParamInfo tmp[] = new ParamInfo[parameters.length]; for (int i = 0; i < parameters.length; ++i) { tmp[i] = (ParamInfo) parameters[i].clone(); } parameters = tmp; } } public void clearParameters() throws SQLException { checkOpen(); for (int i = 0; i < parameters.length; i++) { parameters[i].clearInValue(); } } public boolean execute() throws SQLException { checkOpen(); initialize(); boolean useCursor = useCursor(returnKeys, sqlWord); if (procName == null && !(this instanceof JtdsCallableStatement)) { // Sync on the connection to make sure rollback() isn't called // between the moment when the statement is prepared and the moment // when it's executed. synchronized (connection) { String spName = connection.prepareSQL(this, sql, parameters, returnKeys, useCursor); return executeSQL(sql, spName, parameters, returnKeys, false, useCursor); } } else { return executeSQL(sql, procName, parameters, returnKeys, false, useCursor); } } public void setByte(int parameterIndex, byte x) throws SQLException { setParameter(parameterIndex, new Integer((int) (x & 0xFF)), java.sql.Types.TINYINT, 0, 0); } public void setDouble(int parameterIndex, double x) throws SQLException { setParameter(parameterIndex, new Double(x), java.sql.Types.DOUBLE, 0, 0); } public void setFloat(int parameterIndex, float x) throws SQLException { setParameter(parameterIndex, new Float(x), java.sql.Types.REAL, 0, 0); } public void setInt(int parameterIndex, int x) throws SQLException { setParameter(parameterIndex, new Integer(x), java.sql.Types.INTEGER, 0, 0); } public void setNull(int parameterIndex, int sqlType) throws SQLException { if (sqlType == java.sql.Types.CLOB) { sqlType = java.sql.Types.LONGVARCHAR; } else if (sqlType == java.sql.Types.BLOB) { sqlType = java.sql.Types.LONGVARBINARY; } setParameter(parameterIndex, null, sqlType, -1, 0); } public void setLong(int parameterIndex, long x) throws SQLException { setParameter(parameterIndex, new Long(x), java.sql.Types.BIGINT, 0, 0); } public void setShort(int parameterIndex, short x) throws SQLException { setParameter(parameterIndex, new Integer(x), java.sql.Types.SMALLINT, 0, 0); } public void setBoolean(int parameterIndex, boolean x) throws SQLException { setParameter(parameterIndex, x ? Boolean.TRUE : Boolean.FALSE, BOOLEAN, 0, 0); } public void setBytes(int parameterIndex, byte[] x) throws SQLException { setParameter(parameterIndex, x, java.sql.Types.BINARY, 0, 0); } public void setAsciiStream(int parameterIndex, InputStream inputStream, int length) throws SQLException { if (inputStream == null || length < 0) { setParameter(parameterIndex, null, java.sql.Types.LONGVARCHAR, 0, 0); } else { try { setCharacterStream(parameterIndex, new InputStreamReader(inputStream, "US-ASCII"), length); } catch (UnsupportedEncodingException e) { // Should never happen! } } } public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException { checkOpen(); if (x == null || length < 0) { setBytes(parameterIndex, null); } else { setParameter(parameterIndex, x, java.sql.Types.LONGVARBINARY, 0, length); } } public void setUnicodeStream(int parameterIndex, InputStream inputStream, int length) throws SQLException { if (inputStream == null || length < 0) { setString(parameterIndex, null); } else { try { length /= 2; char[] tmp = new char[length]; int pos = 0; int b1 = inputStream.read(); int b2 = inputStream.read(); while (b1 >= 0 && b2 >= 0 && pos < length) { tmp[pos++] = (char) (((b1 << 8) &0xFF00) | (b2 & 0xFF)); b1 = inputStream.read(); b2 = inputStream.read(); } setString(parameterIndex, new String(tmp, 0, pos)); } catch (java.io.IOException e) { throw new SQLException(Messages.get("error.generic.ioerror", e.getMessage()), "HY000"); } } } public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException { if (reader == null || length < 0) { setParameter(parameterIndex, null, java.sql.Types.LONGVARCHAR, 0, 0); } else { setParameter(parameterIndex, reader, java.sql.Types.LONGVARCHAR, 0, length); } } public void setObject(int parameterIndex, Object x) throws SQLException { setObjectBase(parameterIndex, x, Support.getJdbcType(x), -1); } public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException { setObjectBase(parameterIndex, x, targetSqlType, -1); } public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException { if (scale < 0 || scale > 28) { throw new SQLException(Messages.get("error.generic.badscale"), "HY092"); } setObjectBase(parameterIndex, x, targetSqlType, scale); } public void setNull(int parameterIndex, int sqlType, String typeName) throws SQLException { notImplemented("PreparedStatement.setNull(int, int, String)"); } public void setString(int parameterIndex, String x) throws SQLException { setParameter(parameterIndex, x, java.sql.Types.VARCHAR, 0, 0); } public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException { setParameter(parameterIndex, x, java.sql.Types.DECIMAL, -1, 0); } public void setURL(int parameterIndex, URL url) throws SQLException { setString(parameterIndex, (url == null)? null: url.toString()); } public void setArray(int arg0, Array arg1) throws SQLException { notImplemented("PreparedStatement.setArray"); } public void setBlob(int parameterIndex, Blob x) throws SQLException { if (x == null) { setBytes(parameterIndex, null); } else { long length = x.length(); if (length > Integer.MAX_VALUE) { throw new SQLException(Messages.get("error.resultset.longblob"), "24000"); } setBinaryStream(parameterIndex, x.getBinaryStream(), (int) x.length()); } } public void setClob(int parameterIndex, Clob x) throws SQLException { if (x == null) { this.setString(parameterIndex, null); } else { long length = x.length(); if (length > Integer.MAX_VALUE) { throw new SQLException(Messages.get("error.resultset.longclob"), "24000"); } setCharacterStream(parameterIndex, x.getCharacterStream(), (int) x.length()); } } public void setDate(int parameterIndex, Date x) throws SQLException { setParameter(parameterIndex, x, java.sql.Types.DATE, 0, 0); } public ParameterMetaData getParameterMetaData() throws SQLException { checkOpen(); // // NB. This is usable only with the JDBC3 version of the interface. // if (connection.getServerType() == Driver.SYBASE) { // Sybase does return the parameter types for prepared sql. connection.prepareSQL(this, sql, new ParamInfo[0], false, false); } try { Class pmdClass = Class.forName("net.sourceforge.jtds.jdbc.ParameterMetaDataImpl"); Class[] parameterTypes = new Class[] {ParamInfo[].class}; Object[] arguments = new Object[] {parameters}; Constructor pmdConstructor = pmdClass.getConstructor(parameterTypes); return (ParameterMetaData) pmdConstructor.newInstance(arguments); } catch (Exception e) { notImplemented("PreparedStatement.getParameterMetaData"); } return null; } public void setRef(int parameterIndex, Ref x) throws SQLException { notImplemented("PreparedStatement.setRef"); } public ResultSet executeQuery() throws SQLException { checkOpen(); initialize(); boolean useCursor = useCursor(false, null); if (procName == null && !(this instanceof JtdsCallableStatement)) { // Sync on the connection to make sure rollback() isn't called // between the moment when the statement is prepared and the moment // when it's executed. synchronized (connection) { String spName = connection.prepareSQL(this, sql, parameters, false, useCursor); return executeSQLQuery(sql, spName, parameters, useCursor); } } else { return executeSQLQuery(sql, procName, parameters, useCursor); } } public ResultSetMetaData getMetaData() throws SQLException { checkOpen(); if (colMetaData == null) { if (currentResult != null) { colMetaData = currentResult.columns; } else if (connection.getServerType() == Driver.SYBASE) { // Sybase can provide meta data as a by product of preparing the call. connection.prepareSQL(this, sql, new ParamInfo[0], false, false); if (colMetaData == null) { return null; // Sorry still no go } } else { // For Microsoft set all parameters to null and execute the query. // SET FMTONLY ON asks the server just to return meta data. // This only works for select statements if (!sqlWord.equals("select")) { return null; } // Copy parameters to avoid corrupting any values already set // by the user as we need to set a flag and null out the data. ParamInfo[] params = new ParamInfo[parameters.length]; for (int i = 0; i < params.length; i++) { params[i] = new ParamInfo(parameters[i].markerPos, false); params[i].isSet = true; } // Substitute nulls into SQL String StringBuffer testSql = new StringBuffer(sql.length() + 128); testSql.append("SET FMTONLY ON "); testSql.append( Support.substituteParameters(sql, params, connection.getTdsVersion())); testSql.append(" SET FMTONLY OFF"); try { tds.submitSQL(testSql.toString()); colMetaData = tds.getColumns(); } catch (SQLException e) { // Ensure FMTONLY is switched off! tds.submitSQL("SET FMTONLY OFF"); return null; } } } return new JtdsResultSetMetaData(colMetaData, JtdsResultSet.getColumnCount(colMetaData), connection.getUseLOBs()); } public void setTime(int parameterIndex, Time x) throws SQLException { setParameter( parameterIndex, x, java.sql.Types.TIME, 0, 0 ); } public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException { setParameter(parameterIndex, x, java.sql.Types.TIMESTAMP, 0, 0); } public void setDate(int parameterIndex, Date x, Calendar cal) throws SQLException { if (x != null && cal != null) { x = new java.sql.Date(Support.timeFromZone(x, cal)); } setDate(parameterIndex, x); } public void setTime(int parameterIndex, Time x, Calendar cal) throws SQLException { if (x != null && cal != null) { x = new Time(Support.timeFromZone(x, cal)); } setTime(parameterIndex, x); } public void setTimestamp(int parameterIndex, Timestamp x, Calendar cal) throws SQLException { if (x != null && cal != null) { x = new java.sql.Timestamp(Support.timeFromZone(x, cal)); } setTimestamp(parameterIndex, x); } public int executeUpdate(String sql) throws SQLException { notSupported("executeUpdate(String)"); return 0; } public void addBatch(String sql) throws SQLException { notSupported("executeBatch(String)"); } public boolean execute(String sql) throws SQLException { notSupported("execute(String)"); return false; } public int executeUpdate(String sql, int getKeys) throws SQLException { notSupported("executeUpdate(String, int)"); return 0; } public boolean execute(String arg0, int arg1) throws SQLException { notSupported("execute(String, int)"); return false; } public int executeUpdate(String arg0, int[] arg1) throws SQLException { notSupported("executeUpdate(String, int[])"); return 0; } public boolean execute(String arg0, int[] arg1) throws SQLException { notSupported("execute(String, int[])"); return false; } public int executeUpdate(String arg0, String[] arg1) throws SQLException { notSupported("executeUpdate(String, String[])"); return 0; } public boolean execute(String arg0, String[] arg1) throws SQLException { notSupported("execute(String, String[])"); return false; } public ResultSet executeQuery(String sql) throws SQLException { notSupported("executeQuery(String)"); return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -