Example usage for java.sql PreparedStatement close

List of usage examples for java.sql PreparedStatement close

Introduction

In this page you can find the example usage for java.sql PreparedStatement 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.l2jfree.gameserver.model.entity.Couple.java

public void divorce() {
    Connection con = null;//from w  w  w  .jav a2  s.  c o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM couples WHERE id=?");
        statement.setInt(1, _id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: Couple.divorce(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:UpdateMySqlClobServlet.java

public void updateCLOB(Connection conn, String id, String fileContent) throws Exception {
    PreparedStatement pstmt = null;
    try {/* w w  w. java2  s  .  co  m*/
        pstmt = conn.prepareStatement("update dataTable set filebody= ? where id = ?");
        pstmt.setString(1, fileContent);
        pstmt.setString(2, id);
        pstmt.executeUpdate();
    } finally {
        pstmt.close();
    }
}

From source file:com.micromux.cassandra.jdbc.PooledTest.java

@Test
public void twoMillionPreparedStatements() throws Exception {
    CassandraDataSource connectionPoolDataSource = new CassandraDataSource(HOST, PORT, KEYSPACE, USER, PASSWORD,
            VERSION, CONSISTENCY, TRUST_STORE, TRUST_PASS);

    DataSource pooledCassandraDataSource = new PooledCassandraDataSource(connectionPoolDataSource);

    Connection connection = pooledCassandraDataSource.getConnection();
    for (int i = 0; i < 10000; i++) {
        PreparedStatement preparedStatement = connection
                .prepareStatement("SELECT someInt FROM pooled_test WHERE somekey = ?");
        preparedStatement.close();
    }/* w ww  .j av  a  2s.c  o  m*/
    connection.close();
}

From source file:com.spend.spendService.MainPage.java

private void createSearchQueue() {
    try {/*w  w w  .j av  a 2s  .  c  o  m*/
        String st = getSearchQuery();
        String[] selist = getSearchEngineNamesArray();

        for (Object se : selist) {
            String SQLi = "INSERT INTO searchqueue (searchText,searchEngineName, disabled) VALUES (?,?,0)";
            PreparedStatement pstmt = con2.prepareStatement(SQLi);
            pstmt.setString(1, st);
            pstmt.setString(2, se.toString());

            pstmt.executeUpdate();
            pstmt.close();
        }
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:com.springsource.insight.plugin.jdbc.PoolingConnectionTest.java

@Test
public void testOperationCollection() throws SQLException {
    DataSourceConnectionFactory connFactory = new DataSourceConnectionFactory(dataSource);
    ObjectPool connPool = new GenericObjectPool();
    PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, connPool, null, null,
            false, true);/*from w  ww  . j  a  v a2 s . co  m*/
    PoolingDataSource poolDs = new PoolingDataSource(poolFactory.getPool());
    String sql = "select * from appointment where owner = 'Agim'";
    Connection c = poolDs.getConnection();
    try {
        PreparedStatement ps = c.prepareStatement(sql);
        try {
            System.out.println("Prepared statement=" + ps.getClass());

            ResultSet rs = ps.executeQuery();
            rs.close();
        } finally {
            ps.close();
        }
    } finally {
        c.close();
    }

    ArgumentCaptor<Operation> opCaptor = ArgumentCaptor.forClass(Operation.class);
    verify(spiedOperationCollector, times(3)).enter(opCaptor.capture());

    List<Operation> ops = opCaptor.getAllValues();
    assertEquals("Mismatched number of operations", 3, ops.size());
    assertSourceCodeLocation("top-op", ops.get(0),
            "org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper", "prepareStatement");
    assertSourceCodeLocation("mid-op", ops.get(1), "org.apache.commons.dbcp.DelegatingConnection",
            "prepareStatement");
    assertSourceCodeLocation("bottom-op", ops.get(2), "org.hsqldb.jdbc.jdbcConnection", "prepareStatement");
}

From source file:com.cloud.utils.db.TransactionLegacy.java

/**
 * Receives a list of {@link PreparedStatement} and quietly closes all of them, which
 * triggers also closing their dependent objects, like a {@link ResultSet}
 *
 * @param pstmt2Close/*from   w  w w  .jav a2  s.  co m*/
 */
public static void closePstmts(List<PreparedStatement> pstmt2Close) {
    for (PreparedStatement pstmt : pstmt2Close) {
        try {
            if (pstmt != null && !pstmt.isClosed()) {
                pstmt.close();
            }
        } catch (SQLException e) {
            // It's not possible to recover from this and we need to continue closing
            e.printStackTrace();
        }
    }
}

From source file:dk.netarkivet.common.utils.DBUtils.java

/** Update a database by executing all the statements in
 *  the updates String array./*from w w w. j a  v a 2  s. c  om*/
 *  NOTE: this must NOT be used for tables under version control
 *  It must only be used in connection with temporary tables e.g. used
 *  for backup.
 *
 *  NB: the method does not close the provided connection.
 *
 * @param connection connection to the database.
 * @param updates The SQL statements that makes the necessary
 * updates.
 * @throws IOFailure in case of problems in interacting with the database
 */
public static void executeSQL(Connection connection, final String... updates) {
    ArgumentNotValid.checkNotNull(updates, "String... updates");
    PreparedStatement st = null;
    String s = "";

    try {
        connection.setAutoCommit(false);
        for (String update : updates) {
            s = update;
            log.debug("Executing SQL-statement: " + update);
            st = prepareStatement(connection, update);
            st.executeUpdate();
            st.close();
        }
        connection.setAutoCommit(true);
        log.debug("Updated database using updates '" + StringUtils.conjoin(";", updates) + "'.");
    } catch (SQLException e) {
        String msg = "SQL error updating database with sql: " + s + "\n"
                + ExceptionUtils.getSQLExceptionCause(e);
        log.warn(msg, e);
        throw new IOFailure(msg, e);
    } finally {
        rollbackIfNeeded(connection, "updating table with SQL: ", StringUtils.conjoin(";", updates) + "'.");
    }
}

From source file:com.micromux.cassandra.jdbc.SpashScreenTest.java

@Test
public void test() throws Exception {
    String query = "UPDATE Test SET a=?, b=? WHERE KEY=?";
    PreparedStatement statement = con.prepareStatement(query);
    try {/*from ww  w  .  j  a  v a 2  s  .  c o m*/
        statement.setLong(1, 100);
        statement.setLong(2, 1000);
        statement.setString(3, "key0");

        statement.executeUpdate();
    } finally {
        statement.close();
    }
}

From source file:mupomat.controller.ObradaOperater.java

@Override
public void obrisiPostojeci(Operater entitet) throws SQLException {
    try {/*from   ww  w. j  av a 2s .  co  m*/
        Connection veza = MySqlBazaPodataka.getConnection();
        PreparedStatement izraz = veza.prepareStatement("delete from  operater  where sifra=? ");
        izraz.setInt(1, entitet.getSifra());
        izraz.executeUpdate();
        izraz.close();
        veza.close();
    } catch (ClassNotFoundException | IOException e) {
        //  System.out.println(e.getMessage());
        e.printStackTrace();
        return;
    }
}

From source file:net.sf.l2j.gameserver.model.entity.L2JOneoRusEvents.TvT.java

public static void saveData() {
    java.sql.Connection con = null;
    try {//from   ww w. java  2 s  . c o  m
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement;
        statement = con.prepareStatement("Delete from tvt");
        statement.execute();
        statement.close();
        statement = con.prepareStatement(
                "INSERT INTO tvt (eventName, eventDesc, joiningLocation, minlvl, maxlvl, npcId, npcX, npcY, npcZ, npcHeading, rewardId, rewardAmount, teamsCount, joinTime, eventTime, minPlayers, maxPlayers) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        statement.setString(1, _eventName);
        statement.setString(2, _eventDesc);
        statement.setString(3, _joiningLocationName);
        statement.setInt(4, _minlvl);
        statement.setInt(5, _maxlvl);
        statement.setInt(6, _npcId);
        statement.setInt(7, _npcX);
        statement.setInt(8, _npcY);
        statement.setInt(9, _npcZ);
        statement.setInt(10, _npcHeading);
        statement.setInt(11, _rewardId);
        statement.setInt(12, _rewardAmount);
        statement.setInt(13, _teams.size());
        statement.setInt(14, _joinTime);
        statement.setInt(15, _eventTime);
        statement.setInt(16, _minPlayers);
        statement.setInt(17, _maxPlayers);
        statement.execute();
        statement.close();
        statement = con.prepareStatement("Delete from tvt_teams");
        statement.execute();
        statement.close();
        for (String teamName : _teams) {
            int index = _teams.indexOf(teamName);
            if (index == -1)
                return;
            statement = con.prepareStatement(
                    "INSERT INTO tvt_teams (teamId ,teamName, teamX, teamY, teamZ, teamColor) VALUES (?, ?, ?, ?, ?, ?)");
            statement.setInt(1, index);
            statement.setString(2, teamName);
            statement.setInt(3, _teamsX.get(index));
            statement.setInt(4, _teamsY.get(index));
            statement.setInt(5, _teamsZ.get(index));
            statement.setInt(6, _teamColors.get(index));
            statement.execute();
            statement.close();
        }
    } catch (Exception e) {
        _log.error("Exception: TvT.saveData(): " + e.getMessage());
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}