Example usage for java.sql PreparedStatement execute

List of usage examples for java.sql PreparedStatement execute

Introduction

In this page you can find the example usage for java.sql PreparedStatement execute.

Prototype

boolean execute() throws SQLException;

Source Link

Document

Executes the SQL statement in this PreparedStatement object, which may be any kind of SQL statement.

Usage

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.  j a v  a  2s  .c  om
    pst.execute();
    pst.close();
}

From source file:com.l2jfree.gameserver.network.L2Client.java

public static void deleteCharByObjId(int objid) {
    if (objid < 0)
        return;//w ww  . j a v a 2s.  co m

    Connection con = null;

    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        for (ItemRelatedTable table : TableOptimizer.getItemRelatedTables()) {
            statement = con.prepareStatement(table.getDeleteQuery());
            statement.setInt(1, objid);
            statement.execute();
            statement.close();
        }

        for (CharacterRelatedTable table : TableOptimizer.getCharacterRelatedTables()) {
            statement = con.prepareStatement(table.getDeleteQuery());
            statement.setInt(1, objid);
            statement.execute();
            statement.close();
        }

        statement = con.prepareStatement("DELETE FROM items WHERE owner_id=?");
        statement.setInt(1, objid);
        statement.execute();
        statement.close();

        statement = con.prepareStatement("DELETE FROM characters WHERE charId=?");
        statement.setInt(1, objid);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Error deleting character.", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:edu.jhu.pha.vospace.oauth.MySQLOAuthProvider2.java

/**
 * Generate a fresh request token and secret for a consumer.
 * //from ww w  .j a  v a2 s .  c o  m
 * @throws OAuthException
 */
public static synchronized Token generateRequestToken(final String consumer_key, final String callback_url,
        final MultivaluedMap<String, String> attributes) {

    // generate token and secret based on consumer_key

    // for now use md5 of name + current time as token
    final String token_data = consumer_key + System.nanoTime();
    final String token = DigestUtils.md5Hex(token_data);
    // for now use md5 of name + current time + token as secret
    final String secret_data = consumer_key + System.nanoTime() + token;
    final String secret = DigestUtils.md5Hex(secret_data);

    Token tokenObj = new Token(token, secret, consumer_key, callback_url, attributes);

    if (null == attributes.getFirst("share")) {
        DbPoolServlet.goSql("Insert new request token",
                "insert into oauth_accessors (request_token, token_secret, consumer_id, created) select ?, ?, consumer_id , ? from oauth_consumers where consumer_key = ?",
                new SqlWorker<Boolean>() {
                    @Override
                    public Boolean go(Connection conn, PreparedStatement stmt) throws SQLException {
                        stmt.setString(1, token);
                        stmt.setString(2, secret);
                        stmt.setString(3,
                                dateFormat.format(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime()));
                        stmt.setString(4, consumer_key);
                        return stmt.execute();
                    }
                });
    } else {
        DbPoolServlet.goSql("Insert new request token",
                "insert into oauth_accessors (request_token, token_secret, consumer_id, container_id, created, accessor_write_permission, share_key) "
                        + "select ?, ?, consumer_id , container_id, ?, share_write_permission, share_key "
                        + "from oauth_consumers, container_shares " + "where consumer_key = ? and share_id = ?",
                new SqlWorker<Boolean>() {
                    @Override
                    public Boolean go(Connection conn, PreparedStatement stmt) throws SQLException {
                        stmt.setString(1, token);
                        stmt.setString(2, secret);
                        stmt.setString(3,
                                dateFormat.format(Calendar.getInstance(TimeZone.getTimeZone("UTC")).getTime()));
                        stmt.setString(4, consumer_key);
                        stmt.setString(5, attributes.getFirst("share"));
                        return stmt.execute();
                    }
                });
    }

    return tokenObj;
}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.tags.MMXUserTagsResourceTest.java

@AfterClass
public static void cleanupDatabase() {
    final String statementStr1 = "DELETE FROM mmxApp WHERE appName LIKE '%" + "usertagresourcetestapp" + "%'";
    final String statementStr3 = "DELETE FROM ofUser WHERE username LIKE '%" + "MMXUserTagsResourceTestUser"
            + "%'";
    final String statementStr4 = "DELETE FROM mmxTag WHERE tagname LIKE '%" + "tag" + "%'";

    Connection conn = null;// ww w .j  a  v a2 s .  co m
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt3 = null;
    PreparedStatement pstmt4 = null;

    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt1 = conn.prepareStatement(statementStr1);
        pstmt3 = conn.prepareStatement(statementStr3);
        pstmt4 = conn.prepareStatement(statementStr4);
        pstmt1.execute();
        pstmt3.execute();
        pstmt4.execute();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : {}", e);
    } finally {
        CloseUtil.close(LOGGER, pstmt1, conn);
        CloseUtil.close(LOGGER, pstmt3);
        CloseUtil.close(LOGGER, pstmt4);
    }
}

From source file:com.keybox.manage.db.PublicKeyDB.java

/**
 * deletes all public keys for user/* ww  w . j a v a  2  s . c  o m*/
 *
 * @param userId  user id
 */
public static void deleteUserPublicKeys(Long userId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con
                .prepareStatement("delete from public_keys where user_id=? and enabled=true");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }
    DBUtils.closeConn(con);

}

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

public static boolean failUpload(int upload_id) {
    PreparedStatement prest = null;
    String sql = "UPDATE `uploads` SET `status`=?,`uploaded`=? WHERE `id`=?";
    try {/*www. j  a  v a2 s.co  m*/
        prest = c.prepareStatement(sql);
        prest.setString(1, UploadManager.Status.FAILED.toString());
        prest.setInt(3, upload_id);
        boolean res = prest.execute();
        prest.close();
        return res;
    } catch (SQLException e) {
        LOG.error("Error marking upload as failed", e);
        return false;
    }

}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.tags.MMXDeviceTagsResourceTest.java

@AfterClass
public static void cleanupDatabase() {
    final String statementStr1 = "DELETE FROM mmxApp WHERE appName LIKE '%" + "devicetagresourcetestapp" + "%'";
    final String statementStr2 = "DELETE FROM mmxDevice WHERE deviceId LIKE '%"
            + "mmxdevicetagsresourcetestdevice" + "%'";
    final String statementStr4 = "DELETE FROM mmxTag WHERE tagname LIKE '%" + "tag" + "%'";

    Connection conn = null;/*from   ww w . j  a  va  2s  .  c  o m*/
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;
    PreparedStatement pstmt4 = null;

    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt1 = conn.prepareStatement(statementStr1);
        pstmt2 = conn.prepareStatement(statementStr2);
        pstmt4 = conn.prepareStatement(statementStr4);
        pstmt1.execute();
        pstmt2.execute();
        pstmt4.execute();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : {}", e);
    } finally {
        CloseUtil.close(LOGGER, pstmt1, conn);
        CloseUtil.close(LOGGER, pstmt2);
        CloseUtil.close(LOGGER, pstmt4);
    }
}

From source file:com.hangum.tadpole.rdb.core.editors.main.utils.plan.OracleExecutePlanUtils.java

/**
 * oracle query plan? . //from w w  w  .j  av  a 2 s .c o  m
 * 
 * @param userDB
 * @param reqQuery
 * @param planTableName
 * @throws Exception
 */
public static void plan(UserDBDAO userDB, RequestQuery reqQuery, String planTableName,
        java.sql.Connection javaConn, String statement_id) throws Exception {

    PreparedStatement pstmt = null;
    try {
        String query = PartQueryUtil.makeExplainQuery(userDB, reqQuery.getSql());
        query = StringUtils.replaceOnce(query, PublicTadpoleDefine.STATEMENT_ID, statement_id);
        query = StringUtils.replaceOnce(query, PublicTadpoleDefine.DELIMITER, planTableName);

        pstmt = javaConn.prepareStatement(query);
        if (reqQuery.getSqlStatementType() == SQL_STATEMENT_TYPE.PREPARED_STATEMENT) {
            final Object[] statementParameter = reqQuery.getStatementParameter();
            for (int i = 1; i <= statementParameter.length; i++) {
                pstmt.setObject(i, statementParameter[i - 1]);
            }
        }
        pstmt.execute();
    } finally {
        try {
            if (pstmt != null)
                pstmt.close();
        } catch (Exception e) {
        }
    }

}

From source file:com.magnet.mmx.server.plugin.mmxmgmt.api.tags.MMXTopicTagsResourceTest.java

@AfterClass
public static void cleanupDatabase() {
    final String statementStr1 = "DELETE FROM mmxApp WHERE appName LIKE '%" + "mmxtopicstagsresourcetesttopic"
            + "%'";
    final String statementStr2 = "DELETE FROM ofPubsubNode WHERE nodeID LIKE '%" + "topictagresourcetestapp"
            + "%'";
    final String statementStr3 = "DELETE FROM mmxTag WHERE tagname LIKE '%" + "tag" + "%'";

    Connection conn = null;//from w w  w .  j a v a2  s . c  o  m
    PreparedStatement pstmt1 = null;
    PreparedStatement pstmt2 = null;
    PreparedStatement pstmt3 = null;

    try {
        conn = UnitTestDSProvider.getDataSource().getConnection();
        pstmt1 = conn.prepareStatement(statementStr1);
        pstmt2 = conn.prepareStatement(statementStr2);
        pstmt3 = conn.prepareStatement(statementStr3);
        pstmt1.execute();
        pstmt2.execute();
        pstmt3.execute();
    } catch (SQLException e) {
        LOGGER.error("cleanupDatabase : {}", e);
    } finally {
        CloseUtil.close(LOGGER, pstmt1, conn);
        CloseUtil.close(LOGGER, pstmt2);
        CloseUtil.close(LOGGER, pstmt3);
    }
}

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

public static Boolean updateTemplate(int id, Template template) throws SQLException, IOException {
    PreparedStatement prest = null;
    ObjectMapper mapper = new ObjectMapper();
    String sql = "UPDATE `templates` SET `data`=? WHERE `id`=?";
    prest = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
    prest.setString(1, mapper.writeValueAsString(template));
    prest.setInt(2, id);/*from w w  w  .ja  va2  s  .co  m*/
    boolean res = prest.execute();
    prest.close();
    return res;
}