List of usage examples for java.sql Statement close
void close() throws SQLException;
Statement
object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. From source file:com.naver.timetable.jdbc.CubridDataManager.java
public void close(Statement stmt) { try {//from w w w . ja v a 2 s . com if (stmt != null) { stmt.close(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:net.tirasa.ilgrosso.resetdb.Main.java
private static void resetMySQL(final Connection conn) throws Exception { final Statement statement = conn.createStatement(); ResultSet resultSet = statement .executeQuery("SELECT concat('DROP VIEW IF EXISTS ', table_name, ' CASCADE;')" + "FROM information_schema.views;"); final List<String> drops = new ArrayList<String>(); while (resultSet.next()) { drops.add(resultSet.getString(1)); }// w ww . j a v a2s . c om resultSet.close(); for (String drop : drops) { statement.executeUpdate(drop.substring(0, drop.length() - 1)); } drops.clear(); drops.add("SET FOREIGN_KEY_CHECKS = 0;"); resultSet = statement.executeQuery("SELECT concat('DROP TABLE IF EXISTS ', table_name, ' CASCADE;')" + "FROM information_schema.tables;"); while (resultSet.next()) { drops.add(resultSet.getString(1)); } resultSet.close(); drops.add("SET FOREIGN_KEY_CHECKS = 1;"); for (String drop : drops) { statement.executeUpdate(drop.substring(0, drop.length() - 1)); } statement.close(); conn.close(); }
From source file:com.bt.aloha.testing.DbTestCase.java
protected synchronized int update(String expression) throws SQLException { Statement st = connection.createStatement(); try {/*from w ww. j a va2 s. c om*/ return st.executeUpdate(expression); } finally { st.close(); } }
From source file:com.manpowergroup.cn.icloud.util.Case0.java
private void p0(DataSource dataSource, String name) throws SQLException { long startMillis = System.currentTimeMillis(); long startYGC = TestUtil.getYoungGC(); long startFullGC = TestUtil.getFullGC(); for (int i = 0; i < COUNT; ++i) { Connection conn = dataSource.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT 1"); rs.close();//from w w w. j a va2 s .co m stmt.close(); conn.close(); } long millis = System.currentTimeMillis() - startMillis; long ygc = TestUtil.getYoungGC() - startYGC; long fullGC = TestUtil.getFullGC() - startFullGC; System.out.println(name + " millis : " + NumberFormat.getInstance().format(millis) + ", YGC " + ygc + " FGC " + fullGC); }
From source file:hnu.helper.DataBaseConnection.java
/** * Create and Execute an SQL PreparedStatement and returns true if * everything went ok. Can be used for DML and DDL. * Borrows from apache.commons.scaffold.sql.StatementUtils (see * jakarta-commons-sandbox/scaffold)/*from w w w .j a v a2s . c o m*/ */ public static boolean execute(String sql, Object[] parameters) { DataBaseConnection db = new DataBaseConnection(); boolean returnValue = false; Connection conn = db.getDBConnection(); PreparedStatement pStmt = null; Statement stmt = null; log.debug("About to execute: " + sql); try { if (parameters == null || (parameters.length == 0)) { stmt = conn.createStatement(); stmt.executeUpdate(sql); } else { pStmt = conn.prepareStatement(sql); for (int i = 0; i < parameters.length; i++) { log.debug("parameter " + i + ": " + parameters[i]); pStmt.setObject(i + 1, parameters[i]); } pStmt.executeUpdate(); } log.debug(".. executed without exception (hope to return 'true') "); returnValue = true; } catch (SQLException ex) { log.error("Error executing: '" + sql + "'", ex); returnValue = false; } finally { try { if (stmt != null) { stmt.close(); } if (pStmt != null) { pStmt.close(); } if (conn != null) { conn.close(); } } catch (Exception ex) { log.error("Couldn't close the statement or connection.", ex); } } return returnValue; }
From source file:de.langmi.spring.batch.examples.readers.jdbc.JdbcPagingItemReaderTests.java
/** * Shutdown HSQLDB properly./*from w ww . ja v a2 s.c om*/ * * @throws Exception */ @After public void tearDown() throws Exception { Connection conn = dataSource.getConnection(); Statement st = conn.createStatement(); st.execute("SHUTDOWN"); conn.commit(); st.close(); }
From source file:org.silverpeas.components.blankApp.rest.BlankAppRESTTest.java
protected void close(Statement stmt) { if (stmt != null) { try {/*from ww w. j av a 2 s .c om*/ stmt.close(); } catch (SQLException ex) { throw new AssertionError(ex); } } }
From source file:com.predic8.membrane.core.interceptor.statistics.StatisticsJDBCInterceptor.java
private void closeConnection(Statement con) { try {//from w w w. j a va 2s .c o m if (con != null) con.close(); } catch (Exception e) { log.warn("Could not close Statement", e); } }
From source file:com.glaf.report.jxls.ReportManagerImpl.java
public List<?> exec(String sql) throws SQLException { try {//from w w w . jav a 2 s. com sql = evaluate(sql, beans); } catch (Exception ex) { log.error(sql); ex.printStackTrace(); } sql = sql.replaceAll("'", "'"); Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery(sql); RowSetDynaClass rsdc = new RowSetDynaClass(rs, false, true); stmt.close(); rs.close(); return rsdc.getRows(); }
From source file:com.wavemaker.runtime.data.util.JDBCUtils.java
public static Object runSql(String sql[], String url, String username, String password, String driverClassName, Log logger, boolean isDDL) { Connection con = getConnection(url, username, password, driverClassName); try {// w w w . j av a 2 s . c om try { con.setAutoCommit(true); } catch (SQLException ex) { throw new DataServiceRuntimeException(ex); } Statement s = con.createStatement(); try { try { for (String stmt : sql) { if (logger != null && logger.isInfoEnabled()) { logger.info("Running " + stmt); } s.execute(stmt); } if (!isDDL) { ResultSet r = s.getResultSet(); List<Object> rtn = new ArrayList<Object>(); while (r.next()) { // getting only the first col is pretty unuseful rtn.add(r.getObject(1)); } return rtn.toArray(new Object[rtn.size()]); } } catch (Exception ex) { if (logger != null && logger.isErrorEnabled()) { logger.error(ex.getMessage()); } throw ex; } } finally { try { s.close(); } catch (Exception ignore) { } } } catch (Exception ex) { if (ex instanceof RuntimeException) { throw (RuntimeException) ex; } else { throw new DataServiceRuntimeException(ex); } } finally { try { con.close(); } catch (Exception ignore) { } } return null; }