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.data2semantics.yasgui.server.db.ConnectionFactory.java

/**
 * Check if database exists//from   w ww.  j ava2 s. co  m
 * @param connect
 * @param dbName
 * @return
 * @throws SQLException
 */
private static boolean databaseExists(Connection connect, String dbName) throws SQLException {
    Statement statement = connect.createStatement();
    ResultSet resultSet = statement.executeQuery(
            "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '" + dbName + "'");
    boolean exists = resultSet.next();
    statement.close();
    resultSet.close();
    return exists;
}

From source file:MainClass.java

public static void readDatabase() {
    String data = "jdbc:derby:presidents";
    try {/*from w  ww.j  a  v  a  2  s. c  om*/
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection(data, "", "");
        Statement st = conn.createStatement();
        ResultSet rec = st.executeQuery("SELECT * FROM contacts ORDER BY name");
        while (rec.next()) {
            System.out.println(
                    rec.getString("name") + "\n" + rec.getString("address1") + "\n" + rec.getString("address2")
                            + "\n" + rec.getString("phone") + "\n" + rec.getString("email") + "\n");
        }
        st.close();
    } catch (Exception e) {
        System.out.println("Error - " + e.toString());
    }
}

From source file:edu.arizona.rice.kew.docsearch.dao.impl.DocumentSearchUtils.java

public static void closeDbObjects(Connection conn, Statement stmt, ResultSet res) {
    try {//from  w  w w  . j ava 2 s . c  om
        if (res != null) {
            res.close();
        }
    }

    catch (Exception ex) {
    }

    try {
        if (stmt != null) {
            stmt.close();
        }
    }

    catch (Exception ex) {
    }

    try {
        if (conn != null) {
            conn.close();
        }
    }

    catch (Exception ex) {
    }
}

From source file:net.sf.jabb.util.db.ConnectionUtility.java

/**
 * Closes database Statement.//from   w  ww  .  j  ava  2 s.co m
 * No exception will be thrown even if occurred during closing,
 * instead, the exception will be logged at warning level.
 * 
 * @param st   the Statement that need to be closed
 */
public static void closeStatement(Statement st) {
    if (st != null) {
        try {
            st.close();
        } catch (Exception e) {
            log.warn("Exception when closing database statement.", e);
        }
    }
}

From source file:localdomain.localhost.CasInitializer.java

public static void closeQuietly(Statement stmt) {
    if (stmt == null)
        return;//from  ww  w . j a v  a2  s  .c o  m
    try {
        stmt.close();
    } catch (Exception e) {
        // ignore
    }
}

From source file:Main.java

/**
 * // w w w. ja  v a  2 s  . co m
 * @param conn
 * @param table
 * @param c_name
 */
public static void updateContentVersion(Connection conn, String table, String c_name) throws Exception {
    ResultSet rs;
    Statement s;
    int v;

    s = conn.createStatement();
    rs = s.executeQuery("SELECT c_version FROM " + table + " WHERE c_name = '" + c_name + "'");
    v = 1;

    if (rs.next()) {
        v = rs.getInt(1);
        v++;
    }

    rs.close();

    s.executeUpdate("UPDATE " + table + " SET c_version = " + v + " WHERE c_name = '" + c_name + "'");
    s.close();
}

From source file:demo.learn.shiro.util.SqlUtil.java

/**
 * Quietly closes JDBC objects (exceptions are swallowed).
 * @param conn {@link Connection}.// w w  w  .  ja  v a 2 s .c  o m
 * @param stmt {@link Statement}.
 * @param rs {@link ResultSet}.
 */
public static void closeQuietly(Connection conn, Statement stmt, ResultSet rs) {
    if (null != rs) {
        try {
            rs.close();
            rs = null;
        } catch (Exception ex) {
        }
    }
    if (null != stmt) {
        try {
            stmt.close();
            stmt = null;
        } catch (Exception ex) {
        }
    }
    if (null != conn) {
        try {
            conn.close();
            conn = null;
        } catch (Exception ex) {
        }
    }
}

From source file:com.l2jfree.gameserver.util.IdFactory.java

private static void removeLeftover() {
    int removed = 0;

    Connection con = null;/*from w w  w.j  a v  a  2  s.  com*/
    try {
        con = L2Database.getConnection();

        final Statement st = con.createStatement();

        for (String query : REMOVE_LEFTOVER_QUERIES) {
            removed += st.executeUpdate(query);
        }

        st.close();
    } catch (SQLException e) {
        _log.warn("", e);
    } finally {
        L2Database.close(con);
    }

    _log.info("IdFactory: Removed " + removed + " leftover entries from database.");
}

From source file:org.jamwiki.db.DatabaseConnection.java

/**
 * Utility method for closing a statement that may or may not be <code>null</code>.
 * The statement SHOULD NOT have already been closed.
 *
 * @param stmt A statement object that is to be closed.  May be <code>null</code>.
 *//*w  ww.  j a v  a  2s  . c o  m*/
protected static void closeStatement(Statement stmt) {
    if (stmt != null) {
        try {
            stmt.close();
        } catch (SQLException e) {
        }
    }
}

From source file:MainClass.java

public static void createDatabase() {
    String data = "jdbc:derby:presidents;create=true";
    try {//from w  w  w .j  a va  2 s. c  om
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
        Connection conn = DriverManager.getConnection(data);
        Statement st = conn.createStatement();
        int result = st.executeUpdate("CREATE TABLE contacts (dex INTEGER NOT NULL PRIMARY KEY "
                + "GENERATED ALWAYS AS identity (START WITH 1, INCREMENT BY 1), "
                + "name VARCHAR(40), address1 VARCHAR(40), address2 VARCHAR(40))");

        result = st.executeUpdate(
                "INSERT INTO contacts (name, address1, address2" + ") VALUES('J','Center', 1 , 'GA')");
        st.close();
    } catch (Exception e) {
        System.out.println("Error - " + e.toString());
    }
}