@Override public void execute(String sql) { try (Connection connection = DriverManager.getConnection(jdbcUrl, jdbcProperties); Statement statement = connection.createStatement()) { statement.execute(sql); } catch (SQLException e) { throw new RuntimeException("Error executing sql:\n" + sql, e); } } }
// assumes... // import java.sql.*; Connection conn=DriverManager.getConnection( "jdbc:ucanaccess://C:/__tmp/test/zzz.accdb"); Statement s = conn.createStatement(); ResultSet rs = s.executeQuery("SELECT [LastName] FROM [Clients]"); while (rs.next()) { System.out.println(rs.getString(1)); }
@Override public void shutdown(DataSource dataSource, String databaseName) { Connection con = null; try { con = dataSource.getConnection(); if (con != null) { con.createStatement().execute("SHUTDOWN"); } } catch (SQLException ex) { logger.info("Could not shut down embedded database", ex); } finally { if (con != null) { try { con.close(); } catch (Throwable ex) { logger.debug("Could not close JDBC Connection on shutdown", ex); } } } }
@Test public void testSelectCount() throws Exception { final ResultSet resultSet = client.createStatement().executeQuery("SELECT COUNT(*) AS cnt FROM druid.foo"); final List<Map<String, Object>> rows = getRows(resultSet); Assert.assertEquals( ImmutableList.of( ImmutableMap.of("cnt", 6L) ), rows ); }
/** * @throws Exception if failed. */ @Test public void testRollbackIgnoredWhenMvccIsDisabled() throws Exception { try (Connection conn = DriverManager.getConnection(URL)) { conn.setAutoCommit(false); conn.createStatement().execute("ROLLBACK"); conn.rollback(); } // assert no exception }
@Test public void testCloseConnectionOnRequest() throws Exception { String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3"; given(this.resultSet.next()).willReturn(false); given(this.connection.createStatement()).willReturn(this.preparedStatement); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query(sql, rcch); verify(this.resultSet).close(); verify(this.preparedStatement).close(); verify(this.connection).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 testBatchUpdateWithNoBatchSupport() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 2"}; given(this.statement.execute(sql[0])).willReturn(false); given(this.statement.getUpdateCount()).willReturn(1, 1); given(this.statement.execute(sql[1])).willReturn(false); mockDatabaseMetaData(false); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); int[] actualRowsAffected = template.batchUpdate(sql); assertTrue("executed 2 updates", actualRowsAffected.length == 2); verify(this.statement, never()).addBatch(anyString()); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); }
/** * @throws Exception If failed. */ @Test public void testSelectAfterAlterTableMultiNode() throws Exception { try (Connection conn2 = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1:" + (ClientConnectorConfiguration.DFLT_PORT + 1))) { try (Statement stmt2 = conn2.createStatement()) { stmt2.executeUpdate("alter table person add age int"); } } checkNewColumn(stmt); }
public static void main(String[] args) throws Exception { Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db"); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists people;"); stat.executeUpdate("create table people (name, occupation);"); conn.setAutoCommit(true); ResultSet rs = stat.executeQuery("select * from people;"); while (rs.next()) { System.out.println("name = " + rs.getString("name")); System.out.println("job = " + rs.getString("occupation"));
Class.forName("com.mysql.jdbc.Driver") ; Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/DBNAME", "usrname", "pswd") ; Statement stmt = conn.createStatement() ; String query = "select columnname from tablename ;" ; ResultSet rs = stmt.executeQuery(query) ;
@Test(timeOut = 10000) public void testQueryPartialCancel() throws Exception { try (Connection connection = createConnection("blackhole", "default"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT count(*) FROM slow_test_table")) { statement.unwrap(PrestoStatement.class).partialCancel(); assertTrue(resultSet.next()); assertEquals(resultSet.getLong(1), 0); } }
/** {@inheritDoc} */ @Override protected void beforeTest() throws Exception { super.beforeTest(); conn = DriverManager.getConnection("jdbc:ignite:thin://127.0.0.1"); stmt = conn.createStatement(); stmt.executeUpdate("CREATE TABLE person (id LONG, name VARCHAR, city_id LONG, PRIMARY KEY (id, city_id))"); stmt.executeUpdate("INSERT INTO person (id, name, city_id) values (1, 'name_1', 11)"); stmt.executeQuery("select * from person"); }
protected QueryResult executeLdapQuery(String query, String name, String password) throws SQLException { try (Connection connection = getLdapConnection(name, password)) { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(query); return QueryResult.forResultSet(rs); } }
@Test public void testSelectBoolean() throws Exception { final ResultSet resultSet = client.createStatement().executeQuery( "SELECT dim2, dim2 IS NULL AS isnull FROM druid.foo LIMIT 1" ); Assert.assertEquals( ImmutableList.of( ImmutableMap.of("dim2", "a", "isnull", false) ), getRows(resultSet) ); }
/** * @throws Exception if failed. */ @Test public void testCommitIgnoredWhenMvccIsDisabled() throws Exception { try (Connection conn = DriverManager.getConnection(URL)) { conn.setAutoCommit(false); conn.createStatement().execute("COMMIT"); conn.commit(); } // assert no exception }
private static String getSessionProperty(Connection connection, String key, String valueType) throws SQLException { try (Statement statement = connection.createStatement()) { ResultSet rs = statement.executeQuery("SHOW SESSION"); while (rs.next()) { if (rs.getString("Name").equals(key)) { return rs.getString(valueType); } } } return null; }
@Test public void testCouldNotClose() throws Exception { SQLException sqlException = new SQLException("bar"); given(this.connection.createStatement()).willReturn(this.statement); given(this.resultSet.next()).willReturn(false); willThrow(sqlException).given(this.resultSet).close(); willThrow(sqlException).given(this.statement).close(); willThrow(sqlException).given(this.connection).close(); RowCountCallbackHandler rcch = new RowCountCallbackHandler(); this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch); verify(this.connection).close(); }
@Test public void testTooManyConnections() throws Exception { final Connection connection1 = DriverManager.getConnection(url); final Statement statement1 = connection1.createStatement(); final Connection connection2 = DriverManager.getConnection(url); final Statement statement2 = connection2.createStatement(); final Connection connection3 = DriverManager.getConnection(url); final Statement statement3 = connection3.createStatement(); expectedException.expect(AvaticaClientRuntimeException.class); expectedException.expectMessage("Too many connections, limit is[3]"); final Connection connection4 = DriverManager.getConnection(url); }
@Test public void testBatchUpdateWithNoBatchSupportAndSelect() throws Exception { final String[] sql = {"UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = 1", "SELECT * FROM NOSUCHTABLE"}; given(this.statement.execute(sql[0])).willReturn(false); given(this.statement.getUpdateCount()).willReturn(1); given(this.statement.execute(sql[1])).willReturn(true); mockDatabaseMetaData(false); given(this.connection.createStatement()).willReturn(this.statement); JdbcTemplate template = new JdbcTemplate(this.dataSource, false); this.thrown.expect(InvalidDataAccessApiUsageException.class); try { template.batchUpdate(sql); } finally { verify(this.statement, never()).addBatch(anyString()); verify(this.statement).close(); verify(this.connection, atLeastOnce()).close(); } }