Java Code Examples for java.sql.Statement.setMaxRows()

The following are Jave code examples for showing how to use setMaxRows() of the java.sql.Statement class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: the-vigilantes   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
/**
 * Executes a query containing the clause LIMIT with a Statement and a PreparedStatement, using a combination of
 * Connection properties, maxRows value and limit clause value, and tests if the results count is the expected.
 */
private void testBug71396MultiSettingsCheck(String connProps, int maxRows, int limitClause, int expRowCount) throws SQLException {
    Connection testConn = getConnectionWithProps(connProps);

    Statement testStmt = testConn.createStatement();
    if (maxRows > 0) {
        testStmt.setMaxRows(maxRows);
    }
    testStmt.execute("SELECT 1"); // force limit to be applied into current session

    testBug71396StatementCheck(testStmt, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount);
    testBug71396PrepStatementCheck(testConn, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount, maxRows);

    testStmt.close();
    testConn.close();
}
 
Example 2
Project: OpenVertretung   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
/**
 * Executes a query containing the clause LIMIT with a Statement and a PreparedStatement, using a combination of
 * Connection properties, maxRows value and limit clause value, and tests if the results count is the expected.
 */
private void testBug71396MultiSettingsCheck(String connProps, int maxRows, int limitClause, int expRowCount) throws SQLException {
    Connection testConn = getConnectionWithProps(connProps);

    Statement testStmt = testConn.createStatement();
    if (maxRows > 0) {
        testStmt.setMaxRows(maxRows);
    }
    testStmt.execute("SELECT 1"); // force limit to be applied into current session

    testBug71396StatementCheck(testStmt, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount);
    testBug71396PrepStatementCheck(testConn, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount, maxRows);

    testStmt.close();
    testConn.close();
}
 
Example 3
Project: ProyectoPacientes   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
/**
 * Executes a query containing the clause LIMIT with a Statement and a PreparedStatement, using a combination of
 * Connection properties, maxRows value and limit clause value, and tests if the results count is the expected.
 */
private void testBug71396MultiSettingsCheck(String connProps, int maxRows, int limitClause, int expRowCount) throws SQLException {
    Connection testConn = getConnectionWithProps(connProps);

    Statement testStmt = testConn.createStatement();
    if (maxRows > 0) {
        testStmt.setMaxRows(maxRows);
    }
    testStmt.execute("SELECT 1"); // force limit to be applied into current session

    testBug71396StatementCheck(testStmt, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount);
    testBug71396PrepStatementCheck(testConn, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount, maxRows);

    testStmt.close();
    testConn.close();
}
 
Example 4
Project: BibliotecaPS   File: StatementRegressionTest.java   Source Code and License Vote up 6 votes
/**
 * Executes a query containing the clause LIMIT with a Statement and a PreparedStatement, using a combination of
 * Connection properties, maxRows value and limit clause value, and tests if the results count is the expected.
 */
private void testBug71396MultiSettingsCheck(String connProps, int maxRows, int limitClause, int expRowCount) throws SQLException {
    Connection testConn = getConnectionWithProps(connProps);

    Statement testStmt = testConn.createStatement();
    if (maxRows > 0) {
        testStmt.setMaxRows(maxRows);
    }
    testStmt.execute("SELECT 1"); // force limit to be applied into current session

    testBug71396StatementCheck(testStmt, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount);
    testBug71396PrepStatementCheck(testConn, String.format("SELECT * FROM testBug71396 LIMIT %d", limitClause), expRowCount, maxRows);

    testStmt.close();
    testConn.close();
}
 
Example 5
Project: the-vigilantes   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Tests fix for BUG#907
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testSetMaxRows() throws Exception {
    Statement maxRowsStmt = null;

    try {
        maxRowsStmt = this.conn.createStatement();
        maxRowsStmt.setMaxRows(1);
        this.rs = maxRowsStmt.executeQuery("SELECT 1");
    } finally {
        if (maxRowsStmt != null) {
            maxRowsStmt.close();
        }
    }
}
 
Example 6
Project: the-vigilantes   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Initializes and returns a Statement with maxRows defined. Tests the SQL_SELECT_LIMIT defined. Executing this
 * query also forces this limit to be defined at session level.
 */
private Statement testBug71396StatementInit(Connection testConn, int maxRows) throws SQLException {
    ResultSet testRS;
    Statement testStmt = testConn.createStatement();

    testStmt.setMaxRows(maxRows);
    // while consulting SQL_SELECT_LIMIT setting also forces limit to be applied into current session
    testRS = testStmt.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    testRS.next();
    assertEquals("Wrong @@SESSION.SQL_SELECT_LIMIT", maxRows, testRS.getInt(1));

    return testStmt;
}
 
Example 7
Project: aceql-http   File: ServerSqlUtil.java   Source Code and License Vote up 5 votes
/**
    * Set the maximum rows to return to the client side
    * 
    * @param statement
    *            the statement to set
    * @param databaseConfigurator
    *            the DatabaseConfigurator which contains the getMaxRowsToReturn()
    *            method
    * @throws SQLException
    */
   public static void setMaxRowsToReturn(Statement statement,
    DatabaseConfigurator databaseConfigurator) throws SQLException, IOException {

int maxRowsToReturn = DatabaseConfiguratorCall
	.getMaxRowsToReturn(databaseConfigurator);

if (maxRowsToReturn > 0) {
    if (statement.getMaxRows() == 0
	    || (statement.getMaxRows() > maxRowsToReturn)) {
	statement.setFetchSize(0); // To avoid any possible conflict
	statement.setMaxRows(maxRowsToReturn);
    }
}
   }
 
Example 8
Project: OpenVertretung   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Tests fix for BUG#907
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testSetMaxRows() throws Exception {
    Statement maxRowsStmt = null;

    try {
        maxRowsStmt = this.conn.createStatement();
        maxRowsStmt.setMaxRows(1);
        this.rs = maxRowsStmt.executeQuery("SELECT 1");
    } finally {
        if (maxRowsStmt != null) {
            maxRowsStmt.close();
        }
    }
}
 
Example 9
Project: OpenVertretung   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Initializes and returns a Statement with maxRows defined. Tests the SQL_SELECT_LIMIT defined. Executing this
 * query also forces this limit to be defined at session level.
 */
private Statement testBug71396StatementInit(Connection testConn, int maxRows) throws SQLException {
    ResultSet testRS;
    Statement testStmt = testConn.createStatement();

    testStmt.setMaxRows(maxRows);
    // while consulting SQL_SELECT_LIMIT setting also forces limit to be applied into current session
    testRS = testStmt.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    testRS.next();
    assertEquals("Wrong @@SESSION.SQL_SELECT_LIMIT", maxRows, testRS.getInt(1));

    return testStmt;
}
 
Example 10
Project: lams   File: JdbcTemplate.java   Source Code and License Vote up 5 votes
/**
 * Prepare the given JDBC Statement (or PreparedStatement or CallableStatement),
 * applying statement settings such as fetch size, max rows, and query timeout.
 * @param stmt the JDBC Statement to prepare
 * @throws SQLException if thrown by JDBC API
 * @see #setFetchSize
 * @see #setMaxRows
 * @see #setQueryTimeout
 * @see org.springframework.jdbc.datasource.DataSourceUtils#applyTransactionTimeout
 */
protected void applyStatementSettings(Statement stmt) throws SQLException {
	int fetchSize = getFetchSize();
	if (fetchSize > 0) {
		stmt.setFetchSize(fetchSize);
	}
	int maxRows = getMaxRows();
	if (maxRows > 0) {
		stmt.setMaxRows(maxRows);
	}
	DataSourceUtils.applyTimeout(stmt, getDataSource(), getQueryTimeout());
}
 
Example 11
Project: ProyectoPacientes   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Tests fix for BUG#907
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testSetMaxRows() throws Exception {
    Statement maxRowsStmt = null;

    try {
        maxRowsStmt = this.conn.createStatement();
        maxRowsStmt.setMaxRows(1);
        this.rs = maxRowsStmt.executeQuery("SELECT 1");
    } finally {
        if (maxRowsStmt != null) {
            maxRowsStmt.close();
        }
    }
}
 
Example 12
Project: ProyectoPacientes   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Initializes and returns a Statement with maxRows defined. Tests the SQL_SELECT_LIMIT defined. Executing this
 * query also forces this limit to be defined at session level.
 */
private Statement testBug71396StatementInit(Connection testConn, int maxRows) throws SQLException {
    ResultSet testRS;
    Statement testStmt = testConn.createStatement();

    testStmt.setMaxRows(maxRows);
    // while consulting SQL_SELECT_LIMIT setting also forces limit to be applied into current session
    testRS = testStmt.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    testRS.next();
    assertEquals("Wrong @@SESSION.SQL_SELECT_LIMIT", maxRows, testRS.getInt(1));

    return testStmt;
}
 
Example 13
Project: BibliotecaPS   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Tests fix for BUG#907
 * 
 * @throws Exception
 *             if an error occurs
 */
public void testSetMaxRows() throws Exception {
    Statement maxRowsStmt = null;

    try {
        maxRowsStmt = this.conn.createStatement();
        maxRowsStmt.setMaxRows(1);
        this.rs = maxRowsStmt.executeQuery("SELECT 1");
    } finally {
        if (maxRowsStmt != null) {
            maxRowsStmt.close();
        }
    }
}
 
Example 14
Project: BibliotecaPS   File: StatementRegressionTest.java   Source Code and License Vote up 5 votes
/**
 * Initializes and returns a Statement with maxRows defined. Tests the SQL_SELECT_LIMIT defined. Executing this
 * query also forces this limit to be defined at session level.
 */
private Statement testBug71396StatementInit(Connection testConn, int maxRows) throws SQLException {
    ResultSet testRS;
    Statement testStmt = testConn.createStatement();

    testStmt.setMaxRows(maxRows);
    // while consulting SQL_SELECT_LIMIT setting also forces limit to be applied into current session
    testRS = testStmt.executeQuery("SELECT @@SESSION.SQL_SELECT_LIMIT");
    testRS.next();
    assertEquals("Wrong @@SESSION.SQL_SELECT_LIMIT", maxRows, testRS.getInt(1));

    return testStmt;
}
 
Example 15
Project: calcite-avatica   File: JdbcMeta.java   Source Code and License Vote up 5 votes
/**
 * Sets the provided maximum number of rows on the given statement.
 *
 * @param statement The JDBC Statement to operate on
 * @param maxRowCount The maximum number of rows which should be returned for the query
 */
void setMaxRows(Statement statement, long maxRowCount) throws SQLException {
  // Special handling of maxRowCount as JDBC 0 is unlimited, our meta 0 row
  if (maxRowCount > 0) {
    AvaticaUtils.setLargeMaxRows(statement, maxRowCount);
  } else if (maxRowCount < 0) {
    statement.setMaxRows(0);
  }
}
 
Example 16
Project: calcite-avatica   File: RemoteDriverTest.java   Source Code and License Vote up 5 votes
private void checkStatementExecute(Connection connection,
    boolean prepare, int maxRowCount) throws SQLException {
  final String sql = "select * from (\n"
      + "  values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)";
  final Statement statement;
  final ResultSet resultSet;
  final ParameterMetaData parameterMetaData;
  if (prepare) {
    final PreparedStatement ps = connection.prepareStatement(sql);
    statement = ps;
    ps.setMaxRows(maxRowCount);
    parameterMetaData = ps.getParameterMetaData();
    assertTrue(ps.execute());
    resultSet = ps.getResultSet();
  } else {
    statement = connection.createStatement();
    statement.setMaxRows(maxRowCount);
    parameterMetaData = null;
    assertTrue(statement.execute(sql));
    resultSet = statement.getResultSet();
  }
  if (parameterMetaData != null) {
    assertThat(parameterMetaData.getParameterCount(), equalTo(0));
  }
  final ResultSetMetaData metaData = resultSet.getMetaData();
  assertEquals(2, metaData.getColumnCount());
  assertEquals("C1", metaData.getColumnName(1));
  assertEquals("C2", metaData.getColumnName(2));
  for (int i = 0; i < maxRowCount || (maxRowCount == 0 && i < 3); i++) {
    assertTrue(resultSet.next());
  }
  assertFalse(resultSet.next());
  resultSet.close();
  statement.close();
  connection.close();
}
 
Example 17
Project: incubator-netbeans   File: SQLExecutionHelper.java   Source Code and License Vote up 4 votes
private Statement prepareSQLStatement(Connection conn, String sql) throws SQLException {
    Statement stmt;
    if (sql.startsWith("{")) { // NOI18N
        stmt = useScrollableCursors
                ? conn.prepareCall(sql, resultSetScrollType, ResultSet.CONCUR_READ_ONLY)
                : conn.prepareCall(sql);
    } else if (isSelectStatement(sql)) {
        stmt = useScrollableCursors
                ? conn.createStatement(resultSetScrollType, ResultSet.CONCUR_READ_ONLY)
                : conn.createStatement();

        // set a reasonable fetchsize
        setFetchSize(stmt, 50);

        // hint to only query a certain number of rows -> potentially
        // improve performance for low page numbers
        // only usable for "non-total" resultsets
        try {
            Integer maxRows = 0;
            for (DataViewPageContext pageContext : dataView.getPageContexts()) {
                int currentRows = pageContext.getCurrentPos();
                int pageSize = pageContext.getPageSize();
                if (pageSize <= 0) {
                    maxRows = 0;
                    break;
                } else {
                    maxRows = Math.max(maxRows, currentRows + pageSize);
                }
            }
            stmt.setMaxRows(maxRows);
        } catch (SQLException exc) {
            LOGGER.log(Level.WARNING, "Unable to set Max row count", exc); // NOI18N
            try {
                stmt.setMaxRows(0);
            } catch (SQLException ex) {}
        }
    } else {
        stmt = useScrollableCursors
                ? conn.createStatement(resultSetScrollType, ResultSet.CONCUR_READ_ONLY)
                : conn.createStatement();
    }
    return stmt;
}