下面列出了java.sql.ResultSet#getStatement ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void close() {
ResultSet savedResultSet = resultSet;
if (savedResultSet != null) {
try {
resultSet = null;
final Statement statement = savedResultSet.getStatement();
savedResultSet.close();
if (statement != null) {
final Connection connection = statement.getConnection();
statement.close();
if (connection != null) {
connection.close();
}
}
} catch (SQLException e) {
// ignore
}
}
}
/**
* Extract the Large Object Input Stream from PostgreSQL
*
* @param resultSet
* the Result Set to extract the blob from
* @param columnIndex
* the index of column
* @return the Large Object Input Stream from PostgreSQL
* @throws SQLException
*/
public static InputStream getPostgreSqlnputStream(ResultSet resultSet,
int columnIndex) throws SQLException {
InputStream in;
Statement statement = resultSet.getStatement();
Connection conn = statement.getConnection();
// Get the Large Object Manager to perform operations with
LargeObjectManager lobj = ((org.postgresql.PGConnection) conn)
.getLargeObjectAPI();
long oid = resultSet.getLong(columnIndex);
if (oid < 1) {
return null;
}
LargeObject obj = lobj.open(oid, LargeObjectManager.READ);
in = obj.getInputStream();
return in;
}
@Test public void testRemoteColumnsMeta() throws Exception {
ConnectionSpec.getDatabaseLock().lock();
try {
// Verify all columns are retrieved, thus that frame-based fetching works correctly
// for columns
int rowCount = 0;
try (AvaticaConnection conn = (AvaticaConnection) DriverManager.getConnection(url)) {
ResultSet rs = conn.getMetaData().getColumns(null, null, null, null);
Statement stmt = rs.getStatement();
while (rs.next()) {
rowCount++;
}
rs.close();
// The implicitly created statement should have been closed
assertTrue(stmt.isClosed());
}
// default fetch size is 100, we are well beyond it
assertTrue(rowCount > 900);
} finally {
ConnectionSpec.getDatabaseLock().unlock();
}
}
protected Object processResult(TicketIF ticket, ResultSet rs) throws Exception {
Collection result;
try {
// Prepare result collection
result = createCollection();
// Zero or more rows expected
while (rs.next()) {
// Add row object to result collection
result.add(stm.readValue(ticket, rs, 0, lookup_identities));
}
} finally {
Statement _stm = rs.getStatement();
// Close result set
rs.close();
rs = null;
// Close statement
if (_stm != null) _stm.close();
}
return result;
}
private void queryQueryData(int i, int queryType) throws SQLException {
String stmt = this.queryFactory.getQuery(queryType, i);
ResultSet rs = null;
int rss = 0;
long start = this.querystats.startQuery();
switch (this.queryAPI) {
case QueryPrms.MYSQL:
case QueryPrms.MYSQLC:
case QueryPrms.ORACLE:
case QueryPrms.GPDB:
case QueryPrms.RTE:
case QueryPrms.GFXD:
rs =
((SQLQueryFactory) this.queryFactory)
.execute(stmt, this.connection);
rss = ((SQLQueryFactory)this.queryFactory).readResultSet(queryType, rs);
break;
default:
unsupported();
}
Statement s = rs.getStatement();
rs.close();
s.close();
this.querystats.endQuery(start, rss, this.histogram);
}
protected Object processResult(TicketIF ticket, ResultSet rs) throws Exception {
try {
// Zero or one row expected
if (rs.next())
// Object was found
return stm.readValue(ticket, rs, 0, lookup_identities);
else
// No match
return null;
// FIXME: Should we complain when more than one object was
// found?
} finally {
Statement _stm = rs.getStatement();
// Close result set
rs.close();
rs = null;
// Close statement
if (_stm != null) _stm.close();
}
}
private void queryQueryData(int i, int queryType) throws SQLException {
String stmt = this.queryFactory.getQuery(queryType, i);
ResultSet rs = null;
int rss = 0;
long start = this.querystats.startQuery();
switch (this.queryAPI) {
case QueryPrms.MYSQL:
case QueryPrms.MYSQLC:
case QueryPrms.ORACLE:
case QueryPrms.GPDB:
case QueryPrms.RTE:
case QueryPrms.GFXD:
rs =
((SQLQueryFactory) this.queryFactory)
.execute(stmt, this.connection);
rss = ((SQLQueryFactory)this.queryFactory).readResultSet(queryType, rs);
break;
default:
unsupported();
}
Statement s = rs.getStatement();
rs.close();
s.close();
this.querystats.endQuery(start, rss, this.histogram);
}
/**
* Registers a StatementInfo for the given ResultSet, returning the id under which it is registered. This should be
* used for metadata ResultSets, which have an implicit statement created.
*/
private int registerMetaStatement(ResultSet rs) throws SQLException {
final int id = statementIdGenerator.getAndIncrement();
StatementInfo statementInfo = new StatementInfo(rs.getStatement());
statementInfo.setResultSet(rs);
statementCache.put(id, statementInfo);
return id;
}
/**
* Returns true if engine is terradata
*
* @param resultSet the result set in use
* @returns true if engine is terradata
* @throws SQLException
*/
private String getDatabaseProductName(ResultSet resultSet) throws SQLException {
Statement statement = resultSet.getStatement();
// happens on Metadata requests, we don' care about the result:
if (statement == null) {
return "unknown";
} else {
Connection connection = statement.getConnection();
return new SqlUtil(connection).getDatabaseProductName();
}
}
private void assertStatementOK(String dsName, Connection conn, Statement s)
throws SQLException {
// checks currently only implemented for embedded
if (usingEmbedded())
{
SecurityCheck.assertSourceSecurity(s, "java.sql.Statement");
}
Connection c1 = s.getConnection();
if (c1 != conn)
{
// with DerbyNetClient and any kind of DataSource, this goes wrong
if (!usingDerbyNetClient() && (dsName.indexOf("DataSource") >= 0))
fail ("incorrect connection object returned for Statement.getConnection()");
}
s.addBatch("insert into intTable values 1");
s.addBatch("insert into intTable values 2,3");
int[] states = s.executeBatch();
if (states[0] != 1)
fail ("invalid update count for first batch statement");
if (states[1] != 2)
fail ("invalid update count for second batch statement");
ResultSet rs = s.executeQuery("VALUES 1");
if (rs.getStatement() != s)
fail ("incorrect Statement object returned for ResultSet.getStatement for " + dsName);
rs.close();
s.close();
}
@Override
public void register(ResultSet resultSet, Statement statement) {
log.tracef( "Registering result set [%s]", resultSet );
if ( statement == null ) {
try {
statement = resultSet.getStatement();
}
catch (SQLException e) {
throw convert( e, "unable to access Statement from ResultSet" );
}
}
if ( statement != null ) {
Set<ResultSet> resultSets = xref.get( statement );
// Keep this at DEBUG level, rather than warn. Numerous connection pool implementations can return a
// proxy/wrapper around the JDBC Statement, causing excessive logging here. See HHH-8210.
if ( log.isDebugEnabled() && resultSets == null ) {
log.debug( "ResultSet statement was not registered (on register)" );
}
if ( resultSets == null || resultSets == Collections.EMPTY_SET ) {
resultSets = new HashSet<ResultSet>();
xref.put( statement, resultSets );
}
resultSets.add( resultSet );
}
else {
unassociatedResultSets.add( resultSet );
}
}
private void assertStatementOK(String dsName, Connection conn, Statement s)
throws SQLException {
// checks currently only implemented for embedded
if (usingEmbedded())
{
SecurityCheck.assertSourceSecurity(s, "java.sql.Statement");
}
Connection c1 = s.getConnection();
if (c1 != conn)
{
// with DerbyNetClient and any kind of DataSource, this goes wrong
if (!usingDerbyNetClient() && (dsName.indexOf("DataSource") >= 0))
fail ("incorrect connection object returned for Statement.getConnection()");
}
s.addBatch("insert into intTable values 1");
s.addBatch("insert into intTable values 2,3");
int[] states = s.executeBatch();
if (states[0] != 1)
fail ("invalid update count for first batch statement");
if (states[1] != 2)
fail ("invalid update count for second batch statement");
ResultSet rs = s.executeQuery("VALUES 1");
if (rs.getStatement() != s)
fail ("incorrect Statement object returned for ResultSet.getStatement for " + dsName);
rs.close();
s.close();
}
/**
* Registers a StatementInfo for the given ResultSet, returning the id under
* which it is registered. This should be used for metadata ResultSets, which
* have an implicit statement created.
*/
private int registerMetaStatement(ResultSet rs) throws SQLException {
final int id = statementIdGenerator.getAndIncrement();
StatementInfo statementInfo = new StatementInfo(rs.getStatement());
statementInfo.setResultSet(rs);
statementCache.put(id, statementInfo);
return id;
}
private void assertStatementOK(String dsName, Connection conn, Statement s)
throws SQLException {
// checks currently only implemented for embedded
if (usingEmbedded())
{
SecurityCheck.assertSourceSecurity(s, "java.sql.Statement");
}
Connection c1 = s.getConnection();
if (c1 != conn)
{
// with DerbyNetClient and any kind of DataSource, this goes wrong
if (!usingDerbyNetClient() && (dsName.indexOf("DataSource") >= 0))
fail ("incorrect connection object returned for Statement.getConnection()");
}
s.addBatch("insert into intTable values 1");
s.addBatch("insert into intTable values 2,3");
int[] states = s.executeBatch();
if (states[0] != 1)
fail ("invalid update count for first batch statement");
if (states[1] != 2)
fail ("invalid update count for second batch statement");
ResultSet rs = s.executeQuery("VALUES 1");
if (rs.getStatement() != s)
fail ("incorrect Statement object returned for ResultSet.getStatement for " + dsName);
rs.close();
s.close();
}
private void assertStatementOK(String dsName, Connection conn, Statement s)
throws SQLException {
// checks currently only implemented for embedded
if (usingEmbedded())
{
SecurityCheck.assertSourceSecurity(s, "java.sql.Statement");
}
Connection c1 = s.getConnection();
if (c1 != conn)
{
// with DerbyNetClient and any kind of DataSource, this goes wrong
if (!usingDerbyNetClient() && (dsName.indexOf("DataSource") >= 0))
fail ("incorrect connection object returned for Statement.getConnection()");
}
s.addBatch("insert into intTable values 1");
s.addBatch("insert into intTable values 2,3");
int[] states = s.executeBatch();
if (states[0] != 1)
fail ("invalid update count for first batch statement");
if (states[1] != 2)
fail ("invalid update count for second batch statement");
ResultSet rs = s.executeQuery("VALUES 1");
if (rs.getStatement() != s)
fail ("incorrect Statement object returned for ResultSet.getStatement for " + dsName);
rs.close();
s.close();
}
/**
* Overload of cleanup( S, R ) when only R is visible to caller.
*
* @param R - JDBC ResultSet object
*/
public static synchronized void cleanup(ResultSet R) {
Statement S = null;
if (R != null) {
try {
S = R.getStatement();
} catch (Throwable t) {
surfaceThrowable("cleanup() called ResultSet.getStatement()", t);
}
cleanup(S, R);
}
}
/**
* Close given result set (if not null) and associated statement.
*
* @param rs ResultSet to close.
*/
public void closeResultSetWithStatement(ResultSet rs) {
if(rs != null) {
try {
Statement stmt = rs.getStatement();
rs.close();
stmt.close();
} catch (SQLException e) {
LOG.info("Ignoring exception: ", e);
}
}
}
private void assertStatementOK(String dsName, Connection conn, Statement s)
throws SQLException {
// checks currently only implemented for embedded
if (usingEmbedded())
{
SecurityCheck.assertSourceSecurity(s, "java.sql.Statement");
}
Connection c1 = s.getConnection();
if (c1 != conn)
{
// with DerbyNetClient and any kind of DataSource, this goes wrong
if (!usingDerbyNetClient() && (dsName.indexOf("DataSource") >= 0))
fail ("incorrect connection object returned for Statement.getConnection()");
}
s.addBatch("insert into intTable values 1");
s.addBatch("insert into intTable values 2,3");
int[] states = s.executeBatch();
if (states[0] != 1)
fail ("invalid update count for first batch statement");
if (states[1] != 2)
fail ("invalid update count for second batch statement");
ResultSet rs = s.executeQuery("VALUES 1");
if (rs.getStatement() != s)
fail ("incorrect Statement object returned for ResultSet.getStatement for " + dsName);
rs.close();
s.close();
}
/**
* Check the held state of a ResultSet by fetching one row, executing a
* commit and then fetching the next. Checks the held state matches the
* behaviour.
*/
private static void checkHeldRS(Connection conn, Statement s, ResultSet rs)
throws SQLException {
// DERBY-1008 - can't run with client
if (!usingDerbyNetClient()) {
if (s.getConnection() != conn)
fail("FAIL - mismatched statement & Connection");
}
if (rs.getStatement() != s) {
// DERBY-1009
fail("FAIL - mismatched statement & ResultSet "
+ " Statement class " + s.getClass()
+ " ResultSet' Statements class "
+ rs.getStatement().getClass());
}
boolean held = (ResultSet.HOLD_CURSORS_OVER_COMMIT == s
.getResultSetHoldability());
rs.next();
assertEquals(0, rs.getInt(1));
conn.commit();
try {
rs.next();
} catch (SQLException sqle) {
boolean ok = !held;
if (ok) {
assertSQLState("XCL16", sqle);
} else {
fail("Held cursor closed on commit");
}
}
rs.close();
conn.commit();
}
void close(ResultSet rs) {
if (rs != null) {
try {
Statement s = rs.getStatement();
rs.close();
close(s);
} catch (SQLException e) {
throw new RuntimeException("close ResultSet fail", e);
}
}
}