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.entity.faction.FactionMember.java

public void quitFaction() {
    Connection con = null;/* www . j  a  v  a2  s. c  o  m*/
    _factionId = 0;
    _factionPoints = 0;
    _contributions = 0;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM faction_members WHERE player_id=?");
        statement.setInt(1, _playerId);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.warn("Exception: FactionMember.quitFaction(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:br.com.mysqlmonitor.monitor.Monitor.java

private void adicionarLog(String descricao) {
    try {/*from   w w w  .ja v  a 2s  .  c  o  m*/
        logs.append(descricao + "<br/>");
        Connection con = conexaoJDBC.iniciarConexaoLocal();
        PreparedStatement pst = con
                .prepareStatement("INSERT INTO log_agente (DESCRICAO, DATA) values (?, now())");
        pst.setString(1, descricao);
        pst.execute();
        pst.close();
        con.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:ca.phon.ipadictionary.impl.DatabaseDictionary.java

@Override
public void removeEntry(String ortho, String ipa) throws IPADictionaryExecption {
    Connection conn = IPADatabaseManager.getInstance().getConnection();
    if (conn != null) {
        String qSt = "DELETE FROM transcript WHERE orthography = ?" + "  AND ipa = ? AND langId = ?";
        try {//  w w  w  . j av  a 2s.c  om
            PreparedStatement pSt = conn.prepareStatement(qSt);
            pSt.setString(1, ortho);
            pSt.setString(2, ipa);
            pSt.setString(3, getLanguage().toString());

            pSt.execute();
            pSt.close();
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }
}

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

/**
 *
 *//*from ww  w  .j av  a2s  .  c o  m*/
public void deleteme(Forum f) {
    TopicBBSManager.getInstance().delTopic(this);
    f.rmTopicByID(getID());
    Connection con = null;
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement = con
                .prepareStatement("DELETE FROM topic WHERE topic_id=? AND topic_forum_id=?");
        statement.setInt(1, getID());
        statement.setInt(2, f.getID());
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error(e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:com.symantec.wraith.silo.sql.TestSQLRulesStore.java

@Before
public void before() throws SQLException, IOException {
    System.setProperty("derby.stream.error.field", DerbyUtil.class.getCanonicalName() + ".DEV_NULL");
    File db = new File(TARGET_RULES_DB);
    if (db.exists()) {
        System.out.println("Deleting database");
        FileUtils.deleteDirectory(db);//  ww w. j a  v  a  2 s.  c o m
    }
    String createTable = "create table testRules(" + SQLRulesStore.COLUMN_RULE_ID + " smallint primary key,"
            + SQLRulesStore.COLUMN_RULE_CONTENT + " varchar(3000))";
    runSQL(CONNECTION_STRING, createTable);

    Condition condition = new JavaRegexCondition("tst", "\\d+");
    Action action = new TemplatedAlertAction((short) 2, (short) 2);
    Rule testRule = new SimpleRule((short) 1233, "testRule", true, condition, action);

    Connection conn = DriverManager.getConnection(CONNECTION_STRING);
    PreparedStatement insert = conn.prepareStatement("insert into testRules values(?, ?)");
    insert.setShort(1, testRule.getRuleId());
    insert.setString(2, RuleSerializer.serializeRuleToJSONString(testRule, false));
    insert.execute();
    conn.close();
}

From source file:com.oracle.tutorial.jdbc.DatalinkSample.java

public void addURLRow(String description, String url) throws SQLException {

    PreparedStatement pstmt = null;

    try {//  ww w . j  a v  a 2s .  c  o  m
        pstmt = this.con.prepareStatement("INSERT INTO data_repository(document_name,url) VALUES (?,?)");
        pstmt.setString(1, description);
        pstmt.setURL(2, new URL(url));
        pstmt.execute();
    } catch (SQLException sqlex) {
        JDBCTutorialUtilities.printSQLException(sqlex);
    } catch (Exception ex) {
        System.out.println("Unexpected exception");
        ex.printStackTrace();
    } finally {
        if (pstmt != null) {
            pstmt.close();
        }
    }
}

From source file:edu.lternet.pasta.dml.database.SimpleDatabaseLoader.java

/**
 * /*from  www  .j  a va 2s  .c om*/
 */
public void run() {

    if (entity == null) {
        success = false;
        completed = true;
        return;
    }

    //don't reload data if we have it
    if (doesDataExist(entity.getEntityIdentifier())) {
        return;
    }

    AttributeList attributeList = entity.getAttributeList();
    String tableName = entity.getDBTableName();

    String insertSQL = "";
    Vector rowVector = new Vector();
    Connection connection = null;

    try {
        rowVector = this.dataReader.getOneRowDataVector();
        connection = DataManager.getConnection();
        if (connection == null) {
            success = false;
            exception = new Exception("The connection to db is null");
            completed = true;
            return;
        }
        connection.setAutoCommit(false);
        while (!rowVector.isEmpty()) {
            insertSQL = databaseAdapter.generateInsertSQL(attributeList, tableName, rowVector);
            if (insertSQL != null) {
                PreparedStatement statement = connection.prepareStatement(insertSQL);
                statement.execute();
            }
            rowVector = this.dataReader.getOneRowDataVector();
        }

        connection.commit();
        success = true;
    } catch (Exception e) {
        log.error("problem while loading data into table.  Error message: " + e.getMessage());
        e.printStackTrace();
        log.error("SQL string to insert row:\n" + insertSQL);

        success = false;
        exception = e;

        try {
            connection.rollback();
        } catch (Exception ee) {
            System.err.println(ee.getMessage());
        }
    } finally {
        try {
            connection.setAutoCommit(true);
        } catch (Exception ee) {
            log.error(ee.getMessage());
        }

        DataManager.returnConnection(connection);
    }
}

From source file:com.dangdang.ddframe.rdb.sharding.example.config.spring.masterslave.repository.OrderRepositoryImpl.java

@Override
public void insert() {
    String orderSql = "INSERT INTO `t_order` (`order_id`, `user_id`, `status`) VALUES (?, ?, ?)";
    String orderItemSql = "INSERT INTO `t_order_item` (`order_item_id`, `order_id`, `user_id`, `status`) VALUES (?, ?, ?, ?)";
    for (int orderId = 1; orderId <= 4; orderId++) {
        for (int userId = 1; userId <= 2; userId++) {
            try (Connection connection = shardingDataSource.getConnection()) {
                PreparedStatement preparedStatement = connection.prepareStatement(orderSql);
                preparedStatement.setInt(1, orderId);
                preparedStatement.setInt(2, userId);
                preparedStatement.setString(3, "insert");
                preparedStatement.execute();
                preparedStatement.close();

                preparedStatement = connection.prepareStatement(orderItemSql);
                int orderItemId = orderId + 4;
                preparedStatement.setInt(1, orderItemId);
                preparedStatement.setInt(2, orderId);
                preparedStatement.setInt(3, userId);
                preparedStatement.setString(4, "insert");
                preparedStatement.execute();
                preparedStatement.close();
                // CHECKSTYLE:OFF
            } catch (final Exception ex) {
                // CHECKSTYLE:ON
                ex.printStackTrace();/* ww  w.j  a v  a2s.c  o  m*/
            }
        }
    }
}

From source file:com.redhat.victims.database.VictimsSQL.java

/**
 * Insert a new record with the given hash and return the record id.
 *
 * @param hash/*from   w ww.  ja  v a  2  s .co m*/
 * @return A record id if it was created correctly, else return -1.
 * @throws SQLException
 */
protected int insertRecord(Connection connection, String hash) throws SQLException {
    int id = -1;
    PreparedStatement ps = setObjects(connection, Query.INSERT_RECORD, hash);
    ps.execute();
    ResultSet rs = ps.getGeneratedKeys();
    try {
        while (rs.next()) {
            id = rs.getInt(1);
            break;
        }
    } finally {
        rs.close();
        ps.close();
    }

    return id;
}

From source file:net.sf.l2j.gameserver.model.entity.ClanHallSiege.java

public void setNewSiegeDate(long siegeDate, int ClanHallId, int hour) {
    Calendar tmpDate = Calendar.getInstance();
    if (siegeDate <= System.currentTimeMillis()) {
        tmpDate.setTimeInMillis(System.currentTimeMillis());
        tmpDate.add(Calendar.DAY_OF_MONTH, 3);
        tmpDate.set(Calendar.DAY_OF_WEEK, 6);
        tmpDate.set(Calendar.HOUR_OF_DAY, hour);
        tmpDate.set(Calendar.MINUTE, 0);
        tmpDate.set(Calendar.SECOND, 0);
        setSiegeDate(tmpDate);//from   w ww  .j  a v a2s  .  c  o m
        Connection con = null;
        try {
            con = L2DatabaseFactory.getInstance().getConnection();
            PreparedStatement statement = con
                    .prepareStatement("UPDATE clanhall_siege SET siege_data=? WHERE id = ?");
            statement.setLong(1, getSiegeDate().getTimeInMillis());
            statement.setInt(2, ClanHallId);
            statement.execute();
            statement.close();
        } catch (Exception e) {
            _log.error("Exception: can't save clanhall siege date: " + e.getMessage(), e);
        } finally {
            try {
                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}