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.l2jfree.gameserver.model.ShortCuts.java

public synchronized void registerShortCut(L2ShortCut shortcut) {
    _shortCuts.put(shortcut.getSlot() + 12 * shortcut.getPage(), shortcut);

    Connection con = null;/*from   ww w.j a v  a  2s. c  o m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);

        PreparedStatement statement = con.prepareStatement(
                "REPLACE INTO character_shortcuts (charId,slot,page,type,shortcut_id,level,class_index) values(?,?,?,?,?,?,?)");
        statement.setInt(1, _owner.getObjectId());
        statement.setInt(2, shortcut.getSlot());
        statement.setInt(3, shortcut.getPage());
        statement.setInt(4, shortcut.getType());
        statement.setInt(5, shortcut.getId());
        statement.setInt(6, shortcut.getLevel());
        statement.setInt(7, _owner.getClassIndex());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.micromux.cassandra.jdbc.CollectionsTest.java

@Test
public void testUpdateSet() throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Test: 'testUpdateSet'\n");

    Statement statement = con.createStatement();

    // add some items to the set
    String update1 = "UPDATE testcollection SET S = S + {'green', 'white', 'orange'} WHERE k = 1;";
    statement.executeUpdate(update1);/*from   www  .  j av  a  2  s .  c o m*/

    ResultSet result = statement.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();

    assertEquals(1, result.getInt("k"));
    Object myObj = result.getObject("s");
    Set<String> mySet = (Set<String>) myObj;
    assertEquals(5, mySet.size());
    assertTrue(mySet.contains("white"));

    if (LOG.isDebugEnabled())
        LOG.debug("s           = '{}'", myObj);

    // remove an item from the set
    String update2 = "UPDATE testcollection SET S = S - {'red'} WHERE k = 1;";
    statement.executeUpdate(update2);

    result = statement.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();

    assertEquals(1, result.getInt("k"));

    myObj = result.getObject("s");
    mySet = (Set<String>) myObj;
    assertEquals(4, mySet.size());
    assertTrue(mySet.contains("white"));
    assertFalse(mySet.contains("red"));

    if (LOG.isDebugEnabled())
        LOG.debug("s           = '{}'", myObj);

    String update4 = "UPDATE testcollection SET S =  ? WHERE k = 1;";

    PreparedStatement prepared = con.prepareStatement(update4);
    Set<String> myNewSet = new HashSet<String>();
    myNewSet.add("black");
    myNewSet.add("blue");
    prepared.setObject(1, myNewSet, Types.OTHER);
    prepared.execute();

    result = prepared.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();
    myObj = result.getObject("s");
    mySet = (Set<String>) myObj;

    if (LOG.isDebugEnabled())
        LOG.debug("s (prepared)= '{}'\n", myObj);
}

From source file:com.l2jfree.gameserver.communitybbs.bb.Post.java

public void insertindb(CPost cp) {
    Connection con = null;/*from  w w w . j av a  2 s.  co m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement(
                "INSERT INTO posts (post_id,post_owner_name,post_ownerid,post_date,post_topic_id,post_forum_id,post_txt) values (?,?,?,?,?,?,?)");
        statement.setInt(1, cp.postId);
        statement.setString(2, cp.postOwner);
        statement.setInt(3, cp.postOwnerId);
        statement.setLong(4, cp.postDate);
        statement.setInt(5, cp.postTopicId);
        statement.setInt(6, cp.postForumId);
        statement.setString(7, cp.postTxt);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("error while saving new Post to db ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.communitybbs.bb.Topic.java

/**
 *
 *//*from ww  w  .  j  a v a 2 s . c  o  m*/
public void insertindb() {
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement(
                "INSERT INTO topic (topic_id,topic_forum_id,topic_name,topic_date,topic_ownername,topic_ownerid,topic_type,topic_reply) values (?,?,?,?,?,?,?,?)");
        statement.setInt(1, _id);
        statement.setInt(2, _forumId);
        statement.setString(3, _topicName);
        statement.setLong(4, _date);
        statement.setString(5, _ownerName);
        statement.setInt(6, _ownerId);
        statement.setInt(7, _type);
        statement.setInt(8, _cReply);
        statement.execute();
        statement.close();

    } catch (Exception e) {
        _log.warn("error while saving new Topic to db ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.l2jfree.gameserver.model.entity.Auction.java

/** Save Auction Data End */
private void saveAuctionDate() {
    Connection con = null;//from w  w w.j  a va2 s.c  o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con.prepareStatement("UPDATE auction SET endDate = ? WHERE id = ?");
        statement.setLong(1, _endDate);
        statement.setInt(2, _id);
        statement.execute();

        statement.close();
    } catch (Exception e) {
        _log.fatal("Exception: saveAuctionDate(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.octo.captcha.engine.bufferedengine.buffer.DatabaseCaptchaBuffer.java

/**
 * Clear the buffer from all locale//from  w w w. j av  a2 s.c  om
 */
public void clear() {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        con = datasource.getConnection();
        ps = con.prepareStatement("delete from " + table);
        ps.execute();
        con.commit();

    } catch (SQLException e) {
        log.error(DB_ERROR, e);
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
            }
        }
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
            }
        }
    }

}

From source file:com.netspective.axiom.sql.Query.java

protected boolean checkRecordExistsIgnoreStatistics(ConnectionContext cc, Object[] overrideParams)
        throws NamingException, SQLException {
    if (log.isTraceEnabled())
        trace(cc, overrideParams);/*from w w w.ja va2  s. c o  m*/
    try {
        PreparedStatement stmt = createStatement(cc, overrideParams, false);

        boolean exists = stmt.execute() && stmt.getResultSet().next();
        stmt.close();
        return exists;
    } catch (SQLException e) {
        log.error(createExceptionMessage(cc, overrideParams), e);
        throw e;
    }
}

From source file:com.micromux.cassandra.jdbc.CollectionsTest.java

@Test
public void testUpdateList() throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Test: 'testUpdateList'\n");

    Statement statement = con.createStatement();

    String update1 = "UPDATE testcollection SET L = L + [2,4,6] WHERE k = 1;";
    statement.executeUpdate(update1);/*from   ww  w.java 2  s.  c  om*/

    ResultSet result = statement.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();

    assertEquals(1, result.getInt("k"));
    Object myObj = result.getObject("l");
    List<Long> myList = (List<Long>) myObj;
    assertEquals(6, myList.size());
    assertTrue(12345L == myList.get(2));

    if (LOG.isDebugEnabled())
        LOG.debug("l           = '{}'", myObj);

    String update2 = "UPDATE testcollection SET L = [98,99,100] + L WHERE k = 1;";
    statement.executeUpdate(update2);
    result = statement.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();
    myObj = result.getObject("l");
    myList = (List<Long>) myObj;

    // 98, 99, 100, 1, 3, 12345, 2, 4, 6
    // remove all of these values from the list - it should be empty
    assertEquals("Checking the size of the List", 9, myList.size());

    myList.remove(Long.valueOf(98));
    myList.remove(Long.valueOf(99));
    myList.remove(Long.valueOf(100));
    myList.remove(Long.valueOf(1));
    myList.remove(Long.valueOf(3));
    myList.remove(Long.valueOf(12345));
    myList.remove(Long.valueOf(2));
    myList.remove(Long.valueOf(4));
    myList.remove(Long.valueOf(6));

    assertEquals("List should now be empty", 0, myList.size());

    if (LOG.isDebugEnabled())
        LOG.debug("l           = '{}'", myObj);

    String update3 = "UPDATE testcollection SET L[0] = 2000 WHERE k = 1;";
    statement.executeUpdate(update3);
    result = statement.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();
    myObj = result.getObject("l");
    myList = (List<Long>) myObj;

    if (LOG.isDebugEnabled())
        LOG.debug("l           = '{}'", myObj);

    //        String update4 = "UPDATE testcollection SET L = L +  ? WHERE k = 1;";
    String update4 = "UPDATE testcollection SET L =  ? WHERE k = 1;";

    PreparedStatement prepared = con.prepareStatement(update4);
    List<Long> myNewList = new ArrayList<Long>();
    myNewList.add(8888L);
    myNewList.add(9999L);
    prepared.setObject(1, myNewList, Types.OTHER);
    prepared.execute();

    result = prepared.executeQuery("SELECT * FROM testcollection WHERE k = 1;");
    result.next();
    myObj = result.getObject("l");
    myList = (List<Long>) myObj;

    if (LOG.isDebugEnabled())
        LOG.debug("l (prepared)= '{}'\n", myObj);
}

From source file:com.l2jfree.gameserver.instancemanager.games.Lottery.java

public void increasePrize(long count) {
    _prize += count;/*from   w  ww.j a  v  a  2 s.com*/
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement(UPDATE_PRICE);
        statement.setLong(1, getPrize());
        statement.setLong(2, getPrize());
        statement.setInt(3, getId());
        statement.execute();
        statement.close();
    } catch (SQLException e) {
        _log.warn("Lottery: Could not increase current lottery prize: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:lineage2.gameserver.cache.CrestCache.java

/**
 * Method savePledgeCrest./*from ww w .  j  a v  a  2s  . com*/
 * @param pledgeId int
 * @param crest byte[]
 * @return int
 */
public int savePledgeCrest(int pledgeId, byte[] crest) {
    int crestId = getCrestId(pledgeId, crest);
    writeLock.lock();
    try {
        _pledgeCrestId.put(pledgeId, crestId);
        _pledgeCrest.put(crestId, crest);
    } finally {
        writeLock.unlock();
    }
    Connection con = null;
    PreparedStatement statement = null;
    try {
        con = DatabaseFactory.getInstance().getConnection();
        statement = con.prepareStatement("UPDATE clan_data SET crest=? WHERE clan_id=?");
        statement.setBytes(1, crest);
        statement.setInt(2, pledgeId);
        statement.execute();
    } catch (Exception e) {
        _log.error("", e);
    } finally {
        DbUtils.closeQuietly(con, statement);
    }
    return crestId;
}