Java Code Examples for java.sql.PreparedStatement#setFetchSize()

The following examples show how to use java.sql.PreparedStatement#setFetchSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: J2EEDataSourceTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
private PreparedStatement createFloatStatementForStateChecking(
    int[] parameterExpectedValues, int[] PreparedStatementExpectedValues,
    Connection conn, String sql) 
throws SQLException {
    PreparedStatement s = 
        internalCreateFloatStatementForStateChecking(conn, sql);
    s.setCursorName("StokeNewington");
    s.setFetchDirection(ResultSet.FETCH_REVERSE);
    s.setFetchSize(888);
    s.setMaxFieldSize(317);
    s.setMaxRows(91);

    // PreparedStatement Create        
    assertStatementState(
        parameterExpectedValues, PreparedStatementExpectedValues, s);
    return s;
}
 
Example 2
Source File: J2EEDataSourceTest.java    From spliceengine with GNU Affero General Public License v3.0 6 votes vote down vote up
private PreparedStatement createFloatStatementForStateChecking(
        int[] parameterExpectedValues, int[] PreparedStatementExpectedValues,
        Connection conn, String sql)
        throws SQLException {
    PreparedStatement s =
            internalCreateFloatStatementForStateChecking(conn, sql);
    s.setCursorName("StokeNewington");
    s.setFetchDirection(ResultSet.FETCH_REVERSE);
    s.setFetchSize(888);
    s.setMaxFieldSize(317);
    s.setMaxRows(91);

    // PreparedStatement Create        
    assertStatementState(
            parameterExpectedValues, PreparedStatementExpectedValues, s);
    return s;
}
 
Example 3
Source File: TGroupPreparedStatement.java    From tddl with Apache License 2.0 6 votes vote down vote up
private PreparedStatement createPreparedStatementInternal(Connection conn, String sql) throws SQLException {
    PreparedStatement ps;
    if (autoGeneratedKeys != -1) {
        ps = conn.prepareStatement(sql, autoGeneratedKeys);
    } else if (columnIndexes != null) {
        ps = conn.prepareStatement(sql, columnIndexes);
    } else if (columnNames != null) {
        ps = conn.prepareStatement(sql, columnNames);
    } else {
        int resultSetHoldability = this.resultSetHoldability;
        if (resultSetHoldability == -1) // 未调用过setResultSetHoldability
        resultSetHoldability = conn.getHoldability();

        ps = conn.prepareStatement(sql, this.resultSetType, this.resultSetConcurrency, resultSetHoldability);
    }
    setBaseStatement(ps);
    ps.setQueryTimeout(queryTimeout); // 这句可能抛出异常,所以要放到setBaseStatement之后
    ps.setFetchSize(fetchSize);
    ps.setMaxRows(maxRows);
    fillSqlMetaData(ps, sql);
    return ps;
}
 
Example 4
Source File: MysqlIntermediateSession.java    From dekaf with Apache License 2.0 6 votes vote down vote up
@Override
protected void tuneStatementWithFetchSize(final PreparedStatement stmt,
                                          final int packLimit) throws SQLException {
  final boolean rowByRow;
  switch (getFetchStrategy()) {
    case MysqlConsts.FETCH_STRATEGY_ROW:
      rowByRow = true;
      break;
    case MysqlConsts.FETCH_STRATEGY_AUTO:
      rowByRow = packLimit > 0;
      break;
    case MysqlConsts.FETCH_STRATEGY_WHOLE:
    default:
      rowByRow = false;
      break;
  }

  if (rowByRow) {
    stmt.setFetchSize(Integer.MIN_VALUE);
  }
}
 
Example 5
Source File: PhoenixTransactSQL.java    From ambari-metrics with Apache License 2.0 6 votes vote down vote up
private static PreparedStatement setQueryParameters(PreparedStatement stmt,
                                                    Condition condition) throws SQLException {
  int pos = 1;
  //For GET_LATEST_METRIC_SQL_SINGLE_HOST parameters should be set 2 times
  do {
    if (condition.getUuids() != null) {
      for (byte[] uuid : condition.getUuids()) {
        stmt.setBytes(pos++, uuid);
      }
    }
    if (condition.getFetchSize() != null) {
      stmt.setFetchSize(condition.getFetchSize());
      pos++;
    }
  } while (pos < stmt.getParameterMetaData().getParameterCount());

  return stmt;
}
 
Example 6
Source File: MySqlRecordHandler.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
@Override
public PreparedStatement buildSplitSql(Connection jdbcConnection, String catalogName, TableName tableName, Schema schema, Constraints constraints, Split split)
        throws SQLException
{
    PreparedStatement preparedStatement = jdbcSplitQueryBuilder.buildSql(jdbcConnection, null, tableName.getSchemaName(), tableName.getTableName(), schema, constraints, split);

    // Disable fetching all rows.
    preparedStatement.setFetchSize(Integer.MIN_VALUE);

    return preparedStatement;
}
 
Example 7
Source File: DbHelper.java    From myjdbc-rainbow with Apache License 2.0 5 votes vote down vote up
public ResultSetData selectResultSet(String sqlOrID, Class<?> returnClass, Object[] params) throws Throwable {
	String doSql = JDBCUtils.getFinalSql(sqlOrID);
	SqlContext context = SqlCoreHandle.handleRequest(doSql, params).printSqlLog();
	Connection conn = ConnectionManager.getConnection(dataSource);
	PreparedStatement ps = conn.prepareStatement(context.getSql(),ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    if(conn.toString().equalsIgnoreCase("mysql")){
        ps.setFetchSize(Integer.MIN_VALUE);
    }
    ResultSet rs = JDBCUtils.setParamsReturnRS(ps, context.getParamList().toArray());
    return  new ResultSetData(returnClass,rs,ps);
}
 
Example 8
Source File: DefaultPubSubPersistenceProvider.java    From Openfire with Apache License 2.0 5 votes vote down vote up
@Override
public PublishedItem getLastPublishedItem(LeafNode node) {
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    PublishedItem item = null;

    try {
        con = DbConnectionManager.getConnection();
        // Get published items of the specified node
        pstmt = con.prepareStatement(LOAD_LAST_ITEM);
        pstmt.setFetchSize(1);
        pstmt.setMaxRows(1);
        pstmt.setString(1, node.getUniqueIdentifier().getServiceIdentifier().getServiceId());
        pstmt.setString(2, encodeNodeID(node.getNodeID()));
        rs = pstmt.executeQuery();
        // Rebuild loaded published items
        if (rs.next()) {
            String itemID = rs.getString(1);
            JID publisher = new JID(rs.getString(2));
            Date creationDate = new Date(Long.parseLong(rs.getString(3).trim()));
            // Create the item
            item = new PublishedItem(node, publisher, itemID, creationDate);
            // Add the extra fields to the published item
            if (rs.getString(4) != null) {
            	item.setPayloadXML(rs.getString(4));
            }
        }
    }
    catch (Exception sqle) {
        log.error(sqle.getMessage(), sqle);
    }
    finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return item;
}
 
Example 9
Source File: RoundRobinResultIteratorIT.java    From phoenix with Apache License 2.0 5 votes vote down vote up
@Test
public void testUnionAllSelects() throws Exception {
    int insertedRowsA = 10;
    int insertedRowsB = 5;
    int insertedRowsC = 7;
    String baseTableName = generateUniqueName();
    String tableA = "TABLEA" + baseTableName;
    String tableB = "TABLEB" + baseTableName;
    String tableC = "TABLEC" + baseTableName;
    Set<String> keySetA = createTableAndInsertRows(tableA, insertedRowsA, true, true);
    Set<String> keySetB = createTableAndInsertRows(tableB, insertedRowsB, true, true);
    Set<String> keySetC = createTableAndInsertRows(tableC, insertedRowsC, false, true);
    String query = "SELECT K FROM " + tableA + " UNION ALL SELECT K FROM " + tableB + " UNION ALL SELECT K FROM " + tableC;
    Connection conn = getConnection();
    PreparedStatement stmt = conn.prepareStatement(query);
    stmt.setFetchSize(2); // force parallel fetch of scanner cache
    ResultSet rs = stmt.executeQuery();
    int rowsA = 0, rowsB = 0, rowsC = 0;
    while (rs.next()) {
        String key = rs.getString(1);
        if (key.startsWith("TABLEA")) {
            rowsA++;
        } else if (key.startsWith("TABLEB")) {
            rowsB++;
        } else if (key.startsWith("TABLEC")) {
            rowsC++;
        }
        keySetA.remove(key);
        keySetB.remove(key);
        keySetC.remove(key);
    }
    assertEquals("Not all rows of tableA were returned", 0, keySetA.size());
    assertEquals("Not all rows of tableB were returned", 0, keySetB.size());
    assertEquals("Not all rows of tableC were returned", 0, keySetC.size());
    assertEquals("Number of rows retrieved didn't match for tableA", insertedRowsA, rowsA);
    assertEquals("Number of rows retrieved didnt match for tableB", insertedRowsB, rowsB);
    assertEquals("Number of rows retrieved didn't match for tableC", insertedRowsC, rowsC);
}
 
Example 10
Source File: SelectCommand.java    From doma with Apache License 2.0 5 votes vote down vote up
protected void setupOptions(PreparedStatement preparedStatement) throws SQLException {
  if (query.getFetchSize() > 0) {
    preparedStatement.setFetchSize(query.getFetchSize());
  }
  if (query.getMaxRows() > 0) {
    preparedStatement.setMaxRows(query.getMaxRows());
  }
  if (query.getQueryTimeout() > 0) {
    preparedStatement.setQueryTimeout(query.getQueryTimeout());
  }
}
 
Example 11
Source File: Database.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
void setMysqlFetchSize( PreparedStatement ps, int fs, int getMaxRows ) throws SQLException, KettleDatabaseException {
  if ( databaseMeta.isStreamingResults() && getDatabaseMetaData().getDriverMajorVersion() == 3 ) {
    ps.setFetchSize( Integer.MIN_VALUE );
  } else if ( fs <= getMaxRows ) {
    // PDI-11373 do not set fetch size more than max rows can returns
    ps.setFetchSize( fs );
  }
}
 
Example 12
Source File: Loader.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Obtain a <tt>PreparedStatement</tt> with all parameters pre-bound.
 * Bind JDBC-style <tt>?</tt> parameters, named parameters, and
 * limit parameters.
 */
protected final PreparedStatement prepareQueryStatement(
		String sql,
		final QueryParameters queryParameters,
		final LimitHandler limitHandler,
		final boolean scroll,
		final SharedSessionContractImplementor session) throws SQLException, HibernateException {
	final Dialect dialect = getFactory().getDialect();
	final RowSelection selection = queryParameters.getRowSelection();
	final boolean useLimit = LimitHelper.useLimit( limitHandler, selection );
	final boolean hasFirstRow = LimitHelper.hasFirstRow( selection );
	final boolean useLimitOffset = hasFirstRow && useLimit && limitHandler.supportsLimitOffset();
	final boolean callable = queryParameters.isCallable();
	final ScrollMode scrollMode = getScrollMode( scroll, hasFirstRow, useLimitOffset, queryParameters );

	PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareQueryStatement(
			sql,
			callable,
			scrollMode
	);

	try {

		int col = 1;
		//TODO: can we limit stored procedures ?!
		col += limitHandler.bindLimitParametersAtStartOfQuery( selection, st, col );

		if ( callable ) {
			col = dialect.registerResultSetOutParameter( (CallableStatement) st, col );
		}

		col += bindParameterValues( st, queryParameters, col, session );

		col += limitHandler.bindLimitParametersAtEndOfQuery( selection, st, col );

		limitHandler.setMaxRows( selection, st );

		if ( selection != null ) {
			if ( selection.getTimeout() != null ) {
				st.setQueryTimeout( selection.getTimeout() );
			}
			if ( selection.getFetchSize() != null ) {
				st.setFetchSize( selection.getFetchSize() );
			}
		}

		// handle lock timeout...
		LockOptions lockOptions = queryParameters.getLockOptions();
		if ( lockOptions != null ) {
			if ( lockOptions.getTimeOut() != LockOptions.WAIT_FOREVER ) {
				if ( !dialect.supportsLockTimeouts() ) {
					if ( LOG.isDebugEnabled() ) {
						LOG.debugf(
								"Lock timeout [%s] requested but dialect reported to not support lock timeouts",
								lockOptions.getTimeOut()
						);
					}
				}
				else if ( dialect.isLockTimeoutParameterized() ) {
					st.setInt( col++, lockOptions.getTimeOut() );
				}
			}
		}

		if ( LOG.isTraceEnabled() ) {
			LOG.tracev( "Bound [{0}] parameters total", col );
		}
	}
	catch (SQLException | HibernateException e) {
		session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release( st );
		session.getJdbcCoordinator().afterStatementExecution();
		throw e;
	}

	return st;
}
 
Example 13
Source File: Database.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public ResultSet openQuery( PreparedStatement ps, RowMetaInterface params, Object[] data )
  throws KettleDatabaseException {
  ResultSet res;

  // Create a Statement
  try {
    log.snap( Metrics.METRIC_DATABASE_OPEN_QUERY_START, databaseMeta.getName() );

    log.snap( Metrics.METRIC_DATABASE_SQL_VALUES_START, databaseMeta.getName() );
    setValues( params, data, ps ); // set the parameters!
    log.snap( Metrics.METRIC_DATABASE_SQL_VALUES_STOP, databaseMeta.getName() );

    if ( canWeSetFetchSize( ps ) ) {
      int maxRows = ps.getMaxRows();
      int fs = Const.FETCH_SIZE <= maxRows ? maxRows : Const.FETCH_SIZE;
      // mysql have some restriction on fetch size assignment
      if ( databaseMeta.isMySQLVariant() ) {
        setMysqlFetchSize( ps, fs, maxRows );
      } else {
        // other databases seems not.
        ps.setFetchSize( fs );
      }

      ps.setFetchDirection( ResultSet.FETCH_FORWARD );
    }

    if ( rowlimit > 0 && databaseMeta.supportsSetMaxRows() ) {
      ps.setMaxRows( rowlimit );
    }

    log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_START, databaseMeta.getName() );
    res = ps.executeQuery();
    log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_STOP, databaseMeta.getName() );

    // MySQL Hack only. It seems too much for the cursor type of operation on
    // MySQL, to have another cursor opened
    // to get the length of a String field. So, on MySQL, we ignore the length
    // of Strings in result rows.
    //
    log.snap( Metrics.METRIC_DATABASE_GET_ROW_META_START, databaseMeta.getName() );
    rowMeta = getRowInfo( res.getMetaData(), databaseMeta.isMySQLVariant(), false );
    log.snap( Metrics.METRIC_DATABASE_GET_ROW_META_STOP, databaseMeta.getName() );
  } catch ( SQLException ex ) {
    throw new KettleDatabaseException( "ERROR executing query", ex );
  } catch ( Exception e ) {
    throw new KettleDatabaseException( "ERROR executing query", e );
  } finally {
    log.snap( Metrics.METRIC_DATABASE_OPEN_QUERY_STOP, databaseMeta.getName() );
  }

  return res;
}
 
Example 14
Source File: StatementPreparerImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private void setStatementFetchSize(PreparedStatement statement) throws SQLException {
	if ( settings().getJdbcFetchSize() != null ) {
		statement.setFetchSize( settings().getJdbcFetchSize() );
	}
}
 
Example 15
Source File: MetricsDbBase.java    From mysql_perf_analyzer with Apache License 2.0 4 votes vote down vote up
/** 
  * Retrieve user defined merics
  * @param metrics
  * @param dbid
  * @param startDate
  * @param endDate
  * @return
  */
 public ResultList retrieveUDMMetrics(String metric, int dbid, long startDate, long endDate)
 {		  
int[] snaps = this.getSnapshostRange(startDate, endDate) ;
if(snaps == null)return null;//no data

//later, connection pooling
ResultList rList = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;

int code = 0;
if(this.metricCodeMap.containsKey(metric))
{
	code =	this.metricCodeMap.get(metric);
}else
{
	logger.warning("Failed to find metrics code for "+metric+", "+this.metricCodeMap);
	return null;
}

String sql = "select SNAP_ID, TS, METRIC_ID, VALUE from METRIC_GENERIC where dbid=? and snap_id between ? and ? and METRIC_ID=? order by dbid, METRIC_ID, snap_id";
//String sql = "select * from "+metricGroupName+" where dbid=?";
logger.log(Level.INFO, "To retrieve "+metric+", "+ code+" on db "+dbid+" with time range ("+startDate+", "+endDate+"), using "+sql);
try
{
  conn = createConnection(true);
  stmt = conn.prepareStatement(sql);
  stmt.setFetchSize(1000);
  //stmt.setMaxRows(5000);
  stmt.setInt(1, dbid);
  stmt.setInt(2, snaps[0]);
  stmt.setInt(3,  snaps[1]);
  stmt.setLong(4,  code);
  rs = stmt.executeQuery();
  if(rs==null)return rList;
  rList = new ResultList();
  //java.sql.ResultSetMetaData meta =  rs.getMetaData();
  ColumnDescriptor desc = new ColumnDescriptor();
  desc.addColumn("SNAP_ID", true, 1);
  desc.addColumn("TS", true, 2);
  desc.addColumn(metric, true, 3);
  
  rList.setColumnDescriptor(desc);
  int rowCnt = 0;
  //List<ColumnInfo> cols = desc.getColumns();
  while(rs.next())
  {
		//logger.info(new java.util.Date()+": process "+rowCnt+" rows");
		ResultRow row = new ResultRow();
		row.setColumnDescriptor(desc);
		java.util.ArrayList<String> cols2 = new java.util.ArrayList<String>(3);
		cols2.add(rs.getString(1));
		cols2.add(rs.getString(2));
		cols2.add(rs.getString(4));
		row.setColumns(cols2);
		rList.addRow(row);
		rowCnt++;
		if(rowCnt>=5000)break;
	}
	logger.info(new java.util.Date()+": Process results done: "+rList.getRows().size());
}catch(Exception ex)
{
	logger.log(Level.SEVERE, "Failed to retrieve UDM "+metric+" for db "+dbid+" with time range ("+startDate+", "+endDate+")", ex);
}finally
{
     DBUtils.close(stmt);	
  DBUtils.close(conn);	
}
return rList;
 }
 
Example 16
Source File: MetricsDbBase.java    From mysql_perf_analyzer with Apache License 2.0 4 votes vote down vote up
public ResultList retrieveMetrics(String metricGroupName, Metric[] metrics, boolean hasKeyColumn, int dbid, long startDate, long endDate, boolean agg)
 {
String[] ms = new String[metrics.length];
for(int i=0; i<metrics.length; i++)ms[i] = metrics[i].getName();
if(!agg)return retrieveMetrics(metricGroupName, ms, hasKeyColumn, dbid,startDate,endDate);
int[] snaps = this.getSnapshostRange(startDate, endDate) ;
if(snaps == null)return null;//no data

if(metrics==null||metrics.length==0)//not specify the metrics? Get all
   	return retrieveMetrics( metricGroupName,  dbid,  startDate,  endDate);
   //later, connection pooling
ResultList rList = null;
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
StringBuilder sb = new StringBuilder();//build select list
StringBuilder grpBy = new StringBuilder();//build select list

sb.append("SNAP_ID");
sb.append(", TS");
for(Metric me: metrics)
{
	if(me.isIncremental())
		sb.append(", sum(");
	else
		sb.append(", avg(");
	sb.append(me.getName())
	  .append(") ")
	  .append (me.getName());
}

String sql = "select "+sb.toString()+" from "+metricGroupName+" where dbid=? and snap_id between ? and ? group by snap_id, ts order by snap_id";
//String sql = "select * from "+metricGroupName+" where dbid=?";
logger.log(Level.INFO, "To retrieve metrics "+metricGroupName+", metrics ("+sb.toString()+") for db "+dbid+" with time range ("+startDate+", "+endDate+"), snap ("+snaps[0]+", "+snaps[1]+")");
try
{
  conn = createConnection(true);
  stmt = conn.prepareStatement(sql);
  stmt.setFetchSize(1000);
  //stmt.setMaxRows(5000);
  stmt.setInt(1, dbid);
  stmt.setInt(2, snaps[0]);
  stmt.setInt(3,  snaps[1]);
  rs = stmt.executeQuery();
  rList = ResultListUtil.fromSqlResultSet(rs, 5000);
}catch(Exception ex)
{
	logger.log(Level.SEVERE, "Failed to retrieve metrics "+metricGroupName+" for db "+dbid+" with time range ("+startDate+", "+endDate+")", ex);
}finally
{
     DBUtils.close(stmt);	
  DBUtils.close(conn);	
}
return rList;
 }
 
Example 17
Source File: JdbcExtractor.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Execute query using JDBC PreparedStatement to pass query parameters Set
 * fetch size
 *
 * @param cmds commands - query, fetch size, query parameters
 * @return JDBC ResultSet
 * @throws Exception
 */
private CommandOutput<?, ?> executePreparedSql(List<Command> cmds) {
  String query = null;
  List<String> queryParameters = null;
  int fetchSize = 0;

  for (Command cmd : cmds) {
    if (cmd instanceof JdbcCommand) {
      JdbcCommandType type = (JdbcCommandType) cmd.getCommandType();
      switch (type) {
        case QUERY:
          query = cmd.getParams().get(0);
          break;
        case QUERYPARAMS:
          queryParameters = cmd.getParams();
          break;
        case FETCHSIZE:
          fetchSize = Integer.parseInt(cmd.getParams().get(0));
          break;
        default:
          this.log.error("Command " + type.toString() + " not recognized");
          break;
      }
    }
  }

  this.log.info("Executing query:" + query);
  ResultSet resultSet = null;
  try {
    this.jdbcSource = createJdbcSource();
    if (this.dataConnection == null) {
      this.dataConnection = this.jdbcSource.getConnection();
    }

    PreparedStatement statement =
        this.dataConnection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

    int parameterPosition = 1;
    if (queryParameters != null && queryParameters.size() > 0) {
      for (String parameter : queryParameters) {
        statement.setString(parameterPosition, parameter);
        parameterPosition++;
      }
    }
    if (fetchSize != 0) {
      statement.setFetchSize(fetchSize);
    }
    final boolean status = statement.execute();
    if (status == false) {
      this.log.error("Failed to execute sql:" + query);
    }
    resultSet = statement.getResultSet();

  } catch (Exception e) {
    this.log.error("Failed to execute sql:" + query + " ;error-" + e.getMessage(), e);
  }

  CommandOutput<JdbcCommand, ResultSet> output = new JdbcCommandOutput();
  output.put((JdbcCommand) cmds.get(0), resultSet);
  return output;
}
 
Example 18
Source File: StatementsTest.java    From r-course with MIT License 4 votes vote down vote up
public void testRowFetch() throws Exception {
    if (versionMeetsMinimum(5, 0, 5)) {
        createTable("testRowFetch", "(field1 int)");

        this.stmt.executeUpdate("INSERT INTO testRowFetch VALUES (1)");

        Connection fetchConn = null;

        Properties props = new Properties();
        props.setProperty("useCursorFetch", "true");

        try {
            fetchConn = getConnectionWithProps(props);

            PreparedStatement fetchStmt = fetchConn.prepareStatement("SELECT field1 FROM testRowFetch WHERE field1=1");
            fetchStmt.setFetchSize(10);
            this.rs = fetchStmt.executeQuery();
            assertTrue(this.rs.next());

            this.stmt.executeUpdate("INSERT INTO testRowFetch VALUES (2), (3)");

            fetchStmt = fetchConn.prepareStatement("SELECT field1 FROM testRowFetch ORDER BY field1");
            fetchStmt.setFetchSize(1);
            this.rs = fetchStmt.executeQuery();

            assertTrue(this.rs.next());
            assertEquals(1, this.rs.getInt(1));
            assertTrue(this.rs.next());
            assertEquals(2, this.rs.getInt(1));
            assertTrue(this.rs.next());
            assertEquals(3, this.rs.getInt(1));
            assertEquals(false, this.rs.next());

            this.rs = fetchStmt.executeQuery();
        } finally {
            if (fetchConn != null) {
                fetchConn.close();
            }
        }

    }
}
 
Example 19
Source File: Database.java    From hop with Apache License 2.0 4 votes vote down vote up
public ResultSet openQuery( PreparedStatement ps, IRowMeta params, Object[] data )
  throws HopDatabaseException {
  ResultSet res;

  // Create a Statement
  try {
    log.snap( Metrics.METRIC_DATABASE_OPEN_QUERY_START, databaseMeta.getName() );

    log.snap( Metrics.METRIC_DATABASE_SQL_VALUES_START, databaseMeta.getName() );
    setValues( params, data, ps ); // set the parameters!
    log.snap( Metrics.METRIC_DATABASE_SQL_VALUES_STOP, databaseMeta.getName() );

    if ( canWeSetFetchSize( ps ) ) {
      int maxRows = ps.getMaxRows();
      int fs = Const.FETCH_SIZE <= maxRows ? maxRows : Const.FETCH_SIZE;
      // mysql have some restriction on fetch size assignment
      if ( databaseMeta.isMySqlVariant() ) {
        setMysqlFetchSize( ps, fs, maxRows );
      } else {
        // other databases seems not.
        ps.setFetchSize( fs );
      }

      ps.setFetchDirection( ResultSet.FETCH_FORWARD );
    }

    if ( rowlimit > 0 && databaseMeta.supportsSetMaxRows() ) {
      ps.setMaxRows( rowlimit );
    }

    log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_START, databaseMeta.getName() );
    res = ps.executeQuery();
    log.snap( Metrics.METRIC_DATABASE_EXECUTE_SQL_STOP, databaseMeta.getName() );

    // MySQL Hack only. It seems too much for the cursor type of operation on
    // MySQL, to have another cursor opened
    // to get the length of a String field. So, on MySQL, we ignore the length
    // of Strings in result rows.
    //
    log.snap( Metrics.METRIC_DATABASE_GET_ROW_META_START, databaseMeta.getName() );
    rowMeta = getRowInfo( res.getMetaData(), databaseMeta.isMySqlVariant(), false );
    log.snap( Metrics.METRIC_DATABASE_GET_ROW_META_STOP, databaseMeta.getName() );
  } catch ( SQLException ex ) {
    throw new HopDatabaseException( "ERROR executing query", ex );
  } catch ( Exception e ) {
    throw new HopDatabaseException( "ERROR executing query", e );
  } finally {
    log.snap( Metrics.METRIC_DATABASE_OPEN_QUERY_STOP, databaseMeta.getName() );
  }

  return res;
}
 
Example 20
Source File: PostgreSQLJdbcDumper.java    From shardingsphere with Apache License 2.0 4 votes vote down vote up
@Override
protected PreparedStatement createPreparedStatement(final Connection conn, final String sql) throws SQLException {
    PreparedStatement result = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    result.setFetchSize(1);
    return result;
}