Example usage for java.sql Statement close

List of usage examples for java.sql Statement close

Introduction

In this page you can find the example usage for java.sql Statement close.

Prototype

void close() throws SQLException;

Source Link

Document

Releases this Statement object's database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed.

Usage

From source file:com.baomidou.mybatisplus.toolkit.IOUtils.java

/**
 * Closes a <code>AutoCloseable</code> unconditionally.
 * <p>/*ww w .  j  a va  2s  . c  om*/
 * Equivalent to {@link Statement#close()}, except any exceptions will be ignored. This is typically used in finally
 * blocks.
 * <p>
 * Example code:
 * <p>
 * <pre>
 * AutoCloseable statement = null;
 * try {
 *    statement = new Connection();
 *    // process close
 *    statement.close();
 * } catch (Exception e) {
 *    // error handling
 * } finally {
 *    IOUtils.closeQuietly(conn);
 * }
 * </pre>
 *
 * @param statement the Connection to close, may be null or already closed
 * @since 2.2
 */
public static void closeQuietly(final Statement statement) {
    if (statement != null) {
        try {
            statement.close();
        } catch (Exception e) {
            logger.error("error close statement", e);
        }
    }
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Close a <code>Statement</code>, avoid closing if null.
 *///from   w  w w . j  ava 2s.  c o m
public static void closeAll(Statement stmt) throws SQLException {
    if (stmt != null) {
        Connection conn = stmt.getConnection();
        stmt.close();
        closeQuietly(conn);
    }
}

From source file:com.nextep.designer.sqlgen.helpers.CaptureHelper.java

/**
 * Close the specified <code>ResultSet</code> and <code>Statement</code> objects if not
 * <code>null</code> while catching for potential {@link SQLException} that could be thrown by
 * the close operation if a database access error occurs.
 * //from   www.j ava  2s. c o m
 * @param rset a {@link ResultSet} object to be closed; may be <code>null</code>
 * @param stmt a {@link Statement} object to be closed; may be <code>null</code>
 */
public static void safeClose(ResultSet rset, Statement stmt) {
    try {
        if (rset != null)
            rset.close();
    } catch (SQLException sqle) {
        LOGGER.warn(SQLGenMessages.getString("captureHelper.closeResultSetFailed"), sqle); //$NON-NLS-1$
    }
    try {
        if (stmt != null)
            stmt.close();
    } catch (SQLException sqle) {
        LOGGER.warn(SQLGenMessages.getString("captureHelper.closeStatementFailed"), sqle); //$NON-NLS-1$
    }
}

From source file:com.zimbra.cs.db.DbPool.java

public static void quietCloseStatement(Statement stmt) {
    try {/*  w ww  .  java2s  .  com*/
        if (stmt != null)
            stmt.close();
    } catch (SQLException e) {
    }
}

From source file:gridool.util.jdbc.JDBCUtils.java

/**
 * Close a <code>ResultSet</code>, avoid closing if null.
 *//*  w  w w  .  j a  va  2s .  c om*/
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:io.undertow.js.test.jdbc.JavascriptJDBCWrapperTestCase.java

@BeforeClass
public static void setup() throws Exception {
    ds = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "user", "password");

    Connection conn = null;/*  ww w  . j a  v  a2 s.  c o m*/
    Statement statement = null;
    try {
        conn = ds.getConnection();
        conn.setAutoCommit(true);
        statement = conn.createStatement();
        statement.executeUpdate("CREATE TABLE PUBLIC.CUSTOMER (" + " id SERIAL NOT NULL,"
                + " first VARCHAR(255) NOT NULL," + " last VARCHAR(255)," + " PRIMARY KEY (id)" + " );");
    } finally {
        if (statement != null) {
            statement.close();
        }
        if (conn != null) {
            conn.close();
        }

    }

    js = UndertowJS.builder().addInjectionProvider(new TestDatabaseInjection())
            .addResources(new ClassPathResourceManager(JavascriptJDBCWrapperTestCase.class.getClassLoader(),
                    JavascriptJDBCWrapperTestCase.class.getPackage()), "jdbc.js")
            .build();
    js.start();
    DefaultServer.setRootHandler(js.getHandler(new HttpHandler() {
        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            exchange.getResponseSender().send("Default Response");
        }
    }));
}

From source file:io.agi.framework.persistence.jdbc.JdbcUtil.java

/**
 * Execute a SQL query that returns data.
 *
 * @param dbUrl/*from w w  w .  j a  v a 2s. com*/
 * @param user
 * @param password
 * @param sql
 * @param cb
 */
public static void ExecuteQuery(String dbUrl, String user, String password, String sql, ResultSetCallback cb) {
    Connection c = null;
    Statement s = null;
    try {

        //            c = DriverManager.getConnection(dbUrl, user, password);
        c = GetConnection(dbUrl, user, password);

        //STEP 4: Execute a query
        s = c.createStatement();
        ResultSet rs = s.executeQuery(sql);

        if (cb != null) {
            cb.onResultSet(rs);
        }

        //STEP 6: Clean-up environment
        rs.close();
        s.close();
        c.close();
    } catch (SQLException se) {
        logger.error(se.toString(), se);
    } catch (Exception e) {
        logger.error(s.toString(), s);
    } finally {
        try {
            if (s != null)
                s.close();
        } catch (SQLException se2) {
            logger.error(se2.toString(), se2);
        }

        try {
            if (c != null)
                c.close();
        } catch (SQLException se) {
            logger.error(se.toString(), se);
        }

    }
}

From source file:edu.ku.brc.dbsupport.SchemaUpdateService.java

/**
 * @param conn/*from   www. j av a  2 s  .com*/
 * @param fileName
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static boolean createDBTablesFromSQLFile(final Connection conn, final String fileName) throws Exception {
    File outFile = XMLHelper.getConfigDir(fileName);
    if (outFile != null && outFile.exists()) {
        StringBuilder sb = new StringBuilder();
        Statement stmt = conn.createStatement();
        List<?> list = FileUtils.readLines(outFile);

        for (String line : (List<String>) list) {
            String tLine = line.trim();
            sb.append(tLine);

            if (tLine.endsWith(";")) {
                System.out.println(sb.toString());
                stmt.executeUpdate(sb.toString());
                sb.setLength(0);
            }
        }
        stmt.close();
        return true;
    }
    return false;
}

From source file:com.uber.hoodie.hive.HoodieHiveClient.java

private static void closeQuietly(ResultSet resultSet, Statement stmt) {
    try {//from w w  w. j a  v  a 2  s .com
        if (stmt != null) {
            stmt.close();
        }
        if (resultSet != null) {
            resultSet.close();
        }
    } catch (SQLException e) {
        LOG.error("Could not close the resultset opened ", e);
    }
}

From source file:massbank.extend.ChemicalFormulaUtils.java

/**
 * CIqXg//w ww  .jav  a2s .c om
 */
public static List<String[]> getIonMassList() throws IOException {
    List<String[]> massList = new ArrayList();
    try {

        Class.forName("com.mysql.jdbc.Driver");
        String conUrl = "jdbc:mysql://localhost/FORMULA_STRUCTURE_RELATION";
        Connection con = DriverManager.getConnection(conUrl, "bird", "bird2006");
        Statement stmt = con.createStatement();
        String sql = "SELECT FORMULA, MASS FROM ION_MASS order by MASS";
        ResultSet rs = stmt.executeQuery(sql);
        while (rs.next()) {
            String formula = rs.getString("FORMULA");
            String mass = rs.getString("MASS");
            massList.add(new String[] { formula, mass });
        }
        rs.close();
        stmt.close();
        con.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return massList;
}