According to the jdk jdbc documentation, the resultset: setfetchsize () function syntax sets the number of rows that should be obtained from the database for this resultset object when more rows are required for the JDBC driver. If the specified retrieved size is zero, the JDBC driver ignores this value and arbitrarily guesses the obtained size. The default value is set by the statement object of the created result set. The size can be changed at any time.
However, when the oracle JDBC driver is used, an inexplicable error occurs in the program we developed, and the obtained value is always inconsistent with the actual value of the database, after careful testing and troubleshooting, we found that the error was caused by the call of the resultset: setfetchsize () function. In our testing environment, the default size of oracle JDBC is resultset to 10. If we modify it to 100, generally, when obtaining the first value of 11th records, the obtained value is inconsistent with the actual value of the database. The statement that causes the problem is as follows:
Resultset equiprs = rolestat.exe cutequery ();
If (equiprs. getfetchsize () <20)
Equiprs. setfetchsize (20 );
However, if we call the setfetchsize () function of statement to set fetchsize to the expected value in advance, this will not cause the problem:
Rolestat. setfetchsize (20); // The setfetchsize () method of statement will not cause problems.
Resultset equiprs = rolestat.exe cutequery ();
Therefore, we suspect that this is a bug implemented by Oracle JDBC. The test environment is as follows:
1. Oracle 10201_database_win32.zip server version
2. Both class12.jar and ojdbc14.jar in the oracle10.2.0/Server/jdbc/lib/directory of the installation of JDBC 10201_database_win32 have been tested.
Test code:
Public static void main (string [] aargvs)
{
Try {
String jdbcurl = "JDBC: oracle: thin: @ localhost: 1521: orcl ";
Properties pros = new properties ();
Pros. setproperty ("user", "test ");
Pros. setproperty ("password", "test ");
Connection conn = new Oracle. JDBC. Driver. oracledriver (). Connect (jdbcurl, PROs );
Preparedstatement volstat = conn. preparestatement ("select M, N from M ");
Preparedstatement rolestat = conn. preparestatement ("select M_c, M_f FROM Mn where N_C =? And n_F =? ");
Resultset volrs = volstat.exe cutequery ();
Volrs. setfetchsize (100 );
While (volrs. Next ())
{
Long con = 1492; // voltagelevel
Long frag = volrs. getlong (1 );
String uri = volrs. getstring (2 );
System. Out. Print ("[" + con + "," + frag + "]" + URI + ":");
Rolestat. setlong (1, con );
Rolestat. setlong (2, frag );
// Rolestat. setfetchsize (20); // The setfetchsize () method of statement will not cause problems.
Resultset equiprs = rolestat.exe cutequery ();
If (equiprs. getfetchsize () <20) // The setfetchsize () method of resultset may cause problems.
Equiprs. setfetchsize (20 );
While (equiprs. Next ())
{
System. Out. Print ("[" + equiprs. getlong (1) + "," + equiprs. getlong (2) + "]");
}
Equiprs. Close ();
System. Out. println ("");
}
}
Catch (exception ex)
{
Ex. printstacktrace ();
}
}