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.cloudera.sqoop.TestMerge.java

protected void createTable() throws SQLException {
    PreparedStatement s = conn.prepareStatement("DROP TABLE " + TABLE_NAME + " IF EXISTS");
    try {/*from   ww  w  .  j  a va 2s .  co m*/
        s.executeUpdate();
    } finally {
        s.close();
    }

    s = conn.prepareStatement(
            "CREATE TABLE " + TABLE_NAME + " (id INT NOT NULL PRIMARY KEY, val INT, lastmod TIMESTAMP)");
    try {
        s.executeUpdate();
    } finally {
        s.close();
    }

    s = conn.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES (" + "0, 0, NOW())");
    try {
        s.executeUpdate();
    } finally {
        s.close();
    }

    s = conn.prepareStatement("INSERT INTO " + TABLE_NAME + " VALUES (" + "1, 42, NOW())");
    try {
        s.executeUpdate();
    } finally {
        s.close();
    }

    conn.commit();
}

From source file:bboss.org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader.java

/**
 * Closes the PreparedStatement.//  w ww  .  j ava2 s  .  c o m
 */
private void closeStatement(PreparedStatement ps) {
    if (ps != null) {
        try {
            ps.close();
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            String msg = "DataSourceResourceLoader: problem when closing PreparedStatement ";
            log.error(msg, e);
            throw new VelocityException(msg, e);
        }
    }
}

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

private void updateDB() {
    Connection con = null;// ww  w .ja  va  2s  . co m
    try {
        PreparedStatement statement;

        con = L2DatabaseFactory.getInstance().getConnection(con);

        statement = con.prepareStatement("update factions set points = ? where id = ?");
        statement.setFloat(1, _points);
        statement.setInt(2, _Id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("Exception: Faction.load(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.instancemanager.CCHManager.java

public final boolean checkIsRegistered(L2Clan clan, int hallid) {
    if (clan == null)
        return false;

    if (clan.getHasHideout() > 0)
        return true;

    Connection con = null;//from  w w  w .ja v a 2 s .c o  m
    boolean register = false;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con
                .prepareStatement("SELECT clan_id FROM siege_clans WHERE clan_id=? AND castle_id=?");
        statement.setInt(1, clan.getClanId());
        statement.setInt(2, hallid);
        register = statement.executeQuery().next();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: checkIsRegistered(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }

    return register;
}

From source file:com.l2jfree.gameserver.model.MacroList.java

/**
 * @param shortcut//from w w w .  j av  a2s  .c  o m
 */
private void deleteMacroFromDb(L2Macro macro) {
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);

        PreparedStatement statement = con
                .prepareStatement("DELETE FROM character_macroses WHERE charId=? AND id=?");
        statement.setInt(1, _owner.getObjectId());
        statement.setInt(2, macro.id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("could not delete macro:", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:at.becast.youploader.database.SQLite.java

public static int addUpload(File file, Video data, VideoMetadata metadata, Date startAt)
        throws SQLException, IOException {
    PreparedStatement prest = null;
    ObjectMapper mapper = new ObjectMapper();
    String sql = "INSERT INTO `uploads` (`account`, `file`, `lenght`, `data`,`enddir`, `metadata`, `status`,`starttime`) "
            + "VALUES (?,?,?,?,?,?,?,?)";
    prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    prest.setInt(1, metadata.getAccount());
    prest.setString(2, file.getAbsolutePath());
    prest.setLong(3, file.length());/*from  www.j a  v  a2s. c om*/
    prest.setString(4, mapper.writeValueAsString(data));
    prest.setString(5, metadata.getEndDirectory());
    prest.setString(6, mapper.writeValueAsString(metadata));
    prest.setString(7, UploadManager.Status.NOT_STARTED.toString());
    if (startAt == null) {
        prest.setString(8, "");
    } else {
        prest.setDate(8, new java.sql.Date(startAt.getTime()));
    }
    prest.execute();
    ResultSet rs = prest.getGeneratedKeys();
    prest.close();
    if (rs.next()) {
        int id = rs.getInt(1);
        rs.close();
        return id;
    } else {
        return -1;
    }
}

From source file:com.l2jfree.gameserver.datatables.RecordTable.java

private void load() {
    Connection con = null;/*  ww  w  .j a v a2s .  co m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection();

        PreparedStatement statement = con
                .prepareStatement("SELECT maxplayer, date FROM record ORDER BY maxplayer DESC LIMIT 1");
        ResultSet rset = statement.executeQuery();

        if (rset.next()) {
            _record = rset.getInt("maxplayer");
            _date = rset.getString("date");
        }

        rset.close();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.gameobjects.skills.PlayerSkills.java

public void deleteSkills(Connection con, int classIndex) throws SQLException {
    PreparedStatement statement = con
            .prepareStatement("DELETE FROM character_skills WHERE charId=? AND class_index=?");
    statement.setInt(1, getOwner().getObjectId());
    statement.setInt(2, classIndex);//from  www.  j av  a  2s .  c o m
    statement.execute();
    statement.close();

    _storedSkills.remove(classIndex);
}

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

private List<Order2> fillExpansions(PreparedStatement ps) throws SQLException {
    List<Order2> list = new ArrayList<Order2>();
    ResultSet set = ps.executeQuery();
    while (set.next()) {
        list.add(new Order2(set.getString("word2"), set.getDouble("count")));
    }/*  w  ww.  ja  va2  s  .  c o m*/
    ps.close();
    return list;
}

From source file:net.firejack.platform.core.utils.db.DBUtils.java

private static void insertDataToTargetTable(TablesMapping mapping, Connection sourceConnection,
        Connection targetConnection) throws SQLException {
    Map<Column, Column> columnMapping = mapping.getColumnMapping();
    if (columnMapping.isEmpty()) {
        logger.warn("No columns are detected - no data to insert.");
    } else {/*  w  w w  .j  ava  2  s.  c  o m*/
        ResultSet rs = selectDataFromSource(sourceConnection, mapping);

        String insertQuery = populateInsertQuery(mapping);
        PreparedStatement insertStatement = targetConnection.prepareStatement(insertQuery);
        targetConnection.setAutoCommit(false);
        try {
            int currentStep = 1;
            while (rs.next()) {
                for (int i = 1; i <= columnMapping.size(); i++) {
                    insertStatement.setObject(i, rs.getObject(i));
                }
                insertStatement.addBatch();
                if (++currentStep > DEFAULT_BATCH_SIZE) {
                    insertStatement.executeBatch();
                    targetConnection.commit();
                    currentStep = 1;
                }
            }
            if (currentStep != 1) {
                insertStatement.executeBatch();
                targetConnection.commit();
            }
        } catch (SQLException e) {
            logger.error(e.getMessage(), e);
            targetConnection.rollback();
        } finally {
            insertStatement.close();
            rs.close();
        }
    }
}