📄 preparedstatement.java
字号:
} protected void setBytes(int parameterIndex, byte[] x, boolean checkForIntroducer, boolean escapeForMBChars) throws SQLException { if (x == null) { setNull(parameterIndex, java.sql.Types.BINARY); } else { String connectionEncoding = this.connection.getEncoding(); if (escapeForMBChars && this.connection.getUseUnicode() && connectionEncoding != null && CharsetMapping.MULTIBYTE_CHARSETS.get( connectionEncoding.toLowerCase(Locale.ENGLISH)) != null && !this.connection.parserKnowsUnicode()) { // Send as hex ByteArrayOutputStream bOut = new ByteArrayOutputStream((x.length * 2) + 3); bOut.write('x'); bOut.write('\''); for (int i = 0; i < x.length; i++) { int lowBits = (x[i] & 0xff) / 16; int highBits = (x[i] & 0xff) % 16; bOut.write(HEX_DIGITS[lowBits]); bOut.write(HEX_DIGITS[highBits]); } bOut.write('\''); setInternal(parameterIndex, bOut.toByteArray()); return; } // escape them int numBytes = x.length; int pad = 2; 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()); } } /** * DOCUMENT ME! * * @param parameterIndex DOCUMENT ME! * * @return DOCUMENT ME! * * @throws SQLException DOCUMENT ME! */ public byte[] getBytesRepresentation(int parameterIndex) throws SQLException { if (this.isStream[parameterIndex]) { return streamToBytes(this.parameterStreams[parameterIndex], false, this.streamLengths[parameterIndex], this.connection.getUseStreamLengthsInPrepStmts()); } byte[] parameterVal = this.parameterValues[parameterIndex]; if (parameterVal == null) { return null; } if ((parameterVal[0] == '\'') && (parameterVal[parameterVal.length - 1] == '\'')) { byte[] valNoQuotes = new byte[parameterVal.length - 2]; System.arraycopy(parameterVal, 1, valNoQuotes, 0, parameterVal.length - 2); return valNoQuotes; } return parameterVal; } /** * 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 { 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''"); //$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))); } /* (non-Javadoc) * @see java.sql.Statement#getGeneratedKeys() */ public synchronized java.sql.ResultSet getGeneratedKeys() throws SQLException { if (this.batchedGeneratedKeys == null) { return super.getGeneratedKeys(); } Field[] fields = new Field[1]; fields[0] = new Field("", "GENERATED_KEY", Types.BIGINT, 17); //$NON-NLS-1$ //$NON-NLS-2$ return new com.mysql.jdbc.ResultSet(this.currentCatalog, fields, new RowDataStatic(this.batchedGeneratedKeys), this.connection, this); } /** * 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)); } /** * 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)); } /** * The number, types and properties of a ResultSet's columns are provided * by the getMetaData method. * * @return the description of a ResultSet's columns * * @exception SQLException if a database-access error occurs. */ public synchronized java.sql.ResultSetMetaData getMetaData() throws SQLException { PreparedStatement mdStmt = null; java.sql.ResultSet mdRs = null; if (this.pstmtResultMetaData == null) { try { mdStmt = new PreparedStatement(this.connection, this.originalSql, this.currentCatalog, this.parseInfo); mdStmt.setMaxRows(0); int paramCount = this.parameterValues.length; for (int i = 1; i <= paramCount; i++) { mdStmt.setString(i, ""); //$NON-NLS-1$ } boolean hadResults = mdStmt.execute(); if (hadResults) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -