/** * Returns a connection to the specified database with autocommit on. * * @param host_name * @param database * @return * @throws SQLException */ public synchronized Connection getConnection(final String host_name, final String database) throws SQLException { data_source.setDatabaseName(database); data_source.setServerName(host_name); return data_source.getConnection(); }
/** * Returns a connection to the specified database with autocommit on. * * @param host_name * @param database * @param user * @param password * @return * @throws SQLException */ public synchronized Connection getConnection(final String host_name, final String database, final String user, final String password) throws SQLException { data_source.setDatabaseName(database); data_source.setServerName(host_name); return data_source.getConnection(user, password); } }
public PostgresTestSupport() throws Exception { ds = createTestDataSource(); conn = ds.getConnection(); try { insert = conn.prepareStatement( "INSERT INTO resources (name, parent, content) VALUES (?, ?, ?) RETURNING oid;"); } finally { if (insert == null) conn.close(); } }
@Override public void close() throws SQLException { final PGSimpleDataSource ds = (PGSimpleDataSource) source; try (Connection c = ds.getConnection()) { try (Statement s = c.createStatement()) { s.execute("DROP SCHEMA \"" + ds.getCurrentSchema() + "\" CASCADE"); } } } };
logger.setUseParentHandlers(false); Connection connection = DataSourceRule .waitForDatabaseAvailable((context) -> context.setConnection(dataSource.getConnection())); this.connections.push(connection); return connection;
public static synchronized void INIT() { PGSimpleDataSource ds = new PGSimpleDataSource(); ds.setUrl(PG_URL); try (final Connection cnx = ds.getConnection(); final Statement st = cnx.createStatement()) { st.execute(TestSuite.getResourceAsString(PG_SQL)); } catch (Exception ex) { throw new RuntimeException(ex); } TestSuite.setupSessionFactoryBuilder(ds); } }
protected DataSource createDataSource(HostAndPort bindingHostAndPort) { PGSimpleDataSource ds = new PGSimpleDataSource(); ds.setPortNumber(bindingHostAndPort.getPort()); ds.setServerName(bindingHostAndPort.getHost()); ds.setUser(config.getUsername()); ds.setPassword(config.getPassword()); ds.setDatabaseName(config.getDb()); ds.setSsl(false); try ( Connection conn = ds.getConnection(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("SELECT 1")) { rs.next(); } catch (SQLException ex) { throw new RuntimeException(ex.getLocalizedMessage()); } return ds; }
private static PGSimpleDataSource createTestDataSource() throws SQLException { PGSimpleDataSource ds = new PGSimpleDataSource(); ds.setServerName("localhost"); ds.setDatabaseName("jdbcstoretest"); ds.setPortNumber(5432); ds.setUser("jdbcstore"); ds.setPassword("jdbcstore"); // Ensure the database is empty try (Connection conn = ds.getConnection()) { try (Statement stmt = conn.createStatement()) { stmt.execute("DROP SCHEMA IF EXISTS public CASCADE;"); stmt.execute("CREATE SCHEMA public;"); stmt.execute("GRANT ALL ON SCHEMA public TO postgres;"); stmt.execute("GRANT ALL ON SCHEMA public TO public;"); stmt.execute("COMMENT ON SCHEMA public IS 'standard public schema';"); } } return ds; }
Connection conn = null; try { conn = dataSource.getConnection(); stat = conn.createStatement(); rs = stat.executeQuery("SELECT 1");
try (Connection c = ds.getConnection()) { try (ResultSet reflect = c.getMetaData().getSchemas(null, schema)) { if (reflect.next()) {