For IntelliJ IDEA and
Android Studio


private void myMethod () {Statement s =
Connection connection;connection.createStatement()
Connection connection;connection.createStatement(resultSetType, resultSetConcurrency)
Connection connection;connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability)
- AI code suggestions by Codota
}
long getLastEventId() throws SQLException { DriverManagerConnectionSource cs = getConnectionSource(); Connection con = cs.getConnection(); Statement statement = con.createStatement(); statement.setMaxRows(1); ResultSet rs = statement.executeQuery("select event_id from logging_event order by event_id desc"); rs.next(); long eventId = rs.getLong(1); rs.close(); statement.close(); return eventId; }
Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery( "SELECT FULL_NAME FROM EMP" ); try { while ( rs.next() ) { System.out.println( "Name: " + rs.getString("FULL_NAME") ); } } finally { try { rs.close(); } catch (Exception ignore) { } } } finally { try { stmt.close(); } catch (Exception ignore) { } }
Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT ID FROM USERS"); ... rs.close(); stmt.close(); conn.close();
@Test public void testAppendMultipleEvents() throws SQLException { for (int i = 0; i < 10; i++) { ILoggingEvent event = createLoggingEvent(); appender.append(event); } Statement stmt = connectionSource.getConnection().createStatement(); ResultSet rs = null; rs = stmt.executeQuery("SELECT * FROM logging_event"); int count = 0; while (rs.next()) { count++; } assertEquals(10, count); rs.close(); stmt.close(); }
private void executeQuery(Connection conn, String expression) throws SQLException { Statement st = null; st = conn.createStatement(); int i = st.executeUpdate(expression); if (i == -1) { throw new IllegalStateException("db error : " + expression); } st.close(); }
public Xid[] recover(int flag) throws XAException { return recover(this.underlyingConnection, flag); }
@Test public void testAppendThrowable() throws SQLException { ILoggingEvent event = createLoggingEvent(); appender.append(event); Statement stmt = connectionSource.getConnection().createStatement(); ResultSet rs = null; rs = stmt.executeQuery("SELECT * FROM LOGGING_EVENT_EXCEPTION WHERE EVENT_ID=1"); rs.next(); String expected = "java.lang.Exception: test Ex"; String firstLine = rs.getString(3); assertTrue("[" + firstLine + "] does not match [" + expected + "]", firstLine.contains(expected)); int i = 0; while (rs.next()) { expected = event.getThrowableProxy().getStackTraceElementProxyArray()[i].toString(); String st = rs.getString(3); assertTrue("[" + st + "] does not match [" + expected + "]", st.contains(expected)); i++; } assertTrue(i != 0); rs.close(); stmt.close(); }
@Test public void testNotTooManyConnectionsWhenTheyAreEmpty() throws Exception { final Connection connection1 = DriverManager.getConnection(url); connection1.createStatement().close(); final Connection connection2 = DriverManager.getConnection(url); connection2.createStatement().close(); final Connection connection3 = DriverManager.getConnection(url); connection3.createStatement().close(); final Connection connection4 = DriverManager.getConnection(url); Assert.assertTrue(true); }
@Test public void testAppendLoggingEvent() throws SQLException { ILoggingEvent event = createLoggingEvent(); appender.append(event); StatusPrinter.print(loggerContext); Statement stmt = connectionSource.getConnection().createStatement(); ResultSet rs = null; rs = stmt.executeQuery("SELECT * FROM logging_event"); if (rs.next()) { assertEquals(event.getTimeStamp(), rs.getLong(DBAppender.TIMESTMP_INDEX)); assertEquals(event.getFormattedMessage(), rs.getString(DBAppender.FORMATTED_MESSAGE_INDEX)); assertEquals(event.getLoggerName(), rs.getString(DBAppender.LOGGER_NAME_INDEX)); assertEquals(event.getLevel().toString(), rs.getString(DBAppender.LEVEL_STRING_INDEX)); assertEquals(event.getThreadName(), rs.getString(DBAppender.THREAD_NAME_INDEX)); assertEquals(DBHelper.computeReferenceMask(event), rs.getShort(DBAppender.REFERENCE_FLAG_INDEX)); assertEquals(String.valueOf(diff), rs.getString(DBAppender.ARG0_INDEX)); StackTraceElement callerData = event.getCallerData()[0]; assertEquals(callerData.getFileName(), rs.getString(DBAppender.CALLER_FILENAME_INDEX)); assertEquals(callerData.getClassName(), rs.getString(DBAppender.CALLER_CLASS_INDEX)); assertEquals(callerData.getMethodName(), rs.getString(DBAppender.CALLER_METHOD_INDEX)); } else { fail("No row was inserted in the database"); } rs.close(); stmt.close(); }
private List<Object> testExpr(List<String> exprs) throws Exception { Class.forName("org.apache.calcite.jdbc.Driver"); Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("expr", new ReflectiveSchema(new ExprDatabase())); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select " + Joiner.on(',').join(exprs) + " \n" + "from \"expr\".\"expressions\" as s\n" + " WHERE s.\"id\" > 0 AND s.\"id\" < 2"); List<Object> result = null; while (resultSet.next()) { if (result != null) { Assert.fail("The query is expected to have only one result row"); } int n = resultSet.getMetaData().getColumnCount(); result = new ArrayList<>(); for (int i = 1 ; i <= n; i++) { result.add(resultSet.getObject(i)); } } resultSet.close(); statement.close(); connection.close(); return result; }
@Test public void testContextInfo() throws SQLException { loggerContext.putProperty("testKey1", "testValue1"); MDC.put("k" + diff, "v" + diff); ILoggingEvent event = createLoggingEvent(); appender.append(event); Statement stmt = connectionSource.getConnection().createStatement(); ResultSet rs = null; rs = stmt.executeQuery("SELECT * FROM LOGGING_EVENT_PROPERTY WHERE EVENT_ID=1"); Map<String, String> map = appender.mergePropertyMaps(event); int i = 0; while (rs.next()) { String key = rs.getString(2); assertEquals(map.get(key), rs.getString(3)); i++; } assertTrue(map.size() != 0); assertEquals(map.size(), i); rs.close(); stmt.close(); }
public insertUser(String name, String email) { Connection conn = null; PreparedStatement stmt = null; try { conn = setupTheDatabaseConnectionSomehow(); stmt = conn.prepareStatement("INSERT INTO person (name, email) values (?, ?)"); stmt.setString(1, name); stmt.setString(2, email); stmt.executeUpdate(); } finally { try { if (stmt != null) { stmt.close(); } } catch (Exception e) { // log this error } try { if (conn != null) { conn.close(); } } catch (Exception e) { // log this error } } }
Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { // Do stuff ... } catch (SQLException ex) { // Exception handling stuff ... } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { /* ignored */} } if (ps != null) { try { ps.close(); } catch (SQLException e) { /* ignored */} } if (conn != null) { try { conn.close(); } catch (SQLException e) { /* ignored */} } }
private long insert(Connection con, List<String> list) throws SQLException { Statement stms = con.createStatement(); for (String sql : list) { stms.addBatch(sql); } stms.executeBatch(); if (this.autocommit == false) { con.commit(); } // stms.clearBatch(); stms.close(); return list.size(); }
private static void testMultiNodeLargeResultset(Connection theCon) throws SQLException { theCon.setAutoCommit(true); System.out.println("testMultiNodeLargeResultset begin"); String sql = "select * from travelrecord limit 100000"; for (int i = 0; i < 100; i++) { Statement stmt = theCon.createStatement(); ResultSet rs = stmt.executeQuery(sql); int count = 0; while (rs.next()) { count++; } rs.close(); stmt.close(); System.out.println("total result " + count); } System.out.println("testMultiNodeLargeResultset end"); }
@Override public boolean testConnection(String schema) throws IOException { boolean isConnected = false; Connection connection = null; Statement statement = null; try { DBHostConfig cfg = getConfig(); connection = DriverManager.getConnection(cfg.getUrl(), cfg.getUser(), cfg.getPassword()); statement = connection.createStatement(); if (connection != null && statement != null) { isConnected = true; } } catch (SQLException e) { e.printStackTrace(); } finally { if (statement != null) { try { statement.close(); } catch (SQLException e) {} } if (connection != null) { try { connection.close(); } catch (SQLException e) {} } } return isConnected; }
results = stmt.executeQuery("SHOW DATABASES"); while (results.next()) { resultsAsList.add(results.getString(1)); if (results != null) { try { results.close(); } catch (SQLException sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx); stmt.close(); } catch (SQLException sqlEx) { AssertionFailedException.shouldNotHappen(sqlEx);
"Connection id %d not found in \"SHOW PROCESSLIST\", assuming 32-bit overflow, using SELECT CONNECTION_ID() instead", threadId)); ResultSet rs = processListStmt.executeQuery("SELECT CONNECTION_ID()"); if (rs.next()) { threadId = rs.getLong(1); processHost = findProcessHost(threadId, processListStmt); processListStmt.close();
@Override public void contextDestroyed(ServletContextEvent servletContextEvent) { try { Statement stat = conn.createStatement(); stat.execute("SHUTDOWN"); stat.close(); } catch (Exception e) { e.printStackTrace(); } try { conn.close(); } catch (Exception e) { e.printStackTrace(); } if (server != null) { server.stop(); server = null; } }
@Override public Iterable<SequenceInformation> extractMetadata(ExtractionContext extractionContext) throws SQLException { final IdentifierHelper identifierHelper = extractionContext.getJdbcEnvironment().getIdentifierHelper(); final Statement statement = extractionContext.getJdbcConnection().createStatement(); try { ResultSet resultSet = statement.executeQuery( "select SEQUENCE_CATALOG, SEQUENCE_SCHEMA, SEQUENCE_NAME, INCREMENT " + "from information_schema.sequences" try { final List<SequenceInformation> sequenceInformationList = new ArrayList<SequenceInformation>(); while ( resultSet.next() ) { sequenceInformationList.add( new SequenceInformationImpl( new QualifiedSequenceName( identifierHelper.toIdentifier( resultSet.getString( "SEQUENCE_CATALOG" ) ), identifierHelper.toIdentifier( resultSet.getString( "SEQUENCE_SCHEMA" ) ), identifierHelper.toIdentifier( resultSet.getString( "SEQUENCE_NAME" ) resultSet.close(); statement.close();