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:javasnack.flywaydb.FlywaydbDemo1Test.java

@BeforeTest
public void prepareDb() throws Exception {
    Class.forName("org.h2.Driver");
    conn = DriverManager.getConnection("jdbc:h2:mem:flywaydb_demo1", "sa", "");
    // create test table.
    PreparedStatement ps = conn
            .prepareStatement("create table t1(id integer auto_increment primary key, name varchar, age int)");
    ps.execute();
    ps.close();/*from  www .j a  va2s. c  o m*/
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public boolean fileExists(final String name) throws IOException {
    return ((Boolean) jdbcTemplate.execute(table.sqlSelectNameExists(), new PreparedStatementCallback() {

        @Override/*from  ww  w. j  ava  2  s.  c  om*/
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).booleanValue();
}

From source file:org.apache.lucene.store.jdbc.handler.AbstractFileEntryHandler.java

public long fileModified(final String name) throws IOException {
    return ((Long) jdbcTemplate.execute(table.sqlSelecltLastModifiedByName(), new PreparedStatementCallback() {

        @Override//from  www .ja  v a  2 s .  c  o m
        public Object doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
            ps.setFetchSize(1);
            ps.setString(1, name);
            return ps.execute();
        }
    })).longValue();
}

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

private void checkLangId(Connection conn) throws SQLException {
    final String checkSQL = "SELECT * FROM language WHERE langId = ?";
    final PreparedStatement checkSt = conn.prepareStatement(checkSQL);
    checkSt.setString(1, getLanguage().toString());
    final ResultSet checkRs = checkSt.executeQuery();

    if (!checkRs.next()) {
        final String insertSQL = "INSERT INTO language (langId) VALUES ( ? )";
        final PreparedStatement insertSt = conn.prepareStatement(insertSQL);
        insertSt.setString(1, getLanguage().toString());
        insertSt.execute();
        insertSt.close();/*  w  w  w. j  a v a 2s .  c o  m*/
    }
    checkSt.close();
}

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

/** Init Clan NPC aution */
public void initNPC(int id) {
    Connection con = null;/*www . j av  a  2s.  c o  m*/
    int found = -1;
    for (int i = 0; i < ITEM_INIT_IDS.length; i++) {
        if (ITEM_INIT_IDS[i] == id) {
            found = i;
            break;
        }
    }

    if (found == -1) {
        _log.warn("Clan Hall auction not found for Id :" + id);
        return;
    }
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;
        statement = con.prepareStatement("INSERT INTO `auction` VALUES " + ITEM_INIT_DATA[found]);
        statement.execute();
        statement.close();
        _auctions.add(new Auction(id));
    } catch (Exception e) {
        _log.fatal("Exception: Auction.initNPC(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

From source file:net.mms_projects.copy_it.api.http.pages.v1.ClipboardUpdate.java

public FullHttpResponse onPostRequest(final HttpRequest request,
        final HttpPostRequestDecoder postRequestDecoder, final Database database,
        final HeaderVerifier headerVerifier) throws Exception {
    if (!headerVerifier.getConsumerScope().canWrite() || !headerVerifier.getUserScope().canWrite())
        throw new ErrorException(NO_WRITE_PERMISSION);
    final InterfaceHttpData data = postRequestDecoder.getBodyHttpData("data");
    if (data == null)
        throw new ErrorException(MISSING_DATA_PARAMETER);
    if (data instanceof HttpData) {
        PreparedStatement statement = database.getConnection().prepareStatement(INSERT_DATA);
        statement.setInt(1, headerVerifier.getUserId());
        statement.setString(2, ((HttpData) data).getString());
        statement.execute();
    } else/*from  w  w  w.jav  a  2  s .c  om*/
        throw new ErrorException(MISSING_DATA_PARAMETER);
    dispatchNotification(database, headerVerifier.getUserId());
    final JSONObject json = new JSONObject();
    return new DefaultFullHttpResponse(request.getProtocolVersion(), OK,
            Unpooled.copiedBuffer(json.toString(), CharsetUtil.UTF_8));
}

From source file:com.l2jfree.gameserver.idfactory.BitSetRebuildFactory.java

public synchronized void initialize() {
    _log.info("starting db rebuild, good luck");
    _log.info("this will take a while, dont kill the process or power off youre machine!");
    try {// w  w w  .j a va2s. c  o m
        _freeIds = new BitSet(PrimeFinder.nextPrime(100000));
        _freeIds.clear();
        _freeIdCount = new AtomicInteger(FREE_OBJECT_ID_SIZE);
        List<Integer> used_ids = new FastList<Integer>();
        // first get all used ids
        for (int usedObjectId : extractUsedObjectIDTable())
            used_ids.add(usedObjectId);

        _nextFreeId = new AtomicInteger(_freeIds.nextClearBit(0));

        Connection con = null;
        con = L2DatabaseFactory.getInstance().getConnection(con);
        int nextid;
        int changedids = 0;
        // now loop through all already used oids and assign a new clean one
        for (int i : extractUsedObjectIDTable()) {
            for (;;) //danger ;)
            {
                nextid = getNextId();
                if (!used_ids.contains(nextid))
                    break;
            }
            for (String update : ID_UPDATES) {
                PreparedStatement ps = con.prepareStatement(update);
                ps.setInt(1, nextid);
                ps.setInt(2, i);
                ps.execute();
                ps.close();
                changedids++;
            }
        }
        _log.info(
                "database rebuild done, changed " + changedids + " ids, set idfactory config to BitSet! ^o^/");
        System.exit(0);
    } catch (Exception e) {
        _log.fatal("could not rebuild database! :", e);
        System.exit(0);
    }
}

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

public synchronized void insert(L2Player listOwner, L2Player blocked) {
    Connection con = null;/*from  w  w w . ja v a  2s .com*/
    try {
        con = L2DatabaseFactory.getInstance().getConnection();

        PreparedStatement statement = con.prepareStatement(INSERT_QUERY);
        statement.setInt(1, listOwner.getObjectId());
        statement.setString(2, blocked.getName());

        statement.execute();

        statement.close();
    } catch (SQLException e) {
        _log.warn("", e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

public void divorce() {
    Connection con = null;//ww w.  jav a2  s .  co  m
    try {
        con = L2DatabaseFactory.getInstance().getConnection(con);
        PreparedStatement statement;

        statement = con.prepareStatement("DELETE FROM couples WHERE id=?");
        statement.setInt(1, _id);
        statement.execute();
        statement.close();
    } catch (Exception e) {
        _log.error("Exception: Couple.divorce(): " + e.getMessage(), e);
    } finally {
        L2DatabaseFactory.close(con);
    }
}

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

public void divorce() {
    java.sql.Connection con = null;
    try {/*from w w w . j  ava  2 s. c  o m*/
        con = L2DatabaseFactory.getInstance().getConnection();
        PreparedStatement statement;
        statement = con.prepareStatement("DELETE FROM couples WHERE id=?");
        statement.setInt(1, _Id);
        statement.execute();
    } catch (Exception e) {
        _log.error("Exception: Couple.divorce(): " + e.getMessage(), e);
    } finally {
        try {
            con.close();
        } catch (Exception e) {
        }
    }
}