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:PooledConnectionExample.java

private static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
    if (resultSet != null) {
        try {// ww w. j a v a  2  s .c o  m
            resultSet.close();
        } catch (SQLException e) {
        } // nothing we can do
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
        } // nothing we can do
    }
    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
        } // nothing we can do
    }
}

From source file:org.fornax.cartridges.sculptor.framework.util.db.DbUnitConnection.java

private static void close(Connection con, Statement stmt, ResultSet rs) {
    if (rs != null) {
        try {//from w  ww  . j  ava2  s.c  o m
            rs.close();
        } catch (SQLException ignore) {
        }
    }
    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException ignore) {
        }
    }
    if (con != null) {
        try {
            con.close();
        } catch (SQLException ignore) {
        }
    }
}

From source file:com.p6spy.engine.spy.P6TestUtil.java

public static int executeUpdate(Connection connection, String query) throws SQLException {
    Statement stmt = null;
    try {/*  ww w.  j ava 2 s  .  c o m*/
        stmt = connection.createStatement();
        return stmt.executeUpdate(query);
    } finally {
        if (stmt != null)
            try {
                stmt.close();
            } catch (Exception e) {
            }
    }
}

From source file:massbank.svn.MSDBUpdateUtil.java

/**
 *
 */// w w  w. ja  v a2s  . co m
public static boolean restoreDatabase(String dbName, String workCopyPath) {
    Connection con1 = SVNUtils.connectDB("");
    if (con1 == null) {
        return false;
    }
    try {
        Statement stmt = con1.createStatement();
        String sql = "CREATE DATABASE IF NOT EXISTS " + dbName;
        stmt.executeUpdate(sql);
        stmt.close();
        con1.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    String dumpFilePath = workCopyPath + File.separator + "other" + File.separator + "massbank_backup.sql";
    File file = new File(dumpFilePath);
    if (!file.exists()) {
        logger.severe("[SVNService] File not found:" + dumpFilePath);
        return false;
    }
    boolean ret = FileUtil.execSqlFile("localhost", dbName, dumpFilePath);
    if (!ret) {
        return false;
    }
    Connection con2 = SVNUtils.connectDB(dbName);
    if (con2 == null) {
        return false;
    }
    try {
        Statement stmt = con2.createStatement();
        String sql1 = "DROP TABLE IF EXISTS PEAK_HEAP";
        stmt.executeUpdate(sql1);
        String sql2 = "CREATE TABLE PEAK_HEAP(INDEX(ID),INDEX(MZ),INDEX(RELATIVE)) "
                + "ENGINE=HEAP SELECT ID,MZ,RELATIVE FROM PEAK";
        stmt.executeUpdate(sql2);
        stmt.close();
        con2.close();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:com.data2semantics.yasgui.server.db.ConnectionFactory.java

/**
 * Create database/*from   ww  w. j  av  a2  s .  co  m*/
 * @param connect
 * @param dbName
 * @throws SQLException
 */
private static void createDatabase(Connection connect, String dbName) throws SQLException {
    Statement statement = connect.createStatement();
    statement.executeUpdate(
            "CREATE DATABASE `" + dbName + "` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
    statement.close();
}

From source file:com.surveypanel.dao.DBTestCase.java

/**
 * Executes a SQL script./*from w  w w . java  2 s.c  om*/
 *
 * @param con database connection.
 * @param resource an input stream for the script to execute.
 * @param autoreplace automatically replace jiveVersion with ofVersion
 * @throws IOException if an IOException occurs.
 * @throws SQLException if an SQLException occurs.
 */
private static void executeSQLScript(Connection con, InputStream resource) throws IOException, SQLException {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new InputStreamReader(resource));
        boolean done = false;
        while (!done) {
            StringBuilder command = new StringBuilder();
            while (true) {
                String line = in.readLine();
                if (line == null) {
                    done = true;
                    break;
                }
                // Ignore comments and blank lines.
                if (isSQLCommandPart(line)) {
                    command.append(" ").append(line);
                }
                if (line.trim().endsWith(";")) {
                    break;
                }
            }
            // Send command to database.
            if (!done && !command.toString().equals("")) {
                try {
                    String cmdString = command.toString();
                    Statement stmt = con.createStatement();
                    stmt.execute(cmdString);
                    stmt.close();
                } catch (SQLException e) {
                    // Lets show what failed
                    logger.error("SchemaManager: Failed to execute SQL:\n" + command.toString());
                    throw e;
                }
            }
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:net.mlw.vlh.adapter.jdbc.util.JdbcUtil.java

/** Cleans up resources.
 * /* w w w  .j  a v a2  s.co  m*/
 * @param result ResultSet to close.
 * @param statement Statement to close.
 * @param connection Connection to close.
 */
public static void close(ResultSet result, Statement statement, Connection connection) {
    if (result != null)
        try {
            result.close();
        } catch (Exception ignore) {
            LOGGER.info(ignore);
        }
    if (statement != null)
        try {
            statement.close();
        } catch (Exception ignore) {
            LOGGER.info(ignore);
        }
    if (connection != null)
        try {
            connection.close();
        } catch (Exception ignore) {
            LOGGER.info(ignore);
        }
}

From source file:com.invariantproperties.udt.AbstractDatabaseTest.java

/**
 * Load jar file containing our user-defined types.
 * //from  w ww. j  a v  a  2  s  .com
 * @throws SQLException
 */
@BeforeClass
public static void loadJarFile() throws SQLException {
    File file = new File("userdefinedtypes.jar");
    Connection conn = ds.getConnection();
    Statement stmt = conn.createStatement();
    stmt.execute("create schema invariantproperties");
    stmt.execute(String.format("select sqlj.install_jar('file://%s', 'ip_udt', true)", file.getAbsolutePath()));
    stmt.execute("select sqlj.set_classpath('invariantproperties', 'ip_udt')");
    stmt.close();
    conn.close();
}

From source file:com.skilrock.lms.common.db.DBConnect.java

public static void closeStmt(Statement stmt) {
    try {/* w  w  w  .  j a v  a 2s.c om*/
        if (stmt == null)
            logger.info("Statement Already Closed Or Empty");
        else
            stmt.close();
    } catch (SQLException ex) {
        logger.error("Problem While closing Statement");
        ex.printStackTrace();
    }
}

From source file:gridool.db.DBInsertOperation.java

private static void executeDDL(@Nonnull final Connection conn, @Nonnull final String sql) throws SQLException {
    final Statement st = conn.createStatement();
    try {//from   w w w. j a  v a  2s.c  o  m
        st.executeUpdate(sql);
        conn.commit();
    } finally {
        st.close();
    }
}