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.Hero.java

private void deleteSkillsInDb() {
    Connection con = null;/* www .  java 2  s. co  m*/

    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement(DELETE_SKILLS);
        statement.execute();
        statement.close();
    } catch (SQLException e) {
        _log.error(e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.espertech.esper.epl.db.DatabasePollingViewableFactory.java

private static QueryMetaData getPreparedStmtMetadata(Connection connection, String[] parameters,
        String preparedStatementText, ColumnSettings metadataSetting) throws ExprValidationException {
    PreparedStatement prepared;
    try {/*  w ww.j  a v a  2  s . co  m*/
        if (log.isInfoEnabled()) {
            log.info(".getPreparedStmtMetadata Preparing statement '" + preparedStatementText + "'");
        }
        prepared = connection.prepareStatement(preparedStatementText);
    } catch (SQLException ex) {
        String text = "Error preparing statement '" + preparedStatementText + '\'';
        log.error(text, ex);
        throw new ExprValidationException(text + ", reason: " + ex.getMessage());
    }

    // Interrogate prepared statement - parameters and result
    List<String> inputParameters = new LinkedList<String>();
    try {
        ParameterMetaData parameterMetaData = prepared.getParameterMetaData();
        inputParameters.addAll(Arrays.asList(parameters).subList(0, parameterMetaData.getParameterCount()));
    } catch (Exception ex) {
        try {
            prepared.close();
        } catch (SQLException e) {
            // don't handle
        }
        String text = "Error obtaining parameter metadata from prepared statement, consider turning off metadata interrogation via configuration, for statement '"
                + preparedStatementText + '\'';
        log.error(text, ex);
        throw new ExprValidationException(text + ", please check the statement, reason: " + ex.getMessage());
    }

    Map<String, DBOutputTypeDesc> outputProperties;
    try {
        outputProperties = compileResultMetaData(prepared.getMetaData(), metadataSetting);
    } catch (SQLException ex) {
        try {
            prepared.close();
        } catch (SQLException e) {
            // don't handle
        }
        String text = "Error in statement '" + preparedStatementText
                + "', failed to obtain result metadata, consider turning off metadata interrogation via configuration";
        log.error(text, ex);
        throw new ExprValidationException(text + ", please check the statement, reason: " + ex.getMessage());
    }

    if (log.isDebugEnabled()) {
        log.debug(".createDBEventStream in=" + inputParameters.toString() + " out="
                + outputProperties.toString());
    }

    // Close statement
    try {
        prepared.close();
    } catch (SQLException e) {
        String text = "Error closing prepared statement";
        log.error(text, e);
        throw new ExprValidationException(text + ", reason: " + e.getMessage());
    }

    return new QueryMetaData(inputParameters, outputProperties);
}

From source file:com.dbsvg.models.SQLiteInternalDataDAO.java

/**
 * I chose to delete becuase it's easy enough to put it in again, and maybe
 * they deleted it because the information was sensitive.
 * //from ww  w  .  java  2  s  . co m
 * @param cw
 * @param conn
 */
public void deleteConnectionWrapper(String id, Connection conn) throws SQLException {
    PreparedStatement ps = conn.prepareStatement(DELETE_CONNECTION_SQL);
    ps.setString(1, id);
    ps.executeUpdate();

    ps.close();
}

From source file:com.mechanicshop.service.SearchServiceImpl.java

@Override
public void updateMedia2(String text) {
    String sql = "UPDATE parameters SET DefaultMedia2 = ?";
    Connection conn = null;//w  w  w . j  av  a2s  .  co m

    try {
        conn = dataSource.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, text);
        ps.executeUpdate();

        ps.close();

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }
}

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

public void marry() {
    java.sql.Connection con = null;
    try {/* ww  w  .j  a  v  a 2s. c  o m*/
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement;
        statement = con.prepareStatement("UPDATE couples set maried = ?, weddingDate = ? where id = ?");
        statement.setBoolean(1, true);
        _weddingDate = Calendar.getInstance();
        statement.setLong(2, _weddingDate.getTimeInMillis());
        statement.setInt(3, _Id);
        statement.execute();
        statement.close();
        _maried = true;
    } catch (Exception e) {
        _log.error("", e);
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.mechanicshop.service.SearchServiceImpl.java

@Override
public void updateMedia1(String text) {
    String sql = "UPDATE parameters SET DefaultMedia1 = ?";
    Connection conn = null;/* w  ww  .ja  v a2s .co  m*/

    try {
        conn = dataSource.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, text);
        ps.executeUpdate();

        ps.close();

    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
            }
        }
    }

}

From source file:jobimtext.thesaurus.distributional.DatabaseThesaurusDatastructure.java

public Double getKeyValuesScore(String key, String val) {
    double score = 0.0;
    String sql = "SELECT sig FROM " + tableOrder1 + " WHERE word = ? and feature = ?";
    PreparedStatement ps;
    try {/*from w  w w. j a  va 2s  .  co  m*/
        ps = getDatabaseConnection().getConnection().prepareStatement(sql);
        ps.setString(1, key);
        ps.setString(2, val);
        ResultSet set = ps.executeQuery();
        if (set.next()) {
            score = set.getDouble("sig");
        }
        ps.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return score;
}

From source file:ca.phon.ipadictionary.impl.DatabaseDictionary.java

@Override
public String[] lookup(String ortho) throws IPADictionaryExecption {
    Connection conn = IPADatabaseManager.getInstance().getConnection();
    List<String> retVal = new ArrayList<String>();

    if (conn != null) {
        ortho = StringUtils.strip(ortho, "?!\"'.\\/@&$()^%#*");
        String qSt = "SELECT * FROM transcript WHERE orthography = ? AND langId = ?";
        try {//  ww w  .j a  v a  2 s . c  o m
            PreparedStatement pSt = conn.prepareStatement(qSt);
            pSt.setString(1, ortho.toLowerCase());
            pSt.setString(2, getLanguage().toString());

            java.sql.ResultSet rs = pSt.executeQuery();
            while (rs.next()) {
                retVal.add(rs.getString("IPA"));
            }
            rs.close();
            pSt.close();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }

    return retVal.toArray(new String[0]);
}

From source file:com.oic.net.Callback.java

private void registerData(String studentNumber, String key, HttpSession session) {
    Connection con = null;/*  w w w. jav  a 2s.  co  m*/
    PreparedStatement ps = null;
    try {
        String sql = "SELECT * FROM user WHERE studentnumber = ?";
        con = DatabaseConnection.getConnection();
        ps = con.prepareStatement(sql);
        ps.setString(1, studentNumber);
        ResultSet rs = ps.executeQuery();
        if (!rs.next()) {
            rs.close();
            ps.close();
            con.close();
            session.setAttribute("alreadyId", false);
            return;
        }
        rs.close();
        ps.close();
        sql = "UPDATE user SET secretkey = ? WHERE studentnumber = ? ";
        ps = con.prepareStatement(sql);
        ps.setString(1, key);
        ps.setString(2, studentNumber);
        ps.executeUpdate();
        ps.close();
        session.setAttribute("alreadyId", true);
    } catch (SQLException e) {
        try {
            ps.close();
        } catch (Exception e1) {
        }
    }
}

From source file:net.sf.l2j.gameserver.instancemanager.CoupleManager.java

public void deleteCouple(int coupleId) {
    int index = getCoupleIndex(coupleId);
    Couple couple = getCouples().get(index);
    if (couple != null) {
        L2PcInstance player1 = (L2PcInstance) L2World.getInstance().findObject(couple.getPlayer1Id());
        L2PcInstance player2 = (L2PcInstance) L2World.getInstance().findObject(couple.getPlayer2Id());
        L2ItemInstance item = null;//w w  w.j a v a 2  s  .  co  m
        if (player1 != null) {
            player1.setPartnerId(0);
            player1.setMaried(false);
            player1.setCoupleId(0);
            item = player1.getInventory().getItemByItemId(9140);
            if (player1.isOnline() == 1 && item != null) {
                player1.destroyItem("Removing Cupids Bow", item, player1, true);
                player1.getInventory().updateDatabase();
            }
            if (player1.isOnline() == 0 && item != null) {
                Integer PlayerId = player1.getObjectId();
                Integer ItemId = 9140;
                java.sql.Connection con = null;
                try {
                    con = L2DatabaseFactory.getInstance().getConnection();
                    PreparedStatement statement = con
                            .prepareStatement("delete from items where owner_id = ? and item_id = ?");
                    statement.setInt(1, PlayerId);
                    statement.setInt(2, ItemId);
                    statement.execute();
                    statement.close();
                } catch (Exception e) {
                } finally {
                    try {
                        con.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        if (player2 != null) {
            player2.setPartnerId(0);
            player2.setMaried(false);
            player2.setCoupleId(0);
            item = player2.getInventory().getItemByItemId(9140);
            if (player2.isOnline() == 1 && item != null) {
                player2.destroyItem("Removing Cupids Bow", item, player2, true);
                player2.getInventory().updateDatabase();
            }
            if (player2.isOnline() == 0 && item != null) {
                Integer Player2Id = player2.getObjectId();
                Integer Item2Id = 9140;
                java.sql.Connection con = null;
                try {
                    con = L2DatabaseFactory.getInstance().getConnection();
                    PreparedStatement statement = con
                            .prepareStatement("delete from items where owner_id = ? and item_id = ?");
                    statement.setInt(1, Player2Id);
                    statement.setInt(2, Item2Id);
                    statement.execute();
                    statement.close();
                } catch (Exception e) {
                } finally {
                    try {
                        con.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        couple.divorce();
        getCouples().remove(index);
    }
}