I have an application where I want to define the default number of rows to prefetch for a connection, using for both Oracle and SQL Server drivers. The Oracle driver has a the OracleConnection
interface, which provides the setDefaultRowPrefetch
method to do it, but I didn't find anything equivalent for the SQL Server driver.
There is a method to define the default row prefetch for a connection using the SQL Server JDBC driver?
The usual ways to set row fetch size are:
java.sql.Connection
vendor implementation class custom method (e.g. OracleConnection.setDefaultRowPrefetch
)java.sql.Statement.setFetchSize(int)
: gives a hint to the driver as to the row fetch size for all ResultSets
obtained from this Statement
. This method is inherited by PreparedStatement
and CallableStatement
. Most JDBC drivers support it.java.sql.ResultSet.setFetchSize(int)
: gives a hint to the driver as to the row fetch size for all this ResultSet
. MS SQL Server JDBC driver does not support any of these ways:
Statement
.By default it retrieves all the rows from database unless you specify cursor type in the JDBC driver. MSSQL driver can't directly control the fetch size using the usual methods.
Solutions:
Statement
to SQLServerStatement
and use the method setMaxRows(int)
. Why they didn't implement this within the standard method Steve Ballmer only knows ;^)Connection
string property selectMethod=cursor
. Alternatively, you can create the Statement
with com.microsoft.sqlserver.jdbc.SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY
scrollability for forward-only, read-only access, and then use the setFetchSize
method to tune performance. http://technet.microsoft.com/en-us/library/aa342344%28SQL.90%29.aspx
SET ROWCOUNT
or SELECT TOP N
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With