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.flexive.core.storage.PostgreSQL.PostgreSQLSequencerStorage.java

/**
 * {@inheritDoc}/*from   w w  w.j a  va  2 s .c  o  m*/
 */
@Override
public void setSequencerId(String name, long newId) throws FxApplicationException {
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = Database.getDbConnection();
        ps = con.prepareStatement("SELECT SETVAL('" + PG_SEQ_PREFIX + name + "',?,TRUE)");
        ps.setLong(1, newId);
        ps.execute();
    } catch (SQLException exc) {
        throw new FxDbException(LOG, exc, "ex.db.sqlError", exc.getMessage());
    } finally {
        Database.closeObjects(PostgreSQLSequencerStorage.class, con, ps);
    }
}

From source file:hermes.store.schema.DefaultJDBCAdapter.java

public void insert(Connection connection, String storeId, Message message) throws SQLException, JMSException {
    final String destinationName = message.getJMSDestination() == null ? "default"
            : JMSUtils.getDestinationName(message.getJMSDestination());
    final Domain domain = message.getJMSDestination() == null ? Domain.QUEUE
            : Domain.getDomain(message.getJMSDestination());
    final String messageAsXMLString = xmlHelper.toXML(message);
    final InputStream messageAsXML = new StringInputStream(messageAsXMLString);
    final String messageId = getNextMessageId(storeId);

    ////  w  ww  . j a va  2 s.c  o m
    // DBUtils does not seem to correctly deal with CLOBS, so we have to use
    // normal JDBC...
    //
    // runner.update(connection, "insert into messages values (?, ?)", new
    // Object[] { message.getJMSMessageID(), messageAsXML });

    final PreparedStatement pstmt = connection.prepareStatement("insert into messages values (?, ?)");

    pstmt.setString(1, messageId);
    pstmt.setAsciiStream(2, messageAsXML, messageAsXMLString.length());

    pstmt.execute();
    pstmt.close();

    final QueryRunner runner = new QueryRunner();

    runner.update(connection, "insert into stores values (?, ?, ?, ?)",
            new Object[] { storeId, destinationName, domain.getId(), messageId });
}

From source file:dk.netarkivet.harvester.datamodel.DerbySpecifics.java

/**
 * Dispose of a temporary table gotten with getTemporaryTable.  This can be
 * expected to be called from within a finally clause, so it mustn't throw
 * exceptions./*from   w w  w  .  jav a 2 s.c  o  m*/
 *
 * @param c The DB connection to use.
 * @param tableName The name of the temporary table
 */
public void dropJobConfigsTmpTable(Connection c, String tableName) {
    ArgumentNotValid.checkNotNull(c, "Connection c");
    ArgumentNotValid.checkNotNullOrEmpty(tableName, "String tableName");
    PreparedStatement s = null;
    try {
        // Now drop the temporary table
        s = c.prepareStatement("DROP TABLE " + tableName);
        s.execute();
    } catch (SQLException e) {
        log.warn("Couldn't drop temporary table " + tableName + "\n" + ExceptionUtils.getSQLExceptionCause(e),
                e);
    }
}

From source file:com.fluidops.iwb.wiki.WikiSQLStorageBase.java

@Override
protected boolean deleteFromStorage(URI resource) {
    PreparedStatement prep = null;
    try {/*w  w w.  ja  v a 2s  .c o m*/
        Connection conn = getConnection();
        prep = conn.prepareStatement("delete from revisions where name=?;");
        prep.setString(1, resource.stringValue());

        boolean res = prep.execute();
        SQL.monitorWrite();
        return res;
    } catch (SQLException e) {
        SQL.monitorWriteFailure();
        throw new RuntimeException("Delete revision failed.", e);
    } finally {
        SQL.closeQuietly(prep);
    }
}

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

public FactionMember(int playerId, int factionId) {
    _playerId = playerId;/*from   w  w  w.j  av  a2s  .c  o m*/
    _factionId = factionId;
    _factionPoints = 0;
    _contributions = 0;
    _joinDate = System.currentTimeMillis();
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement(
                "INSERT INTO faction_members (player_id, faction_id, faction_points, contributions, join_date) VALUES (?, ?, 0, 0, ?)");
        statement.setInt(1, _playerId);
        statement.setInt(2, _factionId);
        statement.setLong(3, _joinDate);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

public void deleteSkill(L2Skill skill) {
    if (skill == null)
        return;/*w  w w.  j  a  v  a  2  s.  c om*/

    final SkillMap map = getSkillMap();

    if (map.remove(skill) == null)
        return;

    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection();

        PreparedStatement statement = con.prepareStatement(
                "DELETE FROM character_skills WHERE skill_id=? AND charId=? AND class_index=?");
        statement.setInt(1, skill.getId());
        statement.setInt(2, getOwner().getObjectId());
        statement.setInt(3, getOwner().getClassIndex());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

public void setInstanceTime(int playerObjId, int id, long time) {
    if (!_playerInstanceTimes.containsKey(playerObjId))
        restoreInstanceTimes(playerObjId);
    Connection con = null;/*from  ww w.j a  v  a 2  s  . c o m*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement = null;
        statement = con.prepareStatement(ADD_INSTANCE_TIME);
        statement.setInt(1, playerObjId);
        statement.setInt(2, id);
        statement.setLong(3, time);
        statement.setLong(4, time);
        statement.execute();
        statement.close();
        _playerInstanceTimes.get(playerObjId).put(id, time);
    } catch (Exception e) {
        _log.warn("Could not insert character instance time data: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

public void updateHeroes(boolean setDefault) {
    Connection con = null;//w  w w . j  a  v  a  2s  .c  o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        if (setDefault) {
            try {
                PreparedStatement statement = con.prepareStatement(UPDATE_ALL);
                statement.execute();
                statement.close();
            } catch (SQLException e) {
                _log.warn("HeroSystem: Couldnt update all Heroes", e);
            }
        } else {
            PreparedStatement statement;

            for (Integer heroId : _heroes.keySet()) {
                StatsSet hero = _heroes.get(heroId);

                if (_completeHeroes == null || !_completeHeroes.containsKey(heroId)) {
                    try {
                        statement = con.prepareStatement(INSERT_HERO);
                        statement.setInt(1, heroId);
                        statement.setInt(2, hero.getInteger(Olympiad.CLASS_ID));
                        statement.setInt(3, hero.getInteger(COUNT));
                        statement.setInt(4, hero.getInteger(PLAYED));
                        statement.execute();
                        statement.close();

                        PreparedStatement statement2 = con.prepareStatement(GET_CLAN_ALLY);
                        statement2.setInt(1, heroId);
                        ResultSet rset2 = statement2.executeQuery();

                        initRelationBetweenHeroAndClan(rset2, hero);

                        rset2.close();
                        statement2.close();

                        _heroes.remove(heroId);
                        _heroes.put(heroId, hero);

                        _completeHeroes.put(heroId, hero);
                    } catch (SQLException e) {
                        _log.warn("HeroSystem: Couldnt insert Heroes", e);
                    }
                } else {
                    try {
                        statement = con.prepareStatement(UPDATE_HERO);
                        statement.setInt(1, hero.getInteger(COUNT));
                        statement.setInt(2, hero.getInteger(PLAYED));
                        statement.setInt(3, heroId);
                        statement.execute();
                        statement.close();
                    } catch (SQLException e) {
                        _log.warn("HeroSystem: Couldnt update Heroes", e);
                    }
                }
            }
        }
    } catch (Exception e) {
        _log.error("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

/**
 * Add device request. Takes parameters for deviceid, pubkey, as well as
 * the user info./*w w w .  j av a2  s . c o  m*/
 * @param userinfo
 * @param requestParams
 * @return
 * @throws IOException 
 * @throws SQLException 
 * @throws ClassNotFoundException 
 */
public static boolean processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String deviceid = requestParams.optString("deviceid");
    String pubkey = requestParams.optString("pubkey");

    /*
     * Attempt to insert a new user into the database
     */

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

    try {
        c = Database.get();

        /*
            * We now have the user index. Insert the device. Note that it is
            * highly unlikely we will have a UUID collision, but we verify
            * we don't by deleting any rows in the device table with the
            * specified UUID. The worse case scenario is a collision which
            * knocks someone else off the air. (The alternative would be
            * to accidentally send the wrong person duplicate messages.)
            * 
            * Note that we don't actually use a device-identifying identifer,
            * choosing instead to pick a UUID, so we need to deal with
            * the possibility (however remote) of duplicate UUIDs.
            * 
            * In the off chance we did have a collision, we also delete all
            * old messages to the device; that prevents messages from being
            * accidentally delivered.
            */

        ps = c.prepareStatement("DELETE FROM Messages " + "WHERE messageid IN "
                + "    (SELECT Messages.messageid " + "     FROM Messages, Devices "
                + "     WHERE Messages.deviceid = Devices.deviceid " + "     AND Devices.deviceuuid = ?)");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("DELETE FROM Devices WHERE deviceuuid = ?");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("INSERT INTO Devices " + "    ( userid, deviceuuid, publickey ) " + "VALUES "
                + "    ( ?, ?, ?)");
        ps.setInt(1, userinfo.getUserID());
        ps.setString(2, deviceid);
        ps.setString(3, pubkey);
        ps.execute();

        /*
         * Complete; return result
         */

        return true;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:genericepayadmin.AddIpBean.java

public void doInsertTax(Connection con, AddIpBean aip) throws Exception {
    PreparedStatement ps = null;
    String sql = "insert into webservice_validator(id,ipaddress,deptid,checksum) values(upper(uuid()),?,?,?)";
    try {/*from   w ww  . ja  v a 2s  .c om*/
        ps = con.prepareStatement(sql);
        ps.setString(1, aip.ipaddress);
        ps.setString(2, aip.deptid);
        ps.setString(3, aip.checksum);
        ps.execute();

    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw e;
    } finally {
        if (ps != null)
            ps.close();
    }
}