/EQEmuJSM/mysql-connector-java-5.1.13/src/testsuite/regression/StatementRegressionTest.java
Java | 2317 lines | 1484 code | 423 blank | 410 comment | 147 complexity | 389c787fb48d8438c1c1a908618962de MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0
Large files files are truncated, but you can click here to view the full file
- /*
- Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
-
- All rights reserved. Use is subject to license terms.
- The MySQL Connector/J is licensed under the terms of the GPL,
- like most MySQL Connectors. There are special exceptions to the
- terms and conditions of the GPL as it is applied to this software,
- see the FLOSS License Exception available on mysql.com.
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; version 2 of the
- License.
- 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., 51 Franklin St, Fifth Floor, Boston, MA
- 02110-1301 USA
- */
- package testsuite.regression;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.CharArrayReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.PrintStream;
- import java.io.Reader;
- import java.io.StringReader;
- import java.io.UnsupportedEncodingException;
- import java.io.Writer;
- import java.math.BigDecimal;
- import java.math.BigInteger;
- import java.net.URL;
- import java.sql.Array;
- import java.sql.BatchUpdateException;
- import java.sql.Blob;
- import java.sql.CallableStatement;
- import java.sql.Clob;
- import java.sql.Connection;
- import java.sql.DataTruncation;
- import java.sql.Date;
- import java.sql.PreparedStatement;
- import java.sql.Ref;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.SQLWarning;
- import java.sql.Statement;
- import java.sql.Time;
- import java.sql.Timestamp;
- import java.sql.Types;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Properties;
- import java.util.TimeZone;
- import testsuite.BaseTestCase;
- import testsuite.UnreliableSocketFactory;
- import com.mysql.jdbc.CachedResultSetMetaData;
- import com.mysql.jdbc.Field;
- import com.mysql.jdbc.NonRegisteringDriver;
- import com.mysql.jdbc.ParameterBindings;
- import com.mysql.jdbc.ResultSetInternalMethods;
- import com.mysql.jdbc.SQLError;
- import com.mysql.jdbc.ServerPreparedStatement;
- import com.mysql.jdbc.StatementImpl;
- import com.mysql.jdbc.StatementInterceptor;
- import com.mysql.jdbc.StatementInterceptorV2;
- import com.mysql.jdbc.exceptions.MySQLStatementCancelledException;
- import com.mysql.jdbc.exceptions.MySQLTimeoutException;
- /**
- * Regression tests for the Statement class
- *
- * @author Mark Matthews
- */
- public class StatementRegressionTest extends BaseTestCase {
- class PrepareThread extends Thread {
- Connection c;
- PrepareThread(Connection cn) {
- this.c = cn;
- }
- public void run() {
- for (int i = 0; i < 20; i++) // force this to end eventually
- {
- try {
- this.c.prepareStatement("SELECT 1");
- StatementRegressionTest.this.testServerPrepStmtDeadlockCounter++;
- Thread.sleep(400);
- } catch (SQLException sqlEx) {
- throw new RuntimeException(sqlEx);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- static int count = 0;
- static int nextID = 1; // The next ID we expected to generate
- /*
- * Each row in this table is to be converted into a single REPLACE
- * statement. If the value is zero, a new record is to be created using then
- * autoincrement feature. If the value is non-zero, the existing row of that
- * value is to be replace with, obviously, the same key. I expect one
- * Generated Key for each zero value - but I would accept one key for each
- * value, with non-zero values coming back as themselves.
- */
- static final int[][] tests = { { 0 }, // generate 1
- { 1, 0, 0 }, // update 1, generate 2, 3
- { 2, 0, 0, }, // update 2, generate 3, 4
- };
- /**
- * Runs all test cases in this test suite
- *
- * @param args
- */
- public static void main(String[] args) {
- junit.textui.TestRunner.run(StatementRegressionTest.class);
- }
- private int testServerPrepStmtDeadlockCounter = 0;
- /**
- * Constructor for StatementRegressionTest.
- *
- * @param name
- * the name of the test to run
- */
- public StatementRegressionTest(String name) {
- super(name);
- }
- private void addBatchItems(Statement statement, PreparedStatement pStmt,
- String tableName, int i) throws SQLException {
- pStmt.setString(1, "ps_batch_" + i);
- pStmt.setString(2, "ps_batch_" + i);
- pStmt.addBatch();
- statement.addBatch("INSERT INTO " + tableName
- + " (strdata1, strdata2) VALUES " + "(\"s_batch_" + i
- + "\",\"s_batch_" + i + "\")");
- }
- private void createGGKTables() throws Exception {
- // Delete and recreate table
- dropGGKTables();
- this.stmt.executeUpdate("CREATE TABLE testggk ("
- + "id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,"
- + "val INT NOT NULL" + ")");
- }
- private void doGGKTestPreparedStatement(int[] values, boolean useUpdate)
- throws Exception {
- // Generate the the multiple replace command
- StringBuffer cmd = new StringBuffer("REPLACE INTO testggk VALUES ");
- int newKeys = 0;
- for (int i = 0; i < values.length; i++) {
- cmd.append("(");
- if (values[i] == 0) {
- cmd.append("NULL");
- newKeys += 1;
- } else {
- cmd.append(values[i]);
- }
- cmd.append(", ");
- cmd.append(count++);
- cmd.append("), ");
- }
- cmd.setLength(cmd.length() - 2); // trim the final ", "
- // execute and print it
- System.out.println(cmd.toString());
- PreparedStatement pStmt = this.conn.prepareStatement(cmd.toString(),
- Statement.RETURN_GENERATED_KEYS);
- if (useUpdate) {
- pStmt.executeUpdate();
- } else {
- pStmt.execute();
- }
- // print out what actually happened
- System.out.println("Expect " + newKeys
- + " generated keys, starting from " + nextID);
- this.rs = pStmt.getGeneratedKeys();
- StringBuffer res = new StringBuffer("Got keys");
- int[] generatedKeys = new int[newKeys];
- int i = 0;
- while (this.rs.next()) {
- if (i < generatedKeys.length) {
- generatedKeys[i] = this.rs.getInt(1);
- }
- i++;
- res.append(" " + this.rs.getInt(1));
- }
- int numberOfGeneratedKeys = i;
- assertTrue(
- "Didn't retrieve expected number of generated keys, expected "
- + newKeys + ", found " + numberOfGeneratedKeys,
- numberOfGeneratedKeys == newKeys);
- assertTrue("Keys didn't start with correct sequence: ",
- generatedKeys[0] == nextID);
- System.out.println(res.toString());
- // Read and print the new state of the table
- this.rs = this.stmt.executeQuery("SELECT id, val FROM testggk");
- System.out.println("New table contents ");
- while (this.rs.next())
- System.out.println("Id " + this.rs.getString(1) + " val "
- + this.rs.getString(2));
- // Tidy up
- System.out.println("");
- nextID += newKeys;
- }
- private void doGGKTestStatement(int[] values, boolean useUpdate)
- throws Exception {
- // Generate the the multiple replace command
- StringBuffer cmd = new StringBuffer("REPLACE INTO testggk VALUES ");
- int newKeys = 0;
- for (int i = 0; i < values.length; i++) {
- cmd.append("(");
- if (values[i] == 0) {
- cmd.append("NULL");
- newKeys += 1;
- } else {
- cmd.append(values[i]);
- }
- cmd.append(", ");
- cmd.append(count++);
- cmd.append("), ");
- }
- cmd.setLength(cmd.length() - 2); // trim the final ", "
- // execute and print it
- System.out.println(cmd.toString());
- if (useUpdate) {
- this.stmt.executeUpdate(cmd.toString(),
- Statement.RETURN_GENERATED_KEYS);
- } else {
- this.stmt.execute(cmd.toString(), Statement.RETURN_GENERATED_KEYS);
- }
- // print out what actually happened
- System.out.println("Expect " + newKeys
- + " generated keys, starting from " + nextID);
- this.rs = this.stmt.getGeneratedKeys();
- StringBuffer res = new StringBuffer("Got keys");
- int[] generatedKeys = new int[newKeys];
- int i = 0;
- while (this.rs.next()) {
- if (i < generatedKeys.length) {
- generatedKeys[i] = this.rs.getInt(1);
- }
- i++;
- res.append(" " + this.rs.getInt(1));
- }
- int numberOfGeneratedKeys = i;
- assertTrue(
- "Didn't retrieve expected number of generated keys, expected "
- + newKeys + ", found " + numberOfGeneratedKeys,
- numberOfGeneratedKeys == newKeys);
- assertTrue("Keys didn't start with correct sequence: ",
- generatedKeys[0] == nextID);
- System.out.println(res.toString());
- // Read and print the new state of the table
- this.rs = this.stmt.executeQuery("SELECT id, val FROM testggk");
- System.out.println("New table contents ");
- while (this.rs.next())
- System.out.println("Id " + this.rs.getString(1) + " val "
- + this.rs.getString(2));
- // Tidy up
- System.out.println("");
- nextID += newKeys;
- }
- private void dropGGKTables() throws Exception {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testggk");
- }
- /**
- * @param pStmt
- * @param catId
- * @throws SQLException
- */
- private void execQueryBug5191(PreparedStatement pStmt, int catId)
- throws SQLException {
- pStmt.setInt(1, catId);
- this.rs = pStmt.executeQuery();
- assertTrue(this.rs.next());
- assertTrue(this.rs.next());
- // assertTrue(rs.next());
- assertFalse(this.rs.next());
- }
- private String getByteArrayString(byte[] ba) {
- StringBuffer buffer = new StringBuffer();
- if (ba != null) {
- for (int i = 0; i < ba.length; i++) {
- buffer.append("0x" + Integer.toHexString(ba[i] & 0xff) + " ");
- }
- } else {
- buffer.append("null");
- }
- return buffer.toString();
- }
- /**
- * @param continueBatchOnError
- * @throws SQLException
- */
- private void innerBug6823(boolean continueBatchOnError) throws SQLException {
- Properties continueBatchOnErrorProps = new Properties();
- continueBatchOnErrorProps.setProperty("continueBatchOnError", String
- .valueOf(continueBatchOnError));
- this.conn = getConnectionWithProps(continueBatchOnErrorProps);
- Statement statement = this.conn.createStatement();
- String tableName = "testBug6823";
- createTable(tableName, "(id int not null primary key auto_increment,"
- + " strdata1 varchar(255) not null, strdata2 varchar(255),"
- + " UNIQUE INDEX (strdata1(100)))");
- PreparedStatement pStmt = this.conn.prepareStatement("INSERT INTO "
- + tableName + " (strdata1, strdata2) VALUES (?,?)");
- int c = 0;
- addBatchItems(statement, pStmt, tableName, ++c);
- addBatchItems(statement, pStmt, tableName, ++c);
- addBatchItems(statement, pStmt, tableName, ++c);
- addBatchItems(statement, pStmt, tableName, c); // duplicate entry
- addBatchItems(statement, pStmt, tableName, ++c);
- addBatchItems(statement, pStmt, tableName, ++c);
- int expectedUpdateCounts = continueBatchOnError ? 6 : 3;
- BatchUpdateException e1 = null;
- BatchUpdateException e2 = null;
- int[] updateCountsPstmt = null;
- try {
- updateCountsPstmt = pStmt.executeBatch();
- } catch (BatchUpdateException e) {
- e1 = e;
- updateCountsPstmt = e1.getUpdateCounts();
- }
- int[] updateCountsStmt = null;
- try {
- updateCountsStmt = statement.executeBatch();
- } catch (BatchUpdateException e) {
- e2 = e;
- updateCountsStmt = e1.getUpdateCounts();
- }
- assertNotNull(e1);
- assertNotNull(e2);
- assertEquals(expectedUpdateCounts, updateCountsPstmt.length);
- assertEquals(expectedUpdateCounts, updateCountsStmt.length);
- if (continueBatchOnError) {
- assertTrue(updateCountsPstmt[3] == Statement.EXECUTE_FAILED);
- assertTrue(updateCountsStmt[3] == Statement.EXECUTE_FAILED);
- }
- int psRows = 0;
- this.rs = this.stmt.executeQuery("SELECT * from " + tableName
- + " WHERE strdata1 like \"ps_%\"");
- while (this.rs.next()) {
- psRows++;
- }
- assertTrue(psRows > 0);
- int sRows = 0;
- this.rs = this.stmt.executeQuery("SELECT * from " + tableName
- + " WHERE strdata1 like \"s_%\"");
- while (this.rs.next()) {
- sRows++;
- }
- assertTrue(sRows > 0);
- assertTrue(psRows + "!=" + sRows, psRows == sRows);
- }
- /**
- * Tests fix for BUG#10155, double quotes not recognized when parsing
- * client-side prepared statements.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug10155() throws Exception {
- this.conn.prepareStatement(
- "SELECT \"Test question mark? Test single quote'\"")
- .executeQuery().close();
- }
- /**
- * Tests fix for BUG#10630, Statement.getWarnings() fails with NPE if
- * statement has been closed.
- */
- public void testBug10630() throws Exception {
- Connection conn2 = null;
- Statement stmt2 = null;
- try {
- conn2 = getConnectionWithProps((Properties)null);
- stmt2 = conn2.createStatement();
- conn2.close();
- stmt2.getWarnings();
- fail("Should've caught an exception here");
- } catch (SQLException sqlEx) {
- assertEquals("08003", sqlEx.getSQLState());
- } finally {
- if (stmt2 != null) {
- stmt2.close();
- }
- if (conn2 != null) {
- conn2.close();
- }
- }
- }
- /**
- * Tests fix for BUG#11115, Varbinary data corrupted when using server-side
- * prepared statements.
- */
- public void testBug11115() throws Exception {
- String tableName = "testBug11115";
- if (versionMeetsMinimum(4, 1, 0)) {
- createTable(tableName,
- "(pwd VARBINARY(30)) DEFAULT CHARACTER SET utf8", "InnoDB");
- byte[] bytesToTest = new byte[] { 17, 120, -1, -73, -5 };
- PreparedStatement insStmt = this.conn
- .prepareStatement("INSERT INTO " + tableName
- + " (pwd) VALUES (?)");
- insStmt.setBytes(1, bytesToTest);
- insStmt.executeUpdate();
- this.rs = this.stmt.executeQuery("SELECT pwd FROM " + tableName);
- this.rs.next();
- byte[] fromDatabase = this.rs.getBytes(1);
- assertEquals(bytesToTest.length, fromDatabase.length);
- for (int i = 0; i < bytesToTest.length; i++) {
- assertEquals(bytesToTest[i], fromDatabase[i]);
- }
- this.rs = this.conn
- .prepareStatement("SELECT pwd FROM " + tableName)
- .executeQuery();
- this.rs.next();
- fromDatabase = this.rs.getBytes(1);
- assertEquals(bytesToTest.length, fromDatabase.length);
- for (int i = 0; i < bytesToTest.length; i++) {
- assertEquals(bytesToTest[i], fromDatabase[i]);
- }
- }
- }
- public void testBug11540() throws Exception {
- Locale originalLocale = Locale.getDefault();
- Connection thaiConn = null;
- Statement thaiStmt = null;
- PreparedStatement thaiPrepStmt = null;
- try {
- createTable("testBug11540", "(field1 DATE, field2 TIMESTAMP)");
- this.stmt
- .executeUpdate("INSERT INTO testBug11540 VALUES (NOW(), NOW())");
- Locale.setDefault(new Locale("th", "TH"));
- Properties props = new Properties();
- props.setProperty("jdbcCompliantTruncation", "false");
- thaiConn = getConnectionWithProps(props);
- thaiStmt = thaiConn.createStatement();
- this.rs = thaiStmt
- .executeQuery("SELECT field1, field2 FROM testBug11540");
- this.rs.next();
- Date origDate = this.rs.getDate(1);
- Timestamp origTimestamp = this.rs.getTimestamp(1);
- this.rs.close();
- thaiStmt.executeUpdate("TRUNCATE TABLE testBug11540");
- thaiPrepStmt = ((com.mysql.jdbc.Connection) thaiConn)
- .clientPrepareStatement("INSERT INTO testBug11540 VALUES (?,?)");
- thaiPrepStmt.setDate(1, origDate);
- thaiPrepStmt.setTimestamp(2, origTimestamp);
- thaiPrepStmt.executeUpdate();
- this.rs = thaiStmt
- .executeQuery("SELECT field1, field2 FROM testBug11540");
- this.rs.next();
- Date testDate = this.rs.getDate(1);
- Timestamp testTimestamp = this.rs.getTimestamp(1);
- this.rs.close();
- assertEquals(origDate, testDate);
- assertEquals(origTimestamp, testTimestamp);
- } finally {
- Locale.setDefault(originalLocale);
- }
- }
- /**
- * Tests fix for BUG#11663, autoGenerateTestcaseScript uses bogus parameter
- * names for server-side prepared statements.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug11663() throws Exception {
- if (versionMeetsMinimum(4, 1, 0)
- && ((com.mysql.jdbc.Connection) this.conn)
- .getUseServerPreparedStmts()) {
- Connection testcaseGenCon = null;
- PrintStream oldErr = System.err;
- try {
- createTable("testBug11663", "(field1 int)");
- Properties props = new Properties();
- props.setProperty("autoGenerateTestcaseScript", "true");
- testcaseGenCon = getConnectionWithProps(props);
- ByteArrayOutputStream testStream = new ByteArrayOutputStream();
- PrintStream testErr = new PrintStream(testStream);
- System.setErr(testErr);
- this.pstmt = testcaseGenCon
- .prepareStatement("SELECT field1 FROM testBug11663 WHERE field1=?");
- this.pstmt.setInt(1, 1);
- this.pstmt.execute();
- System.setErr(oldErr);
- String testString = new String(testStream.toByteArray());
- int setIndex = testString.indexOf("SET @debug_stmt_param");
- int equalsIndex = testString.indexOf("=", setIndex);
- String paramName = testString.substring(setIndex + 4,
- equalsIndex);
- int usingIndex = testString.indexOf("USING " + paramName,
- equalsIndex);
- assertTrue(usingIndex != -1);
- } finally {
- System.setErr(oldErr);
- if (this.pstmt != null) {
- this.pstmt.close();
- this.pstmt = null;
- }
- if (testcaseGenCon != null) {
- testcaseGenCon.close();
- }
- }
- }
- }
- /**
- * Tests fix for BUG#11798 - Pstmt.setObject(...., Types.BOOLEAN) throws
- * exception.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug11798() throws Exception {
- if (isRunningOnJdk131()) {
- return; // test not valid on JDK-1.3.1
- }
- try {
- this.pstmt = this.conn.prepareStatement("SELECT ?");
- this.pstmt.setObject(1, Boolean.TRUE, Types.BOOLEAN);
- this.pstmt.setObject(1, new BigDecimal("1"), Types.BOOLEAN);
- this.pstmt.setObject(1, "true", Types.BOOLEAN);
- } finally {
- if (this.pstmt != null) {
- this.pstmt.close();
- this.pstmt = null;
- }
- }
- }
- /**
- * Tests fix for BUG#13255 - Reconnect during middle of executeBatch()
- * should not happen.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug13255() throws Exception {
- createTable("testBug13255", "(field_1 int)");
- Properties props = new Properties();
- props.setProperty("autoReconnect", "true");
- Connection reconnectConn = null;
- Statement reconnectStmt = null;
- PreparedStatement reconnectPStmt = null;
- try {
- reconnectConn = getConnectionWithProps(props);
- reconnectStmt = reconnectConn.createStatement();
- String connectionId = getSingleIndexedValueWithQuery(reconnectConn,
- 1, "SELECT CONNECTION_ID()").toString();
- reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (1)");
- reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (2)");
- reconnectStmt.addBatch("KILL " + connectionId);
- for (int i = 0; i < 100; i++) {
- reconnectStmt.addBatch("INSERT INTO testBug13255 VALUES (" + i
- + ")");
- }
- try {
- reconnectStmt.executeBatch();
- } catch (SQLException sqlEx) {
- // We expect this...we killed the connection
- }
- assertEquals(2, getRowCount("testBug13255"));
- this.stmt.executeUpdate("TRUNCATE TABLE testBug13255");
- reconnectConn.close();
- reconnectConn = getConnectionWithProps(props);
- connectionId = getSingleIndexedValueWithQuery(reconnectConn, 1,
- "SELECT CONNECTION_ID()").toString();
- reconnectPStmt = reconnectConn
- .prepareStatement("INSERT INTO testBug13255 VALUES (?)");
- reconnectPStmt.setInt(1, 1);
- reconnectPStmt.addBatch();
- reconnectPStmt.setInt(1, 2);
- reconnectPStmt.addBatch();
- reconnectPStmt.addBatch("KILL " + connectionId);
- for (int i = 3; i < 100; i++) {
- reconnectPStmt.setInt(1, i);
- reconnectPStmt.addBatch();
- }
- try {
- reconnectPStmt.executeBatch();
- } catch (SQLException sqlEx) {
- // We expect this...we killed the connection
- }
- assertEquals(2, getRowCount("testBug13255"));
- } finally {
- if (reconnectStmt != null) {
- reconnectStmt.close();
- }
- if (reconnectConn != null) {
- reconnectConn.close();
- }
- }
- }
- /**
- * Tests fix for BUG#15024 - Driver incorrectly closes streams passed as
- * arguments to PreparedStatements.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug15024() throws Exception {
- createTable("testBug15024", "(field1 BLOB)");
- try {
- this.pstmt = this.conn
- .prepareStatement("INSERT INTO testBug15024 VALUES (?)");
- testStreamsForBug15024(false, false);
- Properties props = new Properties();
- props.setProperty("useConfigs", "3-0-Compat");
- Connection compatConn = null;
- try {
- compatConn = getConnectionWithProps(props);
- this.pstmt = compatConn
- .prepareStatement("INSERT INTO testBug15024 VALUES (?)");
- testStreamsForBug15024(true, false);
- } finally {
- if (compatConn != null) {
- compatConn.close();
- }
- }
- } finally {
- if (this.pstmt != null) {
- PreparedStatement toClose = this.pstmt;
- this.pstmt = null;
- toClose.close();
- }
- }
- }
- /**
- * PreparedStatement should call EscapeProcessor.escapeSQL?
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug15141() throws Exception {
- try {
- createTable("testBug15141", "(field1 VARCHAR(32))");
- this.stmt.executeUpdate("INSERT INTO testBug15141 VALUES ('abc')");
- this.pstmt = this.conn
- .prepareStatement("select {d '1997-05-24'} FROM testBug15141");
- this.rs = this.pstmt.executeQuery();
- assertTrue(this.rs.next());
- assertEquals("1997-05-24", this.rs.getString(1));
- this.rs.close();
- this.rs = null;
- this.pstmt.close();
- this.pstmt = null;
- this.pstmt = ((com.mysql.jdbc.Connection) this.conn)
- .clientPrepareStatement("select {d '1997-05-24'} FROM testBug15141");
- this.rs = this.pstmt.executeQuery();
- assertTrue(this.rs.next());
- assertEquals("1997-05-24", this.rs.getString(1));
- this.rs.close();
- this.rs = null;
- this.pstmt.close();
- this.pstmt = null;
- } finally {
- if (this.rs != null) {
- ResultSet toCloseRs = this.rs;
- this.rs = null;
- toCloseRs.close();
- }
- if (this.pstmt != null) {
- PreparedStatement toClosePstmt = this.pstmt;
- this.pstmt = null;
- toClosePstmt.close();
- }
- }
- }
- /**
- * Tests fix for BUG#18041 - Server-side prepared statements don't cause
- * truncation exceptions to be thrown.
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug18041() throws Exception {
- if (versionMeetsMinimum(4, 1)) {
- createTable("testBug18041", "(`a` tinyint(4) NOT NULL,"
- + "`b` char(4) default NULL)");
- Properties props = new Properties();
- props.setProperty("jdbcCompliantTruncation", "true");
- props.setProperty("useServerPrepStmts", "true");
- Connection truncConn = null;
- PreparedStatement stm = null;
- try {
- truncConn = getConnectionWithProps(props);
- stm = truncConn
- .prepareStatement("insert into testBug18041 values (?,?)");
- stm.setInt(1, 1000);
- stm.setString(2, "nnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
- stm.executeUpdate();
- fail("Truncation exception should have been thrown");
- } catch (DataTruncation truncEx) {
- // we expect this
- } finally {
- if (this.stmt != null) {
- this.stmt.close();
- }
- if (truncConn != null) {
- truncConn.close();
- }
- }
- }
- }
- private void testStreamsForBug15024(boolean shouldBeClosedStream,
- boolean shouldBeClosedReader) throws SQLException {
- IsClosedInputStream bIn = new IsClosedInputStream(new byte[4]);
- IsClosedReader readerIn = new IsClosedReader("abcdef");
- this.pstmt.setBinaryStream(1, bIn, 4);
- this.pstmt.execute();
- assertEquals(shouldBeClosedStream, bIn.isClosed());
- this.pstmt.setCharacterStream(1, readerIn, 6);
- this.pstmt.execute();
- assertEquals(shouldBeClosedReader, readerIn.isClosed());
- this.pstmt.close();
- }
- class IsClosedReader extends StringReader {
- boolean isClosed = false;
- public IsClosedReader(String arg0) {
- super(arg0);
- }
- public void close() {
- super.close();
- this.isClosed = true;
- }
- public boolean isClosed() {
- return this.isClosed;
- }
- }
- class IsClosedInputStream extends ByteArrayInputStream {
- boolean isClosed = false;
- public IsClosedInputStream(byte[] arg0, int arg1, int arg2) {
- super(arg0, arg1, arg2);
- }
- public IsClosedInputStream(byte[] arg0) {
- super(arg0);
- }
- public void close() throws IOException {
-
- super.close();
- this.isClosed = true;
- }
- public boolean isClosed() {
- return this.isClosed;
- }
- }
- /**
- * Tests fix for BUG#1774 -- Truncated words after double quote
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug1774() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1774");
- this.stmt
- .executeUpdate("CREATE TABLE testBug1774 (field1 VARCHAR(255))");
- PreparedStatement pStmt = this.conn
- .prepareStatement("INSERT INTO testBug1774 VALUES (?)");
- String testString = "The word contains \" character";
- pStmt.setString(1, testString);
- pStmt.executeUpdate();
- this.rs = this.stmt.executeQuery("SELECT * FROM testBug1774");
- this.rs.next();
- assertEquals(this.rs.getString(1), testString);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1774");
- }
- }
- /**
- * Tests fix for BUG#1901 -- PreparedStatement.setObject(int, Object, int,
- * int) doesn't support CLOB or BLOB types.
- *
- * @throws Exception
- * if this test fails for any reason
- */
- public void testBug1901() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1901");
- this.stmt
- .executeUpdate("CREATE TABLE testBug1901 (field1 VARCHAR(255))");
- this.stmt.executeUpdate("INSERT INTO testBug1901 VALUES ('aaa')");
- this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug1901");
- this.rs.next();
- Clob valueAsClob = this.rs.getClob(1);
- Blob valueAsBlob = this.rs.getBlob(1);
- PreparedStatement pStmt = this.conn
- .prepareStatement("INSERT INTO testBug1901 VALUES (?)");
- pStmt.setObject(1, valueAsClob, java.sql.Types.CLOB, 0);
- pStmt.executeUpdate();
- pStmt.setObject(1, valueAsBlob, java.sql.Types.BLOB, 0);
- pStmt.executeUpdate();
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1901");
- }
- }
- /**
- * Test fix for BUG#1933 -- Driver property 'maxRows' has no effect.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug1933() throws Exception {
- if (versionMeetsMinimum(4, 0)) {
- Connection maxRowsConn = null;
- PreparedStatement maxRowsPrepStmt = null;
- Statement maxRowsStmt = null;
- try {
- Properties props = new Properties();
- props.setProperty("maxRows", "1");
- maxRowsConn = getConnectionWithProps(props);
- maxRowsStmt = maxRowsConn.createStatement();
- assertTrue(maxRowsStmt.getMaxRows() == 1);
- this.rs = maxRowsStmt.executeQuery("SELECT 1 UNION SELECT 2");
- this.rs.next();
- maxRowsPrepStmt = maxRowsConn
- .prepareStatement("SELECT 1 UNION SELECT 2");
- assertTrue(maxRowsPrepStmt.getMaxRows() == 1);
- this.rs = maxRowsPrepStmt.executeQuery();
- this.rs.next();
- assertTrue(!this.rs.next());
- props.setProperty("useServerPrepStmts", "false");
- maxRowsConn = getConnectionWithProps(props);
- maxRowsPrepStmt = maxRowsConn
- .prepareStatement("SELECT 1 UNION SELECT 2");
- assertTrue(maxRowsPrepStmt.getMaxRows() == 1);
- this.rs = maxRowsPrepStmt.executeQuery();
- this.rs.next();
- assertTrue(!this.rs.next());
- } finally {
- maxRowsConn.close();
- }
- }
- }
- /**
- * Tests the fix for BUG#1934 -- prepareStatement dies silently when
- * encountering Statement.RETURN_GENERATED_KEY
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug1934() throws Exception {
- if (isRunningOnJdk131()) {
- return; // test not valid on JDK-1.3.1
- }
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1934");
- this.stmt.executeUpdate("CREATE TABLE testBug1934 (field1 INT)");
- System.out.println("Before prepareStatement()");
- this.pstmt = this.conn.prepareStatement(
- "INSERT INTO testBug1934 VALUES (?)",
- java.sql.Statement.RETURN_GENERATED_KEYS);
- assertTrue(this.pstmt != null);
- System.out.println("After prepareStatement() - " + this.pstmt);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1934");
- }
- }
- /**
- * Tests fix for BUG#1958 - Improper bounds checking on
- * PreparedStatement.setFoo().
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug1958() throws Exception {
- PreparedStatement pStmt = null;
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1958");
- this.stmt.executeUpdate("CREATE TABLE testBug1958 (field1 int)");
- pStmt = this.conn
- .prepareStatement("SELECT * FROM testBug1958 WHERE field1 IN (?, ?, ?)");
- try {
- pStmt.setInt(4, 1);
- } catch (SQLException sqlEx) {
- assertTrue(SQLError.SQL_STATE_ILLEGAL_ARGUMENT.equals(sqlEx
- .getSQLState()));
- }
- } finally {
- if (pStmt != null) {
- pStmt.close();
- }
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug1958");
- }
- }
- /**
- * Tests the fix for BUG#2606, server-side prepared statements not returning
- * datatype YEAR correctly.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug2606() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2606");
- this.stmt
- .executeUpdate("CREATE TABLE testBug2606(year_field YEAR)");
- this.stmt.executeUpdate("INSERT INTO testBug2606 VALUES (2004)");
- PreparedStatement yrPstmt = this.conn
- .prepareStatement("SELECT year_field FROM testBug2606");
- this.rs = yrPstmt.executeQuery();
- assertTrue(this.rs.next());
- assertEquals(2004, this.rs.getInt(1));
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug2606");
- }
- }
- /**
- * Tests the fix for BUG#2671, nulls encoded incorrectly in server-side
- * prepared statements.
- *
- * @throws Exception
- * if an error occurs.
- */
- public void testBug2671() throws Exception {
- if (versionMeetsMinimum(4, 1)) {
- createTable("test3", "("
- + " `field1` int(8) NOT NULL auto_increment,"
- + " `field2` int(8) unsigned zerofill default NULL,"
- + " `field3` varchar(30) binary NOT NULL default '',"
- + " `field4` varchar(100) default NULL,"
- + " `field5` datetime NULL default '0000-00-00 00:00:00',"
- + " PRIMARY KEY (`field1`),"
- + " UNIQUE KEY `unq_id` (`field2`),"
- + " UNIQUE KEY (`field3`),"
- + " UNIQUE KEY (`field2`)"
- + " ) CHARACTER SET utf8", "InnoDB");
- this.stmt
- .executeUpdate("insert into test3 (field1, field3, field4) values (1,'blewis','Bob Lewis')");
- String query = " " + "UPDATE "
- + " test3 "
- + "SET "
- + " field2=? " + " ,field3=? "
- + " ,field4=? " + " ,field5=? "
- + "WHERE "
- + " field1 = ? ";
- java.sql.Date mydate = null;
- this.pstmt = this.conn.prepareStatement(query);
- this.pstmt.setInt(1, 13);
- this.pstmt.setString(2, "abc");
- this.pstmt.setString(3, "def");
- this.pstmt.setDate(4, mydate);
- this.pstmt.setInt(5, 1);
- int retval = this.pstmt.executeUpdate();
- assertTrue(retval == 1);
- }
- }
- /**
- * Tests fix for BUG#3103 -- java.util.Date not accepted as parameter to
- * PreparedStatement.setObject().
- *
- * @throws Exception
- * if the test fails
- *
- * @deprecated uses deprecated methods of Date class
- */
- public void testBug3103() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3103");
- this.stmt
- .executeUpdate("CREATE TABLE testBug3103 (field1 DATETIME)");
- PreparedStatement pStmt = this.conn
- .prepareStatement("INSERT INTO testBug3103 VALUES (?)");
- java.util.Date utilDate = new java.util.Date();
- pStmt.setObject(1, utilDate);
- pStmt.executeUpdate();
- this.rs = this.stmt.executeQuery("SELECT field1 FROM testBug3103");
- this.rs.next();
- java.util.Date retrUtilDate = new java.util.Date(this.rs
- .getTimestamp(1).getTime());
- // We can only compare on the day/month/year hour/minute/second
- // interval, because the timestamp has added milliseconds to the
- // internal date...
- assertTrue("Dates not equal", (utilDate.getMonth() == retrUtilDate
- .getMonth())
- && (utilDate.getDate() == retrUtilDate.getDate())
- && (utilDate.getYear() == retrUtilDate.getYear())
- && (utilDate.getHours() == retrUtilDate.getHours())
- && (utilDate.getMinutes() == retrUtilDate.getMinutes())
- && (utilDate.getSeconds() == retrUtilDate.getSeconds()));
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3103");
- }
- }
- /**
- * Tests fix for BUG#3520
- *
- * @throws Exception
- * ...
- */
- public void testBug3520() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS t");
- this.stmt.executeUpdate("CREATE TABLE t (s1 int,primary key (s1))");
- this.stmt.executeUpdate("INSERT INTO t VALUES (1)");
- this.stmt.executeUpdate("INSERT INTO t VALUES (1)");
- } catch (SQLException sqlEx) {
- System.out.println(sqlEx.getSQLState());
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS t");
- }
- }
- /**
- * Test fix for BUG#3557 -- UpdatableResultSet not picking up default values
- *
- * @throws Exception
- * if test fails.
- */
- public void testBug3557() throws Exception {
- boolean populateDefaults = ((com.mysql.jdbc.ConnectionProperties) this.conn)
- .getPopulateInsertRowWithDefaultValues();
- try {
- ((com.mysql.jdbc.ConnectionProperties) this.conn)
- .setPopulateInsertRowWithDefaultValues(true);
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3557");
- this.stmt.executeUpdate("CREATE TABLE testBug3557 ( "
- + "`a` varchar(255) NOT NULL default 'XYZ', "
- + "`b` varchar(255) default '123', "
- + "PRIMARY KEY (`a`(100)))");
- Statement updStmt = this.conn
- .createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
- ResultSet.CONCUR_UPDATABLE);
- this.rs = updStmt.executeQuery("SELECT * FROM testBug3557");
- assertTrue(this.rs.getConcurrency() == ResultSet.CONCUR_UPDATABLE);
- this.rs.moveToInsertRow();
- assertEquals("XYZ", this.rs.getObject(1));
- assertEquals("123", this.rs.getObject(2));
- } finally {
- ((com.mysql.jdbc.ConnectionProperties) this.conn)
- .setPopulateInsertRowWithDefaultValues(populateDefaults);
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3557");
- }
- }
- /**
- * Tests fix for BUG#3620 -- Timezone not respected correctly.
- *
- * @throws SQLException
- * if the test fails.
- */
- public void testBug3620() throws SQLException {
- if (isRunningOnJRockit()) {
- // bug with their timezones
- return;
- }
-
- if (isRunningOnJdk131()) {
- // bug with timezones, no update
- // for new DST in USA
- return;
- }
-
- long epsillon = 3000; // 3 seconds time difference
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3620");
- this.stmt
- .executeUpdate("CREATE TABLE testBug3620 (field1 TIMESTAMP)");
- PreparedStatement tsPstmt = this.conn
- .prepareStatement("INSERT INTO testBug3620 VALUES (?)");
- Calendar pointInTime = Calendar.getInstance();
- pointInTime.set(2004, 02, 29, 10, 0, 0);
- long pointInTimeOffset = pointInTime.getTimeZone().getRawOffset();
- java.sql.Timestamp ts = new java.sql.Timestamp(pointInTime
- .getTime().getTime());
- tsPstmt.setTimestamp(1, ts);
- tsPstmt.executeUpdate();
- String tsValueAsString = getSingleValue("testBug3620", "field1",
- null).toString();
- System.out.println("Timestamp as string with no calendar: "
- + tsValueAsString.toString());
- Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
- this.stmt.executeUpdate("DELETE FROM testBug3620");
- Properties props = new Properties();
- props.put("useTimezone", "true");
- // props.put("serverTimezone", "UTC");
- Connection tzConn = getConnectionWithProps(props);
- Statement tsStmt = tzConn.createStatement();
- tsPstmt = tzConn
- .prepareStatement("INSERT INTO testBug3620 VALUES (?)");
- tsPstmt.setTimestamp(1, ts, cal);
- tsPstmt.executeUpdate();
- tsValueAsString = getSingleValue("testBug3620", "field1", null)
- .toString();
- Timestamp tsValueAsTimestamp = (Timestamp) getSingleValue(
- "testBug3620", "field1", null);
- System.out.println("Timestamp as string with UTC calendar: "
- + tsValueAsString.toString());
- System.out.println("Timestamp as Timestamp with UTC calendar: "
- + tsValueAsTimestamp);
- this.rs = tsStmt.executeQuery("SELECT field1 FROM testBug3620");
- this.rs.next();
- Timestamp tsValueUTC = this.rs.getTimestamp(1, cal);
- //
- // We use this testcase with other vendors, JDBC spec
- // requires result set fields can only be read once,
- // although MySQL doesn't require this ;)
- //
- this.rs = tsStmt.executeQuery("SELECT field1 FROM testBug3620");
- this.rs.next();
- Timestamp tsValueStmtNoCal = this.rs.getTimestamp(1);
- System.out
- .println("Timestamp specifying UTC calendar from normal statement: "
- + tsValueUTC.toString());
- PreparedStatement tsPstmtRetr = tzConn
- .prepareStatement("SELECT field1 FROM testBug3620");
- this.rs = tsPstmtRetr.executeQuery();
- this.rs.next();
- Timestamp tsValuePstmtUTC = this.rs.getTimestamp(1, cal);
- System.out
- .println("Timestamp specifying UTC calendar from prepared statement: "
- + tsValuePstmtUTC.toString());
- //
- // We use this testcase with other vendors, JDBC spec
- // requires result set fields can only be read once,
- // although MySQL doesn't require this ;)
- //
- this.rs = tsPstmtRetr.executeQuery();
- this.rs.next();
- Timestamp tsValuePstmtNoCal = this.rs.getTimestamp(1);
- System.out
- .println("Timestamp specifying no calendar from prepared statement: "
- + tsValuePstmtNoCal.toString());
- long stmtDeltaTWithCal = (ts.getTime() - tsValueStmtNoCal.getTime());
- long deltaOrig = Math.abs(stmtDeltaTWithCal - pointInTimeOffset);
- assertTrue(
- "Difference between original timestamp and timestamp retrieved using java.sql.Statement "
- + "set in database using UTC calendar is not ~= "
- + epsillon + ", it is actually " + deltaOrig,
- (deltaOrig < epsillon));
- long pStmtDeltaTWithCal = (ts.getTime() - tsValuePstmtNoCal
- .getTime());
- System.out
- .println(Math.abs(pStmtDeltaTWithCal - pointInTimeOffset)
- + " < "
- + epsillon
- + (Math.abs(pStmtDeltaTWithCal - pointInTimeOffset) < epsillon));
- assertTrue(
- "Difference between original timestamp and timestamp retrieved using java.sql.PreparedStatement "
- + "set in database using UTC calendar is not ~= "
- + epsillon
- + ", it is actually "
- + pStmtDeltaTWithCal, (Math.abs(pStmtDeltaTWithCal
- - pointInTimeOffset) < epsillon));
- System.out
- .println("Difference between original ts and ts with no calendar: "
- + (ts.getTime() - tsValuePstmtNoCal.getTime())
- + ", offset should be " + pointInTimeOffset);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3620");
- }
- }
- /**
- * Tests that DataTruncation is thrown when data is truncated.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug3697() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3697");
- this.stmt
- .executeUpdate("CREATE TABLE testBug3697 (field1 VARCHAR(255))");
- StringBuffer updateBuf = new StringBuffer(
- "INSERT INTO testBug3697 VALUES ('");
- for (int i = 0; i < 512; i++) {
- updateBuf.append("A");
- }
- updateBuf.append("')");
- try {
- this.stmt.executeUpdate(updateBuf.toString());
- } catch (DataTruncation dtEx) {
- // This is an expected exception....
- }
- SQLWarning warningChain = this.stmt.getWarnings();
- System.out.println(warningChain);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3697");
- }
- }
- /**
- * Tests fix for BUG#3804, data truncation on server should throw
- * DataTruncation exception.
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug3804() throws Exception {
- if (versionMeetsMinimum(4, 1)) {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3804");
- this.stmt
- .executeUpdate("CREATE TABLE testBug3804 (field1 VARCHAR(5))");
- boolean caughtTruncation = false;
- try {
- this.stmt
- .executeUpdate("INSERT INTO testBug3804 VALUES ('1234567')");
- } catch (DataTruncation truncationEx) {
- caughtTruncation = true;
- System.out.println(truncationEx);
- }
- assertTrue("Data truncation exception should've been thrown",
- caughtTruncation);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3804");
- }
- }
- }
- /**
- * Tests BUG#3873 - PreparedStatement.executeBatch() not returning all
- * generated keys (even though that's not JDBC compliant).
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug3873() throws Exception {
- if (isRunningOnJdk131()) {
- return; // test not valid on JDK-1.3.1
- }
- PreparedStatement batchStmt = null;
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3873");
- this.stmt
- .executeUpdate("CREATE TABLE testBug3873 (keyField INT NOT NULL PRIMARY KEY AUTO_INCREMENT, dataField VARCHAR(32))");
- batchStmt = this.conn.prepareStatement(
- "INSERT INTO testBug3873 (dataField) VALUES (?)",
- Statement.RETURN_GENERATED_KEYS);
- batchStmt.setString(1, "abc");
- batchStmt.addBatch();
- batchStmt.setString(1, "def");
- batchStmt.addBatch();
- batchStmt.setString(1, "ghi");
- batchStmt.addBatch();
- int[] updateCounts = batchStmt.executeBatch();
- this.rs = batchStmt.getGeneratedKeys();
- while (this.rs.next()) {
- System.out.println(this.rs.getInt(1));
- }
- this.rs = batchStmt.getGeneratedKeys();
- assertTrue(this.rs.next());
- assertTrue(1 == this.rs.getInt(1));
- assertTrue(this.rs.next());
- assertTrue(2 == this.rs.getInt(1));
- assertTrue(this.rs.next());
- assertTrue(3 == this.rs.getInt(1));
- assertTrue(!this.rs.next());
- } finally {
- if (batchStmt != null) {
- batchStmt.close();
- }
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug3873");
- }
- }
- /**
- * Tests fix for BUG#4119 -- misbehavior in a managed environment from
- * MVCSoft JDO
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug4119() throws Exception {
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4119");
- this.stmt.executeUpdate("CREATE TABLE `testBug4119` ("
- + "`field1` varchar(255) NOT NULL default '',"
- + "`field2` bigint(20) default NULL,"
- + "`field3` int(11) default NULL,"
- + "`field4` datetime default NULL,"
- + "`field5` varchar(75) default NULL,"
- + "`field6` varchar(75) default NULL,"
- + "`field7` varchar(75) default NULL,"
- + "`field8` datetime default NULL,"
- + " PRIMARY KEY (`field1`(100))" + ")");
- PreparedStatement pStmt = this.conn
- .prepareStatement("insert into testBug4119 (field2, field3,"
- + "field4, field5, field6, field7, field8, field1) values (?, ?,"
- + "?, ?, ?, ?, ?, ?)");
- pStmt.setString(1, "0");
- pStmt.setString(2, "0");
- pStmt.setTimestamp(3, new java.sql.Timestamp(System
- .currentTimeMillis()));
- pStmt.setString(4, "ABC");
- pStmt.setString(5, "DEF");
- pStmt.setString(6, "AA");
- pStmt.setTimestamp(7, new java.sql.Timestamp(System
- .currentTimeMillis()));
- pStmt.setString(8, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
- pStmt.executeUpdate();
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4119");
- }
- }
- /**
- * Tests fix for BUG#4311 - Error in JDBC retrieval of mediumint column when
- * using prepared statements and binary result sets.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug4311() throws Exception {
- try {
- int lowValue = -8388608;
- int highValue = 8388607;
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4311");
- this.stmt
- .executeUpdate("CREATE TABLE testBug4311 (low MEDIUMINT, high MEDIUMINT)");
- this.stmt.executeUpdate("INSERT INTO testBug4311 VALUES ("
- + lowValue + ", " + highValue + ")");
- PreparedStatement pStmt = this.conn
- .prepareStatement("SELECT low, high FROM testBug4311");
- this.rs = pStmt.executeQuery();
- assertTrue(this.rs.next());
- assertTrue(this.rs.getInt(1) == lowValue);
- assertTrue(this.rs.getInt(2) == highValue);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4311");
- }
- }
- /**
- * Tests fix for BUG#4510 -- Statement.getGeneratedKeys() fails when key >
- * 32767
- *
- * @throws Exception
- * if the test fails
- */
- public void testBug4510() throws Exception {
- if (isRunningOnJdk131()) {
- return; // test not valid on JDK-1.3.1
- }
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4510");
- this.stmt.executeUpdate("CREATE TABLE testBug4510 ("
- + "field1 INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
- + "field2 VARCHAR(100))");
- this.stmt
- .executeUpdate("INSERT INTO testBug4510 (field1, field2) VALUES (32767, 'bar')");
- PreparedStatement p = this.conn.prepareStatement(
- "insert into testBug4510 (field2) values (?)",
- Statement.RETURN_GENERATED_KEYS);
- p.setString(1, "blah");
- p.executeUpdate();
- ResultSet rs = p.getGeneratedKeys();
- rs.next();
- System.out.println("Id: " + rs.getInt(1));
- rs.close();
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4510");
- }
- }
- /**
- * Server doesn't accept everything as a server-side prepared statement, so
- * by default we scan for stuff it can't handle.
- *
- * @throws SQLException
- */
- public void testBug4718() throws SQLException {
- if (versionMeetsMinimum(4, 1, 0)
- && ((com.mysql.jdbc.Connection) this.conn)
- .getUseServerPreparedStmts()) {
- this.pstmt = this.conn.prepareStatement("SELECT 1 LIMIT ?");
- assertTrue(this.pstmt instanceof com.mysql.jdbc.PreparedStatement);
- this.pstmt = this.conn.prepareStatement("SELECT 1 LIMIT 1");
- assertTrue(this.pstmt instanceof com.mysql.jdbc.ServerPreparedStatement);
- this.pstmt = this.conn.prepareStatement("SELECT 1 LIMIT 1, ?");
- assertTrue(this.pstmt instanceof com.mysql.jdbc.PreparedStatement);
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4718");
- this.stmt
- .executeUpdate("CREATE TABLE testBug4718 (field1 char(32))");
- this.pstmt = this.conn
- .prepareStatement("ALTER TABLE testBug4718 ADD INDEX (field1)");
- assertTrue(this.pstmt instanceof com.mysql.jdbc.PreparedStatement);
- this.pstmt = this.conn.prepareStatement("SELECT 1");
- assertTrue(this.pstmt instanceof ServerPreparedStatement);
- this.pstmt = this.conn
- .prepareStatement("UPDATE testBug4718 SET field1=1");
- assertTrue(this.pstmt instanceof ServerPreparedStatement);
- this.pstmt = this.conn
- .prepareStatement("UPDATE testBug4718 SET field1=1 LIMIT 1");
- assertTrue(this.pstmt instanceof ServerPreparedStatement);
- this.pstmt = this.conn
- .prepareStatement("UPDATE testBug4718 SET field1=1 LIMIT ?");
- assertTrue(this.pstmt instanceof com.mysql.jdbc.PreparedStatement);
- this.pstmt = this.conn
- .prepareStatement("UPDATE testBug4718 SET field1='Will we ignore LIMIT ?,?'");
- assertTrue(this.pstmt instanceof ServerPreparedStatement);
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug4718");
- }
- }
- }
- /**
- * Tests fix for BUG#5012 -- ServerPreparedStatements dealing with return of
- * DECIMAL type don't work.
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug5012() throws Exception {
- PreparedStatement pStmt = null;
- String valueAsString = "12345.12";
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5012");
- this.stmt
- .executeUpdate("CREATE TABLE testBug5012(field1 DECIMAL(10,2))");
- this.stmt.executeUpdate("INSERT INTO testBug5012 VALUES ("
- + valueAsString + ")");
- pStmt = this.conn
- .prepareStatement("SELECT field1 FROM testBug5012");
- this.rs = pStmt.executeQuery();
- assertTrue(this.rs.next());
- assertEquals(new BigDecimal(valueAsString), this.rs
- .getBigDecimal(1));
- } finally {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5012");
- if (pStmt != null) {
- pStmt.close();
- }
- }
- }
- /**
- * Tests fix for BUG#5133 -- PreparedStatement.toString() doesn't return
- * correct value if no parameters are present in statement.
- *
- * @throws Exception
- */
- public void testBug5133() throws Exception {
- String query = "SELECT 1";
- String output = this.conn.prepareStatement(query).toString();
- System.out.println(output);
- assertTrue(output.indexOf(query) != -1);
- }
- /**
- * Tests for BUG#5191 -- PreparedStatement.executeQuery() gives
- * OutOfMemoryError
- *
- * @throws Exception
- * if the test fails.
- */
- public void testBug5191() throws Exception {
- PreparedStatement pStmt = null;
- try {
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5191Q");
- this.stmt.executeUpdate("DROP TABLE IF EXISTS testBug5191C");
- this.stmt.executeUpdate("CREATE TABLE testBug5191Q"
- + "(QuestionId int NOT NULL AUTO_INCREMENT, "
- + "Text VARCHAR(200), " + "PRIMARY KEY(QuestionId))");
- this.stmt.executeUpdate("CREATE TABLE testBug519…
Large files files are truncated, but you can click here to view the full file