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:adept.kbapi.sql.QuickJDBC.java

/**
 * execute query to check if object exists in the database. This closes the
 * prepared statement passed in/*from  w ww .ja v  a 2 s  . co m*/
 */
public boolean recordExists(PreparedStatement preparedStmt) throws SQLException {
    boolean doesRecordExist = false;
    java.sql.ResultSet rs = null;
    try {
        rs = preparedStmt.executeQuery();
        doesRecordExist = rs.next();
    } finally {
        try {
            if (rs != null)
                rs.close();
        } catch (Exception e) {
        }
        ;
        try {
            if (preparedStmt != null)
                preparedStmt.close();
        } catch (Exception e) {
        }
        ;
    }
    return doesRecordExist;
}

From source file:org.kuali.kra.subaward.dao.impl.SubAwardFundingSourceDaoOjb.java

protected void closeDatabaseObjects(ResultSet rs, PreparedStatement ps, Connection conn,
        PersistenceBroker broker) {/*from ww w.  j  a v  a  2  s  .com*/
    if (rs != null) {
        try {
            rs.close();
        } catch (SQLException ex) {
            LOG.warn("Failed to close ResultSet.", ex);
        }
    }
    if (ps != null) {
        try {
            ps.close();
        } catch (SQLException ex) {
            LOG.warn("Failed to close PreparedStatement.", ex);
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException ex) {
            LOG.warn("Failed to close Connection.", ex);
        }
    }

    if (broker != null) {
        this.releasePersistenceBroker(broker);
    }
}

From source file:com.l2jfree.gameserver.cache.CrestCache.java

public void convertOldPedgeFiles() {
    File dir = new File(Config.DATAPACK_ROOT, "data/crests/");

    File[] files = dir.listFiles(new OldPledgeFilter());

    if (files == null)
        files = new File[0];

    for (File file : files) {
        int clanId = Integer.parseInt(file.getName().substring(7, file.getName().length() - 4));

        _log.info("Found old crest file \"" + file.getName() + "\" for clanId " + clanId);

        int newId = IdFactory.getInstance().getNextId();

        L2Clan clan = ClanTable.getInstance().getClan(clanId);

        if (clan != null) {
            removeOldPledgeCrest(clan.getCrestId());

            file.renameTo(new File(Config.DATAPACK_ROOT, "data/crests/Crest_" + newId + ".bmp"));
            _log.info("Renamed Clan crest to new format: Crest_" + newId + ".bmp");

            Connection con = null;

            try {
                con = L2DatabaseFactory.getInstance().getConnection(con);
                PreparedStatement statement = con
                        .prepareStatement("UPDATE clan_data SET crest_id = ? WHERE clan_id = ?");
                statement.setInt(1, newId);
                statement.setInt(2, clan.getClanId());
                statement.executeUpdate();
                statement.close();
            } catch (SQLException e) {
                _log.warn("Could not update the crest id:", e);
            } finally {
                L2DatabaseFactory.close(con);
            }/*from   w  w  w  .  ja v  a 2  s  .  co  m*/

            clan.setCrestId(newId);
            clan.setHasCrest(true);
        } else {
            _log.info("Clan Id: " + clanId + " does not exist in table.. deleting.");
            file.delete();
        }
    }
}

From source file:mx.com.pixup.portal.dao.DisqueraDaoJdbc.java

@Override
public void deleteDisquera(Disquera disquera) {
    Connection connection = DBConecta.getConnection();
    PreparedStatement preparedStatement = null;
    String sql = "delete from disquera  where id = ?";
    try {/*from w w w .  jav  a2s .  c o  m*/
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, disquera.getId());
        preparedStatement.execute();
    } catch (Exception e) {
        Logger.getLogger(DBConecta.class.getName()).log(Level.SEVERE, null, e);
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:com.redhat.victims.database.VictimsSQL.java

/**
 * Insert a new record with the given hash and return the record id.
 *
 * @param hash//from www  .ja v a  2 s.c om
 * @return A record id if it was created correctly, else return -1.
 * @throws SQLException
 */
protected int insertRecord(Connection connection, String hash) throws SQLException {
    int id = -1;
    PreparedStatement ps = setObjects(connection, Query.INSERT_RECORD, hash);
    ps.execute();
    ResultSet rs = ps.getGeneratedKeys();
    try {
        while (rs.next()) {
            id = rs.getInt(1);
            break;
        }
    } finally {
        rs.close();
        ps.close();
    }

    return id;
}

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

private List<Order1> fillKeyValuesScores(String sql, String key) {
    PreparedStatement ps;
    List<Order1> list = new ArrayList<Order1>();
    try {/*from  w w w  . ja  v  a2  s .c o m*/
        ps = getDatabaseConnection().getConnection().prepareStatement(sql);

        ps.setString(1, key);
        ResultSet set = ps.executeQuery();
        while (set.next()) {
            list.add(new Order1(set.getString("feature"), set.getDouble("sig")));
        }
        ps.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return list;
}

From source file:com.l2jfree.gameserver.model.entity.faction.FactionMember.java

private void updateDb() {
    Connection con = null;/*from  ww w. ja va  2s . c om*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement(
                "UPDATE faction_members SET faction_points=?,contributions=?,faction_id=? WHERE player_id=?");
        statement.setInt(1, _factionPoints);
        statement.setInt(2, _contributions);
        statement.setInt(3, _factionId);
        statement.setInt(4, _playerId);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("Exception: FactionMember.updateDb(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.model.entity.events.TvT.java

public static void saveData() {
    Connection con = null;//from ww w .  j  a  v a  2s  . co m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        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);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:at.becast.youploader.account.Account.java

public int save() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    LOG.info("Saving account");
    try {//ww  w  . jav  a  2  s  . co m
        PreparedStatement stmt = c
                .prepareStatement("INSERT INTO `accounts` (`name`,`refresh_token`,`cookie`) VALUES(?,?,?)");
        stmt.setString(1, this.name);
        stmt.setString(2, this.refreshToken);
        stmt.setString(3, mapper.writeValueAsString(this.cdata));
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        stmt.close();
        if (rs.next()) {
            int id = rs.getInt(1);
            rs.close();
            LOG.info("Account saved");
            return id;
        } else {
            LOG.error("Could not save account {}!", this.name);
            return -1;
        }
    } catch (SQLException e) {
        LOG.error("Could not save account Ex:", e);
        return -1;
    }
}

From source file:gov.nih.nci.ncicb.tcga.dcc.dam.util.TempClinicalDataLoader.java

private static void emptyClinicalTables(String username) throws SQLException {
    final ArrayList<String> tables = new ArrayList<String>();
    PreparedStatement stmt;
    tables.add("DNA");
    tables.add("RNA");
    tables.add("PROTOCOL");
    tables.add("ALIQUOT");
    tables.add("ANALYTE");
    tables.add("PORTION_SLIDE");
    tables.add("PORTION");
    if (username.contains("gbm")) {
        tables.add("GBMSLIDE");
    }/*w  w w  .  jav  a  2s.com*/
    tables.add("SLIDE");
    if (username.contains("gbm")) {
        tables.add("GBM_PATHOLOGY");
    } else if (username.contains("ov")) {
        tables.add("OVARIAN_PATHOLOGY");
    }
    tables.add("TUMORPATHOLOGY");
    tables.add("SAMPLE");
    tables.add("SURGERY");
    tables.add("DRUG_INTGEN");
    tables.add("EXAMINATION");
    tables.add("RADIATION");
    tables.add("PATIENT");
    final String baseSQL = "delete from ";
    for (final String table : tables) {
        final String workingSQL = baseSQL + " " + table;
        System.out.println("workingSQL = " + workingSQL);
        stmt = dbConnection.prepareStatement(workingSQL);
        final int c = stmt.executeUpdate();
        System.out.println("deleted = " + c);
        stmt.close();
    }

    // also, reset sequences! TODO

}