📄 preparedstatement.java
字号:
boolean needsIntroducer = checkForIntroducer && this.connection.versionMeetsMinimum(4, 1, 0); if (needsIntroducer) { pad += 7; } ByteArrayOutputStream bOut = new ByteArrayOutputStream(numBytes + pad); if (needsIntroducer) { bOut.write('_'); bOut.write('b'); bOut.write('i'); bOut.write('n'); bOut.write('a'); bOut.write('r'); bOut.write('y'); } bOut.write('\''); for (int i = 0; i < numBytes; ++i) { byte b = x[i]; switch (b) { case 0: /* Must be escaped for 'mysql' */ bOut.write('\\'); bOut.write('0'); break; case '\n': /* Must be escaped for logs */ bOut.write('\\'); bOut.write('n'); break; case '\r': bOut.write('\\'); bOut.write('r'); break; case '\\': bOut.write('\\'); bOut.write('\\'); break; case '\'': bOut.write('\\'); bOut.write('\''); break; case '"': /* Better safe than sorry */ bOut.write('\\'); bOut.write('"'); break; case '\032': /* This gives problems on Win32 */ bOut.write('\\'); bOut.write('Z'); break; default: bOut.write(b); } } bOut.write('\''); setInternal(parameterIndex, bOut.toByteArray()); } } /** * Used by updatable result sets for refreshRow() because the parameter has * already been escaped for updater or inserter prepared statements. * * @param parameterIndex * the parameter to set. * @param parameterAsBytes * the parameter as a string. * * @throws SQLException * if an error occurs */ protected void setBytesNoEscape(int parameterIndex, byte[] parameterAsBytes) throws SQLException { byte[] parameterWithQuotes = new byte[parameterAsBytes.length + 2]; parameterWithQuotes[0] = '\''; System.arraycopy(parameterAsBytes, 0, parameterWithQuotes, 1, parameterAsBytes.length); parameterWithQuotes[parameterAsBytes.length + 1] = '\''; setInternal(parameterIndex, parameterWithQuotes); } protected void setBytesNoEscapeNoQuotes(int parameterIndex, byte[] parameterAsBytes) throws SQLException { setInternal(parameterIndex, parameterAsBytes); } /** * JDBC 2.0 When a very large UNICODE value is input to a LONGVARCHAR * parameter, it may be more practical to send it via a java.io.Reader. JDBC * will read the data from the stream as needed, until it reaches * end-of-file. The JDBC driver will do any necessary conversion from * UNICODE to the database char format. * * <P> * <B>Note:</B> This stream object can either be a standard Java stream * object or your own subclass that implements the standard interface. * </p> * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param reader * the java reader which contains the UNICODE data * @param length * the number of characters in the stream * * @exception SQLException * if a database-access error occurs. */ public void setCharacterStream(int parameterIndex, java.io.Reader reader, int length) throws SQLException { try { if (reader == null) { setNull(parameterIndex, Types.LONGVARCHAR); } else { char[] c = null; int len = 0; boolean useLength = this.connection .getUseStreamLengthsInPrepStmts(); if (useLength && (length != -1)) { c = new char[length]; int numCharsRead = readFully(reader, c, length); // blocks // until // all // read setString(parameterIndex, new String(c, 0, numCharsRead)); } else { c = new char[4096]; StringBuffer buf = new StringBuffer(); while ((len = reader.read(c)) != -1) { buf.append(c, 0, len); } setString(parameterIndex, buf.toString()); } } } catch (java.io.IOException ioEx) { throw new SQLException(ioEx.toString(), SQLError.SQL_STATE_GENERAL_ERROR); } } /** * JDBC 2.0 Set a CLOB parameter. * * @param i * the first parameter is 1, the second is 2, ... * @param x * an object representing a CLOB * * @throws SQLException * if a database error occurs */ public void setClob(int i, Clob x) throws SQLException { if (x == null) { setNull(i, Types.CLOB); return; } setString(i, x.getSubString(1L, (int) x.length())); } /** * Set a parameter to a java.sql.Date value. The driver converts this to a * SQL DATE value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception java.sql.SQLException * if a database access error occurs */ public void setDate(int parameterIndex, java.sql.Date x) throws java.sql.SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.DATE); } else { // FIXME: Have instance version of this, problem as it's // not thread-safe :( SimpleDateFormat dateFormatter = new SimpleDateFormat( "''yyyy-MM-dd''", Locale.US); //$NON-NLS-1$ setInternal(parameterIndex, dateFormatter.format(x)); } } /** * Set a parameter to a java.sql.Date value. The driver converts this to a * SQL DATE value when it sends it to the database. * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param x * the parameter value * @param cal * the calendar to interpret the date with * * @exception SQLException * if a database-access error occurs. */ public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException { setDate(parameterIndex, x); } /** * Set a parameter to a Java double value. The driver converts this to a SQL * DOUBLE value when it sends it to the database * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception SQLException * if a database access error occurs */ public void setDouble(int parameterIndex, double x) throws SQLException { if (!this.connection.getAllowNanAndInf() && (x == Double.POSITIVE_INFINITY || x == Double.NEGATIVE_INFINITY || Double.isNaN(x))) { throw new SQLException("'" + x + "' is not a valid numeric or approximate numeric value", SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } setInternal(parameterIndex, StringUtils.fixDecimalExponent(String .valueOf(x))); } /** * Set a parameter to a Java float value. The driver converts this to a SQL * FLOAT value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception SQLException * if a database access error occurs */ public void setFloat(int parameterIndex, float x) throws SQLException { setInternal(parameterIndex, StringUtils.fixDecimalExponent(String .valueOf(x))); } /** * Set a parameter to a Java int value. The driver converts this to a SQL * INTEGER value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception SQLException * if a database access error occurs */ public void setInt(int parameterIndex, int x) throws SQLException { setInternal(parameterIndex, String.valueOf(x)); } private final void setInternal(int paramIndex, byte[] val) throws SQLException { if (this.isClosed) { throw new SQLException(Messages.getString("PreparedStatement.48"), //$NON-NLS-1$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } if ((paramIndex < 1)) { throw new SQLException( Messages.getString("PreparedStatement.49") //$NON-NLS-1$ + paramIndex + Messages.getString("PreparedStatement.50"), SQLError.SQL_STATE_ILLEGAL_ARGUMENT); //$NON-NLS-1$ } else if (paramIndex > this.parameterCount) { throw new SQLException( Messages.getString("PreparedStatement.51") //$NON-NLS-1$ + paramIndex + Messages.getString("PreparedStatement.52") + (this.parameterValues.length) + Messages.getString("PreparedStatement.53"), //$NON-NLS-1$ //$NON-NLS-2$ SQLError.SQL_STATE_ILLEGAL_ARGUMENT); } this.isStream[paramIndex - 1] = false; this.isNull[paramIndex - 1] = false; this.parameterStreams[paramIndex - 1] = null; this.parameterValues[paramIndex - 1] = val; } private final void setInternal(int paramIndex, String val) throws SQLException { byte[] parameterAsBytes = null; if (this.charConverter != null) { parameterAsBytes = this.charConverter.toBytes(val); } else { parameterAsBytes = StringUtils.getBytes(val, this.charConverter, this.charEncoding, this.connection .getServerCharacterEncoding(), this.connection .parserKnowsUnicode()); } setInternal(paramIndex, parameterAsBytes); } /** * Set a parameter to a Java long value. The driver converts this to a SQL * BIGINT value when it sends it to the database. * * @param parameterIndex * the first parameter is 1... * @param x * the parameter value * * @exception SQLException * if a database access error occurs */ public void setLong(int parameterIndex, long x) throws SQLException { setInternal(parameterIndex, String.valueOf(x)); } /** * Set a parameter to SQL NULL * * <p> * <B>Note:</B> You must specify the parameters SQL type (although MySQL * ignores it) * </p> * * @param parameterIndex * the first parameter is 1, etc... * @param sqlType * the SQL type code defined in java.sql.Types * * @exception SQLException * if a database access error occurs */ public void setNull(int parameterIndex, int sqlType) throws SQLException { setInternal(parameterIndex, "null"); //$NON-NLS-1$ this.isNull[parameterIndex - 1] = true; } /** * Set a parameter to SQL NULL. * * <P> * <B>Note:</B> You must specify the parameter's SQL type. * </p> * * @param parameterIndex * the first parameter is 1, the second is 2, ... * @param sqlType * SQL type code defined by java.sql.Types * @param arg * argument parameters for null * * @exception SQLException * if a database-access error occurs. */ public void setNull(int parameterIndex, int sqlType, String arg) throws SQLException { setNull(parameterIndex, sqlType); } private void setNumericObject(int parameterIndex, Object parameterObj, int targetSqlType, int scale) throws SQLException { Number parameterAsNum; if (parameterObj instanceof Boolean) { parameterAsNum = ((Boolean) parameterObj) .booleanValue() ? new Integer(1) : new Integer( 0); } else if (parameterObj instanceof String) { switch (targetSqlType) { case Types.BIT: boolean parameterAsBoolean = "true" .equalsIgnoreCase((String) parameterObj); parameterAsNum = parameterAsBoolean ? new Integer(1) : new Integer(0); break; case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: parameterAsNum = Integer .valueOf((String) parameterObj); break; case Types.BIGINT: parameterAsNum = Long .valueOf((String) parameterObj); break; case Types.REAL: parameterAsNum = Float .valueOf((String) parameterObj); break; case Types.FLOAT: case Types.DOUBLE: parameterAsNum = Double .valueOf((String) parameterObj); break; case Types.DECIMAL: case Types.NUMERIC: default: parameterAsNum = new java.math.BigDecimal( (String) parameterObj); } } else { parameterAsNum = (Number) parameterObj; } switch (targetSqlType) { case Types.BIT: case Types.TINYINT: case Types.SMALLINT: case Types.INTEGER: setInt(parameterIndex, parameterAsNum.intValue()); break; case Types.BIGINT: setLong(parameterIndex, parameterAsNum.longValue()); break; case Types.REAL: setFloat(parameterIndex, parameterAsNum.floatValue()); break; case Types.FLOAT: case Types.DOUBLE: setDouble(parameterIndex, parameterAsNum.doubleValue()); break; case Types.DECIMAL: case Types.NUMERIC: if (parameterAsNum instanceof java.math.BigDecimal) { BigDecimal scaledBigDecimal = null; try { scaledBigDecimal = ((java.math.BigDecimal) parameterAsNum) .setScale(scale);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -