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.useekm.indexing.postgis.IndexedStatement.java

/**
 * Close, but ignore null input and catch and log resulting
 * {@link SQLException}s.//from  w w w  . j ava2s . c  o m
 */
public static void closeQuietly(PreparedStatement stat) {
    try {
        if (stat != null) // when preparedstatements are pooled, we can't
                          // check for isClosed()
            stat.close();
    } catch (SQLException e) {
        LOG.error("could not close jdbc statement", e);
    }
}

From source file:com.zimbra.cs.mailbox.util.MetadataDump.java

private static int getMailboxGroup(DbConnection conn, int mboxId) throws SQLException {
    int gid = 0;/*www  . j a  v a  2  s  . c  o  m*/
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT group_id FROM mailbox WHERE id = ?");
        stmt.setInt(1, mboxId);
        rs = stmt.executeQuery();
        if (rs.next())
            gid = rs.getInt(1);
    } finally {
        if (rs != null)
            rs.close();
        if (stmt != null)
            stmt.close();
    }
    return gid;
}

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

public static Boolean updateUploadProgress(int id, long progress) {
    PreparedStatement prest = null;
    String sql = "UPDATE `uploads` SET `uploaded`=? WHERE `id`=?";
    try {/*w ww .  jav  a  2  s .com*/
        prest = c.prepareStatement(sql);
        prest.setLong(1, progress);
        prest.setInt(2, id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error updating upload progress", e);
        return false;
    }
}

From source file:com.useekm.indexing.postgis.IndexedStatement.java

/**
 * Deletes matching indexed statements.//from   ww  w .  j  a va  2s .  co  m
 * 
 * @param subject
 *            The subject, BNode subjects are ignored.
 * @param predicate
 *            The predicate
 * @param object
 *            The object/value
 * 
 * @return The number of matching (hence deleted) statements.
 * 
 * @throws IndexException
 * @Throws SQLException
 */
static int delete(Connection conn, String table, Resource subject, URI predicate, Value object)
        throws SQLException {
    if (subject instanceof BNode || object instanceof BNode)
        return 0; // // BNodes are not indexed, so there is nothing to
                  // delete
    String sql = createQuery(DELETE_FROM_STS + table, subject, predicate, object);
    PreparedStatement stat = conn.prepareStatement(sql);
    try {
        addBindings(stat, subject, predicate, object);
        return stat.executeUpdate();
    } finally {
        stat.close();
    }
}

From source file:com.chaosinmotion.securechat.server.commands.GetMessages.java

public static ReturnResult processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String deviceid = requestParams.optString("deviceid");
    MessageReturnResult mrr = new MessageReturnResult();

    /*//from w  w w  .j av  a2s.co  m
     * Save message to the database.
     */

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        /*
         * Get the device ID for this device. Verify it belongs to the
         * user specified
         */
        c = Database.get();
        ps = c.prepareStatement("SELECT deviceid " + "FROM Devices " + "WHERE deviceuuid = ? AND userid = ?");
        ps.setString(1, deviceid);
        ps.setInt(2, userinfo.getUserID());
        rs = ps.executeQuery();

        int deviceID = 0;
        if (rs.next()) {
            deviceID = rs.getInt(1);
        }

        rs.close();
        ps.close();
        if (deviceID == 0) {
            return new ReturnResult(Errors.ERROR_UNKNOWNDEVICE, "Unknown device");
        }

        /*
         * Run query to get messages
         */

        ps = c.prepareStatement("SELECT Messages.messageid, " + "    Messages.senderid, "
                + "    Users.username, " + "    Messages.toflag, " + "    Messages.received, "
                + "    Messages.message " + "FROM Messages, Users " + "WHERE Messages.deviceid = ? "
                + "  AND Messages.senderid = Users.userid");
        ps.setInt(1, deviceID);

        rs = ps.executeQuery();
        while (rs.next()) {
            int messageID = rs.getInt(1);
            int senderID = rs.getInt(2);
            String senderName = rs.getString(3);
            boolean toflag = rs.getBoolean(4);
            Timestamp received = rs.getTimestamp(5);
            byte[] message = rs.getBytes(6);

            mrr.addMessage(messageID, senderID, senderName, toflag, received, message);
        }

        /*
         * Return messages
         */
        return mrr;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:com.concursive.connect.web.modules.common.social.rating.dao.Rating.java

public static void deleteByProject(Connection db, int projectId, String table, String uniqueField)
        throws SQLException {
    PreparedStatement pst = db.prepareStatement("DELETE FROM " + table + "_rating " + "WHERE " + uniqueField
            + " IN (SELECT " + uniqueField + "FROM " + table + " WHERE project_id = ?)");
    pst.setInt(1, projectId);/*ww w.  ja v  a2  s  . co  m*/
    pst.execute();
    pst.close();
}

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

public static void saveData() {
    Connection con = null;/*  w  ww  .j  av a 2s  . com*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("Delete from dm");
        statement.execute();
        statement.close();

        statement = con.prepareStatement(
                "INSERT INTO dm (eventName, eventDesc, joiningLocation, minlvl, maxlvl, npcId, npcX, npcY, npcZ, rewardId, rewardAmount, color, playerX, playerY, playerZ ) 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, _rewardId);
        statement.setInt(11, _rewardAmount);
        statement.setInt(12, _playerColors);
        statement.setInt(13, _playerX);
        statement.setInt(14, _playerY);
        statement.setInt(15, _playerZ);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: DM.saveData(): ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.nabla.wapp.report.server.ReportManager.java

protected static Integer getRole(final Connection conn, final String name) throws SQLException {
    if (name != null) {
        final PreparedStatement stmt = StatementFormat.prepare(conn,
                "SELECT id FROM role WHERE name LIKE ? AND uname IS NOT NULL;", name);
        try {//from  w  w w .  j a v  a  2 s.c  o m
            final ResultSet rs = stmt.executeQuery();
            try {
                if (rs.next())
                    return rs.getInt(1);
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
    }
    return null;
}

From source file:net.codjo.dataprocess.server.treatmenthelper.TreatmentHelper.java

public static void initRepositoryUserAccess(Connection con, String userName,
        List<RepositoryDescriptor> repositoryDescList) throws SQLException {
    User user = new User();
    user.setUserName(userName);// w ww.  j a  va 2  s .c  o  m
    for (RepositoryDescriptor repoDescriptor : repositoryDescList) {
        user.addRepository(new Repository(repoDescriptor.getRepositoryName()));
    }
    String xml = new UserXStreamImpl().toXml(user);
    PreparedStatement pStmt = con.prepareStatement("delete from PM_DP_USER where USER_NAME = ? "
            + " insert into PM_DP_USER (USER_NAME, USER_PARAM) values (?, ?)");
    try {
        pStmt.setString(1, userName);
        pStmt.setString(2, userName);
        pStmt.setString(3, xml);
        pStmt.executeUpdate();
        LOG.info(String.format("Droits d'accs accords  l'utilisateur '%s' pour tous les repositories.",
                userName));
    } finally {
        pStmt.close();
    }
}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Delete a user/*ww  w . j a v a 2 s.  c  o  m*/
 * 
 * @param userName The username of the user to delete.
 * @return true if the user was deleted successfully, false otherwise
 */
public static boolean deleteUser(String userName) {
    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement deleteUser = conn.prepareStatement(DELETE_USER);

            deleteUser.setString(1, userName); // Set parameters
            deleteUser.executeUpdate();

            deleteUser.close();
            conn.close(); // Close statement and connection

            return true;
        }

        catch (Exception e) {
            MetaDbHelper.logEvent(e);
        }
    }
    return false;
}