Bug #69788 Some ResultSet methods don't throw the proper exception when on an invalid row
Submitted: 19 Jul 2013 0:57
Reporter: Jess Balint Email Updates:
Status: Verified Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:5.1.25 OS:Any
Assigned to: Alexander Soklakov CPU Architecture:Any

[19 Jul 2013 0:57] Jess Balint
Description:
ResultSet.getString(), etc should throw an exception when on an "invalid row", e.g. beforeFirst:

java.sql.SQLException: Before start of result set
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
    at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:855)
    at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5773)
    at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5693)

Some methods don't throw the proper exception:
java.lang.NullPointerException
    at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2211)
    at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2173)

How to repeat:
call getDate() when on an invalid row

Suggested fix:
All public get*() methods on ResultSet should call checkRowPos()
[19 Jul 2013 2:20] Jess Balint
NPE is thrown on this line:

Date tmpDate = this.thisRow.getDateFast(columnIndexMinusOne, this.connection, this, cal);
[2 Jan 2016 20:07] Ramin Orujov
Fixed this bug in this commit.
https://github.com/raminorujov/mysql-connector-j/commit/dc4667f84f6a6a3223fde4793bd050f4de...
[2 Jan 2016 20:15] Ramin Orujov
Test case

Attachment: Bug69788.java (text/x-java), 1.13 KiB.

[2 Jan 2016 20:19] Ramin Orujov
Here is test case with attached Bug69788.java file. 

create table test.bug69788(
    col_int int,
    col_str varchar(50),
    col_datetime datetime,
    col_date date
);

insert into test.bug69788 values(1, 'test', now(), curdate());

select col_int, col_str, col_datetime, col_date from test.bug69788;

raminorujov@ro--ubuntu:~/repo/ro-mysql-connector-j/mysql-connector-j$ cd ~/Downloads/
raminorujov@ro--ubuntu:~/Downloads$ javac -cp .:mysql-connector-java-5.1.38-SNAPSHOT-bin.jar Bug69788.java 
raminorujov@ro--ubuntu:~/Downloads$ java -cp .:mysql-connector-java-5.1.38-SNAPSHOT-bin.jar Bug69788
Exception in thread "main" java.sql.SQLException: Before start of result set
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
	at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:790)
	at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:2012)
	at com.mysql.jdbc.ResultSetImpl.getDate(ResultSetImpl.java:1978)
	at Bug69788.main(Bug69788.java:30)
raminorujov@ro--ubuntu:~/Downloads$