List of usage examples for java.sql Statement getConnection
Connection getConnection() throws SQLException;
Connection
object that produced this Statement
object. From source file:org.kawanfw.sql.servlet.sql.PostgreSqlUtil.java
/** * Extract the Large Object Input Stream from PostgreSQL * /*from ww w . ja va 2 s.c o m*/ * @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); LargeObject obj = lobj.open(oid, LargeObjectManager.READ); in = obj.getInputStream(); return in; }
From source file:gridool.util.jdbc.JDBCUtils.java
/** * Close a <code>Statement</code>, avoid closing if null. */// ww w . j av a2 s . c om public static void closeAll(Statement stmt) throws SQLException { if (stmt != null) { Connection conn = stmt.getConnection(); stmt.close(); closeQuietly(conn); } }
From source file:gridool.util.jdbc.JDBCUtils.java
/** * Close a <code>ResultSet</code>, avoid closing if null. *//*from w ww. j a va 2 s . c o m*/ public static void closeAll(ResultSet rs) throws SQLException { if (rs != null) { Statement stmt = rs.getStatement(); if (stmt != null) { Connection conn = stmt.getConnection(); stmt.close(); closeQuietly(conn); } else { rs.close(); } } }
From source file:org.kuali.rice.kew.test.TestUtilities.java
public static void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String edenSchemaName, final List<String> dontClear) { LOG.info("Clearing tables for schema " + edenSchemaName); if (dataSource == null) { Assert.fail("Null data source given"); }/* w ww . j a v a 2 s .c o m*/ if (edenSchemaName == null || edenSchemaName.equals("")) { Assert.fail("Empty eden schema name given"); } new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { verifyTestEnvironment(dataSource); JdbcTemplate template = new JdbcTemplate(dataSource); return template.execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { List<String> reEnableConstraints = new ArrayList<String>(); ResultSet resultSet = statement.getConnection().getMetaData().getTables(null, edenSchemaName, null, new String[] { "TABLE" }); while (resultSet.next()) { String tableName = resultSet.getString("TABLE_NAME"); if (tableName.startsWith("EN_") && !dontClear.contains(tableName)) { ResultSet keyResultSet = statement.getConnection().getMetaData() .getExportedKeys(null, edenSchemaName, tableName); while (keyResultSet.next()) { String fkName = keyResultSet.getString("FK_NAME"); String fkTableName = keyResultSet.getString("FKTABLE_NAME"); statement.addBatch( "ALTER TABLE " + fkTableName + " DISABLE CONSTRAINT " + fkName); reEnableConstraints .add("ALTER TABLE " + fkTableName + " ENABLE CONSTRAINT " + fkName); } keyResultSet.close(); statement.addBatch("DELETE FROM " + tableName); } } for (String constraint : reEnableConstraints) { statement.addBatch(constraint); } statement.executeBatch(); resultSet.close(); return null; } }); } }); LOG.info("Tables successfully cleared for schema " + edenSchemaName); }
From source file:org.kuali.kra.infrastructure.TestUtilities.java
License:asdf
public static void clearTables(final PlatformTransactionManager transactionManager, final DataSource dataSource, final String edenSchemaName, final List<String> dontClear) { LOG.info("Clearing tables for schema " + edenSchemaName); if (dataSource == null) { Assert.fail("Null data source given"); }//w w w . java 2 s. c o m if (edenSchemaName == null || edenSchemaName.equals("")) { Assert.fail("Empty eden schema name given"); } new TransactionTemplate(transactionManager).execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { verifyTestEnvironment(dataSource); JdbcTemplate template = new JdbcTemplate(dataSource); return template.execute(new StatementCallback() { public Object doInStatement(Statement statement) throws SQLException { List<String> reEnableConstraints = new ArrayList<String>(); ResultSet resultSet = statement.getConnection().getMetaData().getTables(null, edenSchemaName, null, new String[] { "TABLE" }); while (resultSet.next()) { String tableName = resultSet.getString("TABLE_NAME"); if (tableName.startsWith("EN_") && !dontClear.contains(tableName)) { ResultSet keyResultSet = statement.getConnection().getMetaData() .getExportedKeys(null, edenSchemaName, tableName); while (keyResultSet.next()) { String fkName = keyResultSet.getString("FK_NAME"); String fkTableName = keyResultSet.getString("FKTABLE_NAME"); statement.addBatch( "ALTER TABLE " + fkTableName + " DISABLE CONSTRAINT " + fkName); reEnableConstraints .add("ALTER TABLE " + fkTableName + " ENABLE CONSTRAINT " + fkName); } keyResultSet.close(); statement.addBatch("DELETE FROM " + tableName.toUpperCase()); } } for (String constraint : reEnableConstraints) { statement.addBatch(constraint); } statement.executeBatch(); resultSet.close(); return null; } }); } }); LOG.info("Tables successfully cleared for schema " + edenSchemaName); }
From source file:org.sonar.core.persistence.profiling.ProfiledDataSourceTest.java
@Test public void log_sql_requests() throws Exception { BasicDataSource originDataSource = mock(BasicDataSource.class); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sql = "select 'polop' from dual;"; String sqlWithParams = "insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?);"; int param1 = 42; String param2 = "plouf"; Date param3 = new Date(System.currentTimeMillis()); Timestamp param4 = new Timestamp(System.currentTimeMillis()); byte[] param5 = "blob".getBytes("UTF-8"); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); when(preparedStatement.execute()).thenReturn(true); Statement statement = mock(Statement.class); when(connection.createStatement()).thenReturn(statement); when(statement.execute(sql)).thenReturn(true); ProfiledDataSource ds = new ProfiledDataSource(originDataSource); assertThat(ds.getUrl()).isNull();//from www . j a v a 2 s. c o m assertThat(ds.getConnection().getClientInfo()).isNull(); PreparedStatement preparedStatementProxy = ds.getConnection().prepareStatement(sqlWithParams); preparedStatementProxy.setInt(1, param1); preparedStatementProxy.setString(2, param2); preparedStatementProxy.setDate(3, param3); preparedStatementProxy.setTimestamp(4, param4); preparedStatementProxy.setBlob(5, new ByteArrayInputStream(param5)); assertThat(preparedStatementProxy.getConnection()).isNull(); assertThat(preparedStatementProxy.execute()).isTrue(); final Statement statementProxy = ds.getConnection().createStatement(); assertThat(statementProxy.getConnection()).isNull(); assertThat(statementProxy.execute(sql)).isTrue(); assertThat(logTester.logs()).hasSize(2); assertThat(logTester.logs().get(1)).contains(sql); }
From source file:org.sonar.core.persistence.profiling.PersistenceProfilingTest.java
@Test public void should_enable_profiling_when_profiling_is_full() throws Exception { final Logger sqlLogger = (Logger) LoggerFactory.getLogger("sql"); ListAppender<ILoggingEvent> appender = new ListAppender<ILoggingEvent>(); appender.setContext(new ContextBase()); appender.start();/*from w w w . j a v a2s . c om*/ sqlLogger.addAppender(appender); BasicDataSource originDataSource = mock(BasicDataSource.class); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sql = "select 'polop' from dual;"; String sqlWithParams = "insert into polop (col1, col2, col3, col4) values (?, ?, ?, ?, ?);"; int param1 = 42; String param2 = "plouf"; Date param3 = new Date(System.currentTimeMillis()); Timestamp param4 = new Timestamp(System.currentTimeMillis()); byte[] param5 = "blob".getBytes("UTF-8"); PreparedStatement preparedStatement = mock(PreparedStatement.class); when(connection.prepareStatement(sqlWithParams)).thenReturn(preparedStatement); when(preparedStatement.execute()).thenReturn(true); Statement statement = mock(Statement.class); when(connection.createStatement()).thenReturn(statement); when(statement.execute(sql)).thenReturn(true); Settings settings = new Settings(); settings.setProperty(Profiling.CONFIG_PROFILING_LEVEL, Profiling.Level.FULL.toString()); BasicDataSource resultDataSource = PersistenceProfiling.addProfilingIfNeeded(originDataSource, settings); assertThat(resultDataSource).isInstanceOf(ProfilingDataSource.class); assertThat(resultDataSource.getUrl()).isNull(); assertThat(resultDataSource.getConnection().getClientInfo()).isNull(); PreparedStatement preparedStatementProxy = resultDataSource.getConnection().prepareStatement(sqlWithParams); preparedStatementProxy.setInt(1, param1); preparedStatementProxy.setString(2, param2); preparedStatementProxy.setDate(3, param3); preparedStatementProxy.setTimestamp(4, param4); preparedStatementProxy.setBlob(5, new ByteArrayInputStream(param5)); assertThat(preparedStatementProxy.getConnection()).isNull(); assertThat(preparedStatementProxy.execute()).isTrue(); final Statement statementProxy = resultDataSource.getConnection().createStatement(); assertThat(statementProxy.getConnection()).isNull(); assertThat(statementProxy.execute(sql)).isTrue(); assertThat(appender.list).hasSize(2); assertThat(appender.list.get(0).getLevel()).isEqualTo(Level.INFO); assertThat(appender.list.get(0).getFormattedMessage()).contains(sqlWithParams) .contains(" - parameters are: ").contains(Integer.toString(param1)).contains(param2); assertThat(appender.list.get(1).getLevel()).isEqualTo(Level.INFO); assertThat(appender.list.get(1).getFormattedMessage()).contains(sql); }
From source file:org.sonar.db.profiling.ProfiledDataSourceTest.java
@Test public void execute_and_log_statement() throws Exception { logTester.setLevel(LoggerLevel.TRACE); Connection connection = mock(Connection.class); when(originDataSource.getConnection()).thenReturn(connection); String sql = "select from dual"; Statement stmt = mock(Statement.class); when(connection.createStatement()).thenReturn(stmt); when(stmt.execute(sql)).thenReturn(true); ProfiledDataSource underTest = new ProfiledDataSource(originDataSource, ProfiledConnectionInterceptor.INSTANCE); assertThat(underTest.getUrl()).isNull(); assertThat(underTest.getConnection().getClientInfo()).isNull(); final Statement statementProxy = underTest.getConnection().createStatement(); assertThat(statementProxy.getConnection()).isNull(); assertThat(statementProxy.execute(sql)).isTrue(); assertThat(logTester.logs(LoggerLevel.TRACE)).hasSize(1); assertThat(logTester.logs(LoggerLevel.TRACE).get(0)).contains("sql=select from dual"); }
From source file:net.sourceforge.vulcan.spring.jdbc.JdbcSchemaMigrator.java
boolean isTablePresent(final String tableName) { final Boolean b = (Boolean) jdbcTemplate.execute(new StatementCallback() { public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { ResultSet rs = stmt.getConnection().getMetaData().getTables(null, null, null, new String[] { "TABLE" }); while (rs.next()) { if (tableName.equalsIgnoreCase(rs.getString("TABLE_NAME"))) { return true; }/*from w w w .j av a 2 s. c om*/ } return false; } }); return b.booleanValue(); }
From source file:net.sourceforge.vulcan.spring.jdbc.JdbcSchemaMigrator.java
private void executeSql(final Resource resource) throws IOException { final String text = loadResource(resource); log.info("Running migration script " + resource.getFilename()); final String[] commands = text.split(";"); for (String command : commands) { final String trimmed = command.trim(); if (StringUtils.isBlank(trimmed)) { continue; }/* www .jav a 2s . co m*/ jdbcTemplate.execute(new StatementCallback() { public Object doInStatement(Statement stmt) throws SQLException, DataAccessException { stmt.execute(trimmed); final Connection conn = stmt.getConnection(); if (!conn.getAutoCommit()) { conn.commit(); } return null; } }); } }