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.util.OfflineTradeManager.java

public void store() {
    _log.info("OfflineTradeManager: Storring...");
    _playerCount = 0;/*from  ww  w.  ja va 2s.co  m*/
    _itemCount = 0;
    _recipeCount = 0;

    cleanTables();
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        for (L2Player p : L2World.getInstance().getAllPlayers()) {
            try {
                if (p.isInOfflineMode()) {
                    final int privateStoreType = p.getPrivateStoreType();
                    TradeList tradeList = null;
                    L2ManufactureList manufactureList = null;

                    switch (privateStoreType) {
                    case L2Player.STORE_PRIVATE_SELL:
                    case L2Player.STORE_PRIVATE_PACKAGE_SELL:
                        tradeList = p.getSellList();
                        for (TradeItem i : tradeList.getItems()) {
                            PreparedStatement st2 = con
                                    .prepareStatement("INSERT INTO offline_traders_items VALUES(?,?,?,?)");
                            st2.setInt(1, p.getObjectId());
                            st2.setInt(2, i.getObjectId());
                            st2.setLong(3, i.getCount());
                            st2.setLong(4, i.getPrice());
                            st2.execute();
                            st2.close();

                            _itemCount++;
                        }
                        break;
                    case L2Player.STORE_PRIVATE_BUY:
                        tradeList = p.getBuyList();
                        for (TradeItem i : tradeList.getItems()) {
                            PreparedStatement st2 = con
                                    .prepareStatement("INSERT INTO offline_traders_items VALUES(?,?,?,?)");
                            st2.setInt(1, p.getObjectId());
                            st2.setInt(2, i.getItem().getItemId());
                            st2.setLong(3, i.getCount());
                            st2.setLong(4, i.getPrice());
                            st2.execute();
                            st2.close();

                            _itemCount++;
                        }
                        break;
                    case L2Player.STORE_PRIVATE_MANUFACTURE:
                        manufactureList = p.getCreateList();
                        for (L2ManufactureItem i : manufactureList.getList()) {
                            PreparedStatement st2 = con
                                    .prepareStatement("INSERT INTO offline_traders_items VALUES(?,?,?,?)");
                            st2.setInt(1, p.getObjectId());
                            st2.setInt(2, i.getRecipeId());
                            st2.setLong(3, -1);
                            st2.setLong(4, i.getCost());
                            st2.execute();
                            st2.close();

                            _recipeCount++;
                        }
                        break;
                    }
                    PreparedStatement st = con.prepareStatement("INSERT INTO offline_traders VALUES(?,?,?)");
                    st.setInt(1, p.getObjectId());
                    st.setString(2, String.valueOf(privateStoreType));
                    switch (privateStoreType) {
                    case L2Player.STORE_PRIVATE_MANUFACTURE:
                        if (manufactureList != null)
                            st.setString(3, manufactureList.getStoreName());
                        else
                            st.setString(3, "");
                        break;
                    case L2Player.STORE_PRIVATE_SELL:
                    case L2Player.STORE_PRIVATE_PACKAGE_SELL:
                    case L2Player.STORE_PRIVATE_BUY:
                        if (tradeList != null)
                            st.setString(3, tradeList.getTitle());
                        else
                            st.setString(3, "");
                        break;
                    default:
                        st.setString(3, "");
                    }

                    st.execute();
                    st.close();

                    _playerCount++;
                }
                //new Disconnection(p).defaultSequence(true);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    } catch (Exception e) {
        _log.error("OfflineTradeManager: Could not store char private store list: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
    _log.info("OfflineTradeManager: Stored " + _playerCount + " offline traders with " + _itemCount
            + " items and " + _recipeCount + " recipes!");
}

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

public void saveNpc(StatsSet npc) {
    Connection con = null;//from  w ww .j a v a2s.c o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection();
        Map<String, Object> set = npc.getSet();

        String name = "";
        String values = "";

        for (Object obj : set.keySet()) {
            name = (String) obj;

            if (name.equalsIgnoreCase("npcId"))
                continue;

            if (values != "")
                values += ", ";

            values += name + " = '" + set.get(name) + "'";
        }

        String query = "UPDATE npc SET " + values + " WHERE id = ?";
        String query_custom = "UPDATE custom_npc SET " + values + " WHERE id = ?";

        try {
            PreparedStatement statement = con.prepareStatement(query);
            statement.setInt(1, npc.getInteger("npcId"));
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.warn("", e);
        }

        try {
            PreparedStatement statement = con.prepareStatement(query_custom);
            statement.setInt(1, npc.getInteger("npcId"));
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.warn("", e);
        }
    } catch (Exception e) {
        _log.warn("NPCTable: Could not store new NPC data in database: ", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

/** Cancel bid */
public synchronized void cancelBid(int bidder) {
    Connection con = null;//  w ww  .j  a  v  a  2  s .  c o  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM auction_bid WHERE auctionId=? AND bidderId=?");
        statement.setInt(1, getId());
        statement.setInt(2, bidder);
        statement.execute();

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

    returnItem(_bidders.get(bidder).getClanName(), PlayerInventory.ADENA_ID, _bidders.get(bidder).getBid(),
            true);
    ClanTable.getInstance().getClanByName(_bidders.get(bidder).getClanName()).setAuctionBiddedAt(0, true);
    _bidders.clear();
    loadBid();
}

From source file:com.splicemachine.derby.impl.load.HdfsUnsafeImportIT.java

/**
 * Worker method for import tests related to CSV files that are missing the end quote for a quoted column.
 *
 * @param schemaName     table schema//from w ww. j av a2s  .c om
 * @param tableName      table name
 * @param importFilePath full path to the import file
 * @param colList        list of columns and their order
 * @param badDir         where to place the error file
 * @param failErrorCount how many errors do we allow before failing the whole import
 * @param importCount    verification of number of rows imported
 * @param oneLineRecords whether the import file has one record per line or records span lines
 * @throws Exception
 */
private void testMissingEndQuoteForQuotedColumn(String schemaName, String tableName, String importFilePath,
        String colList, String badDir, int failErrorCount, int importCount, String oneLineRecords)
        throws Exception {
    methodWatcher.executeUpdate("delete from " + schemaName + "." + tableName);
    PreparedStatement ps = methodWatcher.prepareStatement(format(
            "call SYSCS_UTIL.IMPORT_DATA_UNSAFE('%s','%s','%s','%s',',',null,null,null,null,%d,'%s','%s',null)",
            schemaName, tableName, colList, importFilePath, failErrorCount, badDir, oneLineRecords));
    ps.execute();
    ResultSet rs = methodWatcher.executeQuery(format("select * from %s.%s", schemaName, tableName));
    List<String> results = Lists.newArrayList();
    while (rs.next()) {
        String name = rs.getString(1);
        String title = rs.getString(2);
        int age = rs.getInt(3);
        Assert.assertTrue("age was null!", !rs.wasNull());
        assertNotNull("name is null!", name);
        assertNotNull("title is null!", title);
        results.add(String.format("name:%s,title:%s,age:%d", name, title, age));
    }
    Assert.assertEquals("Incorrect number of rows imported", importCount, results.size());
}

From source file:br.com.fatecpg.repositories.mysql.MySqlLogRepository.java

@Override
public void add(Log log) {
    if (log == null) {
        throw new IllegalArgumentException("log can't be null.");
    }//from  w ww. ja va 2  s  .  co  m

    String sql = "insert into log (applicationName, message, url, ipaddress, username, createdon, details) values (?, ?, ?, ?, ?, ?, ?)";
    Connection connection = dbProvider.getConnection();

    try {

        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, log.getApplicationName());
        preparedStatement.setString(2, log.getMessage());
        preparedStatement.setString(3, log.getUrl());
        preparedStatement.setString(4, log.getIpAddress());
        preparedStatement.setString(5, log.getUsername());

        Date sqlDate = new Date(log.getCreatedDate().getYear(), log.getCreatedDate().getMonth(),
                log.getCreatedDate().getDay());

        preparedStatement.setDate(6, sqlDate);
        preparedStatement.setString(7, log.getDetails());

        preparedStatement.execute();

    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.microsoftopentechnologies.azchat.web.dao.UserDAOImpl.java

/**
 * This method executes create user table query on the azure SQL database.
 *//*from   w  ww .  ja  v  a2s  .  c om*/
@Override
public void createUserTable() throws Exception {
    Connection connection = null;
    Connection connection1 = null;
    PreparedStatement preparedStatement = null;
    PreparedStatement preparedStatement1 = null;
    try {
        connection = AzureChatUtils.getConnection(AzureChatUtils.buildConnectionString());
        preparedStatement = connection.prepareStatement(AzureChatSQLConstants.CREATE_USER_TABLE);
        preparedStatement.execute();
        connection1 = AzureChatUtils.getConnection(AzureChatUtils.buildConnectionString());
        preparedStatement1 = connection1.prepareStatement(AzureChatSQLConstants.CREATE_USER_TABLE_INDEX);
        preparedStatement1.execute();
    } catch (Exception e) {
        LOGGER.error(
                "Exception occurred while executing create user table query on the azure SQL database. Exception Message : "
                        + e.getMessage());
        throw new AzureChatSystemException(
                "Exception occurred while executing create user table query on the azure SQL database. Exception Message : "
                        + e.getMessage());
    } finally {
        AzureChatUtils.closeDatabaseResources(preparedStatement, connection);
        AzureChatUtils.closeDatabaseResources(preparedStatement1, connection1);

    }
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownLocation wkl) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String
            .format("INSERT into %s (id, name, description, updated_by) VALUES (?, ?, ?, ?)", wkl.tableName()));
    try {//from  w w  w .  j a  v  a  2  s  . c o  m
        s.setString(1, wkl.id());
        s.setString(2, wkl.wkoName());
        s.setString(3, wkl.description());
        s.setString(4, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert  %s", wkl), e);
    }
}

From source file:com.chiralbehaviors.CoRE.kernel.Bootstrap.java

public void insert(WellKnownObject wko) throws SQLException {
    PreparedStatement s = connection.prepareStatement(String
            .format("INSERT into %s (id, name, description, updated_by) VALUES (?, ?, ?, ?)", wko.tableName()));
    try {/*from ww w  .  ja va 2 s.c om*/
        s.setString(1, wko.id());
        s.setString(2, wko.wkoName());
        s.setString(3, wko.description());
        s.setString(4, WellKnownAgency.CORE.id());
        s.execute();
    } catch (SQLException e) {
        throw new SQLException(String.format("Unable to insert %s", wko), e);
    }
}

From source file:lineage2.gameserver.tables.ClanTable.java

/**
 * Method deleteAllyFromDb.//from  w  w w.jav a2 s .  c  o m
 * @param allyId int
 */
public void deleteAllyFromDb(int allyId) {
    Connection con = null;
    PreparedStatement statement = null;
    try {
        con = DatabaseFactory.getInstance().getConnection();
        statement = con.prepareStatement("UPDATE clan_data SET ally_id=0 WHERE ally_id=?");
        statement.setInt(1, allyId);
        statement.execute();
        DbUtils.close(statement);
        statement = con.prepareStatement("DELETE FROM ally_data WHERE ally_id=?");
        statement.setInt(1, allyId);
        statement.execute();
    } catch (Exception e) {
        _log.warn("could not dissolve clan:" + e);
    } finally {
        DbUtils.closeQuietly(con, statement);
    }
}

From source file:com.webpagebytes.cms.local.WPBLocalDataStoreDao.java

@SuppressWarnings("rawtypes")
public boolean deleteRecords(Class kind) throws SQLException {
    String tableName = kind.getSimpleName();
    String queryString = String.format("DELETE FROM %s", tableName);

    Connection connection = getConnection();
    PreparedStatement preparedStatement = null;
    try {// w ww  .j  a v  a2s .  co m
        preparedStatement = connection.prepareStatement(queryString);
        return preparedStatement.execute();

    } catch (SQLException e) {
        throw e;
    } finally {
        if (preparedStatement != null) {
            preparedStatement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}