Description:
There is a problem running a query containing the text LIMIT when connecting to mysql 4.1.0 alpha using mysql-connector-java-3.0.7.jar and mysql-connector-java-3.0.9.jar. The followinf exception is thrown
java.sql.SQLException: Error during query: Unexpected Exception:
java.lang.ArrayIndexOutOfBoundsException message given: null
Nested Stack Trace:
** BEGIN NESTED EXCEPTION **
java.lang.ArrayIndexOutOfBoundsException
STACKTRACE:
java.lang.ArrayIndexOutOfBoundsException
at com.mysql.jdbc.Buffer.readFieldLength(Buffer.java:311)
at com.mysql.jdbc.Buffer.fastSkipLenString(Buffer.java:112)
at com.mysql.jdbc.MysqlIO.unpackField(MysqlIO.java:381)
at com.mysql.jdbc.MysqlIO.getResultSet(MysqlIO.java:242)
at com.mysql.jdbc.MysqlIO.readResults(MysqlIO.java:1189)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1150)
at com.mysql.jdbc.MysqlIO.sqlQuery(MysqlIO.java:1114)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1873)
at com.mysql.jdbc.Connection.execSQL(Connection.java:1807)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1072)
How to repeat:
Create a Statement and limit the results to 250 rows (testStmt.setMaxRows(250)), but if the text "LIMIT" appears anywhere in the statement an ArrayIndexOutOfBoundsException thrown
e.g.
try{
Statement testStmt = con.createStatement();
testStmt.setMaxRows(250);
ResultSet testResults = testStmt.executeQuery("SELECT * FROM CUSTOMER_LIMIT);
.
.
.
}
catch (SQLException e)
{
e.printStackTrace();
}
Suggested fix:
I looked the mysql-connector-java-3.0.9-stable code in com.mysql.jdbc.Statement.executeQuery(String sql)it looks for the text "LIMIT" I presume it should be " LIMIT "
if (sql.toUpperCase().indexOf("LIMIT") != -1) {
results = connection.execSQL(sql, maxRows,
resultSetConcurrency, createStreamingResultSet(),
true, this.currentCatalog);
} else {
This will not totally fix the problem, because if a LIMIT is set in the sql query and it is larger than the setMaxRows(250) an exception is thrown again.
Statement testStmt = con.createStatement();
testStmt.setMaxRows(250);
ResultSet testResults = testStmt.executeQuery("SELECT * FROM CUSTOMER LIMIT 500);
If the resultSet contains 500 it will cause the exception to be thrown again.