List of usage examples for java.sql PreparedStatement getResultSet
ResultSet getResultSet() throws SQLException;
ResultSet
object. From source file:org.zaproxy.zap.extension.sse.db.TableEventStream.java
private int executeAndGetSingleIntValue(PreparedStatement stmt) throws SQLException { stmt.execute();/* ww w. j a va2 s . c o m*/ ResultSet rs = stmt.getResultSet(); try { if (rs.next()) { return rs.getInt(1); } return 0; } finally { rs.close(); } }
From source file:org.biokoframework.system.repository.sql.SqlRepository.java
@Override public ArrayList<DE> getAll() { ArrayList<DE> entities = new ArrayList<DE>(); Connection connection = null; PreparedStatement retrieveStatement = null; try {// ww w . ja v a 2 s . com connection = fDbConnector.getConnection(); retrieveStatement = SqlStatementsHelper.preparedRetrieveAllStatement(fEntityClass, fTableName, connection); retrieveStatement.execute(); entities = SqlStatementsHelper.retrieveEntities(retrieveStatement.getResultSet(), fEntityClass, fTranslator, fEntityBuilderService); retrieveStatement.close(); connection.close(); } catch (SQLException exception) { closeDumbSql(connection, retrieveStatement, null); exception.printStackTrace(); } return entities; }
From source file:com.netspective.axiom.sql.Query.java
protected boolean checkRecordExistsIgnoreStatistics(ConnectionContext cc, Object[] overrideParams) throws NamingException, SQLException { if (log.isTraceEnabled()) trace(cc, overrideParams);/*from ww w .j a va 2s .c om*/ try { PreparedStatement stmt = createStatement(cc, overrideParams, false); boolean exists = stmt.execute() && stmt.getResultSet().next(); stmt.close(); return exists; } catch (SQLException e) { log.error(createExceptionMessage(cc, overrideParams), e); throw e; } }
From source file:org.biokoframework.system.repository.sql.SqlRepository.java
@Override public DE retrieve(String anEntityKey) { ArrayList<DE> entities = new ArrayList<DE>(); Connection connection = null; PreparedStatement retrieveStatement = null; try {/* ww w. j av a 2 s . c o m*/ connection = fDbConnector.getConnection(); retrieveStatement = SqlStatementsHelper.preparedRetrieveByIdStatement(fEntityClass, fTableName, connection); retrieveStatement.setObject(1, anEntityKey); retrieveStatement.execute(); entities = SqlStatementsHelper.retrieveEntities(retrieveStatement.getResultSet(), fEntityClass, fTranslator, fEntityBuilderService); } catch (SQLException exception) { exception.printStackTrace(); } finally { closeDumbSql(connection, retrieveStatement, null); } if (entities.isEmpty()) { return null; } else { return entities.get(0); } }
From source file:org.biokoframework.system.repository.sql.SqlRepository.java
@Override public ArrayList<DE> getEntitiesByForeignKey(String foreignKeyName, Object foreignKeyValue) { ArrayList<DE> entities = new ArrayList<DE>(); Connection connection = null; PreparedStatement retrieveStatement = null; try {/* w w w . j a v a 2 s . c o m*/ connection = fDbConnector.getConnection(); retrieveStatement = SqlStatementsHelper.prepareRetrieveByForeignKey(fEntityClass, fTableName, connection, foreignKeyName); retrieveStatement.setObject(1, foreignKeyValue); retrieveStatement.execute(); entities = SqlStatementsHelper.retrieveEntities(retrieveStatement.getResultSet(), fEntityClass, fTranslator, fEntityBuilderService); } catch (SQLException exception) { exception.printStackTrace(); } finally { closeDumbSql(connection, retrieveStatement, null); } return entities; }
From source file:org.biokoframework.system.repository.sql.SqlRepository.java
@Override public DE retrieveByForeignKey(String foreignKeyName, Object foreignKeyValue) { ArrayList<DE> entities = new ArrayList<DE>(); Connection connection = null; PreparedStatement retrieveStatement = null; try {/*from w w w.j av a2 s . c om*/ connection = fDbConnector.getConnection(); retrieveStatement = SqlStatementsHelper.prepareRetrieveOneByForeignKey(fEntityClass, fTableName, connection, foreignKeyName); retrieveStatement.setObject(1, foreignKeyValue); retrieveStatement.execute(); entities = SqlStatementsHelper.retrieveEntities(retrieveStatement.getResultSet(), fEntityClass, fTranslator, fEntityBuilderService); retrieveStatement.close(); connection.close(); } catch (SQLException exception) { LOGGER.error("Retrieve:", exception); exception.printStackTrace(); } finally { closeDumbSql(connection, retrieveStatement, null); } if (entities.isEmpty()) { return null; } else { return entities.get(0); } }
From source file:org.apache.nifi.processors.standard.TestExecuteSQL.java
@Test @SuppressWarnings("unchecked") public void testWithSqlExceptionErrorProcessingResultSet() throws Exception { DBCPService dbcp = mock(DBCPService.class); Connection conn = mock(Connection.class); when(dbcp.getConnection(any(Map.class))).thenReturn(conn); when(dbcp.getIdentifier()).thenReturn("mockdbcp"); PreparedStatement statement = mock(PreparedStatement.class); when(conn.prepareStatement(anyString())).thenReturn(statement); when(statement.execute()).thenReturn(true); ResultSet rs = mock(ResultSet.class); when(statement.getResultSet()).thenReturn(rs); // Throw an exception the first time you access the ResultSet, this is after the flow file to hold the results has been created. when(rs.getMetaData()).thenThrow(SQLException.class); runner.addControllerService("mockdbcp", dbcp, new HashMap<>()); runner.enableControllerService(dbcp); runner.setProperty(ExecuteSQL.DBCP_SERVICE, "mockdbcp"); runner.setIncomingConnection(true);/* w ww .j av a 2 s . c o m*/ runner.enqueue("SELECT 1"); runner.run(); runner.assertTransferCount(ExecuteSQL.REL_FAILURE, 1); runner.assertTransferCount(ExecuteSQL.REL_SUCCESS, 0); }
From source file:com.netspective.axiom.sql.Query.java
protected QueryResultSet executeAndIgnoreStatistics(ConnectionContext cc, Object[] overrideParams, boolean scrollable) throws NamingException, SQLException { if (log.isTraceEnabled()) trace(cc, overrideParams);//from w w w . ja v a2s . c o m try { PreparedStatement stmt = createStatement(cc, overrideParams, scrollable); boolean executeStmtResult = stmt.execute(); return new QueryResultSet(this, cc, executeStmtResult, stmt.getResultSet()); } catch (SQLException e) { log.error(createExceptionMessage(cc, overrideParams), e); throw e; } }
From source file:org.jumpmind.db.sql.JdbcSqlTransaction.java
public int prepareAndExecute(final String sql, final Object... args) { return executeCallback(new IConnectionCallback<Integer>() { public Integer execute(Connection con) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null;//w w w. j ava 2 s. c om try { logSql(sql, args); stmt = con.prepareStatement(sql); if (args != null && args.length > 0) { jdbcSqlTemplate.setValues(stmt, args); } if (stmt.execute()) { rs = stmt.getResultSet(); while (rs.next()) { } } return stmt.getUpdateCount(); } finally { JdbcSqlTemplate.close(rs); JdbcSqlTemplate.close(stmt); } } }); }
From source file:org.jumpmind.db.sql.JdbcSqlTransaction.java
public int prepareAndExecute(final String sql, final Object[] args, final int[] types) { return executeCallback(new IConnectionCallback<Integer>() { public Integer execute(Connection con) throws SQLException { PreparedStatement stmt = null; ResultSet rs = null;/*from w w w . j av a2 s . c om*/ try { logSql(sql, args); stmt = con.prepareStatement(sql); jdbcSqlTemplate.setValues(stmt, args, types, jdbcSqlTemplate.getLobHandler().getDefaultHandler()); if (stmt.execute()) { rs = stmt.getResultSet(); while (rs.next()) { } } return stmt.getUpdateCount(); } finally { JdbcSqlTemplate.close(rs); JdbcSqlTemplate.close(stmt); } } }); }