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.opensymphony.module.propertyset.database.JDBCPropertySet.java

public void remove(String key) throws PropertyException {
    Connection conn = null;//from w w  w .  ja  v a 2 s .  co m

    try {
        conn = ds.getConnection();

        String sql = "DELETE FROM " + tableName + " WHERE " + colGlobalKey + " = ? AND " + colItemKey + " = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1, globalKey);
        ps.setString(2, key);
        ps.executeUpdate();
        ps.close();
    } catch (SQLException e) {
        throw new PropertyException(e.getMessage());
    } finally {
        closeConnection(conn);
    }
}

From source file:com.opensymphony.module.propertyset.database.JDBCPropertySet.java

protected void setImpl(int type, String key, Object value) throws PropertyException {
    if (value == null) {
        throw new PropertyException("JDBCPropertySet does not allow for null values to be stored");
    }//from   w ww . j  a v a 2s  . co m

    Connection conn = null;

    try {
        conn = ds.getConnection();

        String sql = "UPDATE " + tableName + " SET " + colString + " = ?, " + colDate + " = ?, " + colData
                + " = ?, " + colFloat + " = ?, " + colNumber + " = ?, " + colItemType + " = ? " + " WHERE "
                + colGlobalKey + " = ? AND " + colItemKey + " = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        setValues(ps, type, key, value);

        int rows = ps.executeUpdate();
        ps.close();

        if (rows != 1) {
            // ok, this is a new value, insert it
            sql = "INSERT INTO " + tableName + " (" + colString + ", " + colDate + ", " + colData + ", "
                    + colFloat + ", " + colNumber + ", " + colItemType + ", " + colGlobalKey + ", " + colItemKey
                    + ") VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            ps = conn.prepareStatement(sql);
            setValues(ps, type, key, value);
            ps.executeUpdate();
            ps.close();
        }
    } catch (SQLException e) {
        throw new PropertyException(e.getMessage());
    } finally {
        closeConnection(conn);
    }
}

From source file:com.vigglet.util.ModelUtilBase.java

public T findById(int id) {
    T result = null;// w w w . j a  v  a 2s  . co  m

    try {
        PreparedStatement stmt = getFindByIdQuery();
        stmt.setInt(1, id);
        ResultSet rs = stmt.executeQuery();

        if (rs.next()) {
            result = readModel(rs);
        }

        rs.close();
        stmt.close();
    } catch (SQLException ex) {
        logger.log(Level.SEVERE, null, ex);
    }

    return result;
}

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

/**
 * Inserts a table into the internal DB. Need to save table positions
 * separately./*from  w  w w . j av  a2  s.  c om*/
 * 
 * @param t
 * @param conn
 */
public void saveTable(Table t, Connection conn) throws SQLException {
    String insertTableSQL = "INSERT OR REPLACE INTO table_schema (id,name,schema) VALUES (?,?,?);";
    PreparedStatement ps = conn.prepareStatement(insertTableSQL);
    ps.setString(1, t.getId().toString());
    ps.setString(2, t.getName());
    ps.setString(3, t.getSchemaId());
    ps.executeUpdate();
    ps.close();

    LOG.info("Saved Table {}", t);
}

From source file:ece356.UserDBAO.java

public static String getSalt(String username) throws ClassNotFoundException, SQLException {
    Connection con = null;// ww w.  jav  a  2  s  .  co m
    PreparedStatement pstmt = null;
    UserData ret;
    try {
        con = getConnection();
        String query = "select COUNT(*)as numRecords, password_salt from user INNER JOIN userType ON user.userTypeID = userType.userTypeID where user.username = ?";
        pstmt = con.prepareStatement(query);
        pstmt.setString(1, username);
        ResultSet resultSet;
        resultSet = pstmt.executeQuery();
        resultSet.next();

        if (resultSet.getInt("numRecords") > 0) {
            return resultSet.getString("password_salt");
        } else
            return null;
    } catch (Exception e) {
        System.out.println("EXCEPTION:%% " + e);
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
        if (con != null) {
            con.close();
        }
    }
    return null;
}

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

public synchronized void destroyClan(int clanId) {
    _log.info("destroy clan with id:" + clanId);
    L2Clan clan = getClan(clanId);/*from   w  ww.j a v a2  s  . com*/
    if (clan == null) {
        return;
    }

    clan.broadcastToOnlineMembers(SystemMessageId.CLAN_HAS_DISPERSED.getSystemMessage());

    int castleId = clan.getHasCastle();
    if (castleId == 0) {
        for (Castle castle : CastleManager.getInstance().getCastles().values()) {
            castle.getSiege().removeSiegeClan(clanId);
        }
    }
    int fortId = clan.getHasFort();
    if (fortId == 0) {
        for (FortSiege siege : FortSiegeManager.getInstance().getSieges()) {
            siege.removeSiegeClan(clanId);
        }
    }

    L2ClanMember leaderMember = clan.getLeader();
    if (leaderMember == null)
        clan.getWarehouse().destroyAllItems("ClanRemove", null, null);
    else
        clan.getWarehouse().destroyAllItems("ClanRemove", clan.getLeader().getPlayerInstance(), null);

    for (L2ClanMember member : clan.getMembers()) {
        clan.removeClanMember(member.getObjectId(), 0);
    }

    _clans.remove(clanId);
    IdFactory.getInstance().releaseId(clanId);

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement("DELETE FROM clan_data WHERE clan_id=?");
        statement.setInt(1, clanId);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM clan_privs WHERE clan_id=?");
        statement.setInt(1, clanId);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM clan_skills WHERE clan_id=?");
        statement.setInt(1, clanId);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM clan_subpledges WHERE clan_id=?");
        statement.setInt(1, clanId);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM clan_wars WHERE clan1=? OR clan2=?");
        statement.setInt(1, clanId);
        statement.setInt(2, clanId);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM clan_notices WHERE clanID=?");
        statement.setInt(1, clanId);
        statement.execute();
        statement.close();

        if (castleId != 0) {
            statement = con.prepareStatement("UPDATE castle SET taxPercent = 0 WHERE id = ?");
            statement.setInt(1, castleId);
            statement.execute();
            statement.close();
        }
        if (fortId != 0) {
            Fort fort = FortManager.getInstance().getFortById(fortId);
            if (fort != null) {
                L2Clan owner = fort.getOwnerClan();
                if (clan == owner)
                    fort.removeOwner(true);
            }
        }

        if (_log.isDebugEnabled())
            _log.info("clan removed in db: " + clanId);
    } catch (Exception e) {
        _log.error("error while removing clan in db ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.github.woozoo73.test.dummy.ProcessorTest.java

private int executeUpdate(String sql, Object[] args) {
    int result = 0;
    Connection con = null;//from w  w  w.ja  v a  2  s. c o  m
    PreparedStatement ps = null;

    try {
        con = getConnection();
        ps = con.prepareStatement(sql);
        if (args != null) {
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
        }

        result = ps.executeUpdate();
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }

    return result;
}

From source file:com.l2jfree.gameserver.idfactory.StackIDFactory.java

private int insertUntil(int[] tmp_obj_ids, int idx, int N, Connection con) throws SQLException {
    int id = tmp_obj_ids[idx];
    if (id == _tempOID) {
        _tempOID++;//from   www  . j  a v a2 s. co m
        return N;
    }
    // Check these IDs not present in DB
    if (Config.BAD_ID_CHECKING) {
        for (String check : ID_CHECKS) {
            PreparedStatement ps = con.prepareStatement(check);
            ps.setInt(1, _tempOID);
            //ps.setInt(1, _curOID);
            ps.setInt(2, id);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                int badId = rs.getInt(1);
                _log.fatal("Bad ID " + badId + " in DB found by: " + check);
                throw new RuntimeException();
            }
            rs.close();
            ps.close();
        }
    }

    //int hole = id - _curOID;
    int hole = id - _tempOID;
    if (hole > N - idx)
        hole = N - idx;
    for (int i = 1; i <= hole; i++) {
        //_log.debugr("Free ID added " + (_tempOID));
        _freeOIDStack.push(_tempOID);
        _tempOID++;
        //_curOID++;
    }
    if (hole < N - idx)
        _tempOID++;
    return N - hole;
}

From source file:eu.sisob.uma.restserver.AuthorizationManager.java

private static boolean DBAuthorizeUserIn(String user, String pass) {
    boolean success = false;
    Connection conn = null;/*from w  w  w . ja  va 2 s .  com*/
    PreparedStatement statement = null;
    ResultSet rs = null;
    try {
        String query = "SELECT 1 FROM USERS WHERE user_email = ? and user_pass = ?";
        conn = SystemManager.getInstance().getSystemDbPool().getConnection();
        statement = conn.prepareStatement(query);
        statement.setString(1, user);
        statement.setString(2, pass);

        rs = statement.executeQuery();
        if (rs.next())
            success = true;
        else
            success = false;
    } catch (SQLException ex) {
        ProjectLogger.LOGGER.error("", ex);
        success = false;
    } catch (Exception ex) {
        ProjectLogger.LOGGER.error("", ex);
        success = false;
    } finally {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        if (statement != null)
            try {
                statement.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        if (conn != null)
            try {
                conn.close();
            } catch (SQLException ex) {
                ProjectLogger.LOGGER.error("", ex);
            }

        statement = null;
        rs = null;
    }

    return success;
}

From source file:fr.aliacom.obm.common.addition.CommitedOperationDaoJdbcImpl.java

private PreparedStatement prepareUpdateStatement(Connection con, CommitedElement commitedElement)
        throws SQLException {
    PreparedStatement ps = con.prepareStatement("UPDATE CommitedOperation "
            + "SET commitedoperation_entity_id=? " + "WHERE commitedoperation_hash_client_id=? ");
    try {//from w w  w  .  j av  a 2 s .  co m
        int idx = 1;
        ps.setInt(idx++, commitedElement.getEntityId().getId());
        ps.setString(idx++, commitedElement.getClientId());
        return ps;
    } catch (SQLException e) {
        ps.close();
        throw e;
    } catch (RuntimeException e) {
        ps.close();
        throw e;
    }
}