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.github.brandtg.switchboard.TestMysqlLogServer.java

@Test
public void testSimpleWrites() throws Exception {
    try (Connection conn = DriverManager.getConnection(jdbc, "root", "")) {
        // Write some rows, so we have binlog entries
        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO simple VALUES(?, ?)");
        for (int i = 0; i < 10; i++) {
            pstmt.setInt(1, i);//  w w  w . j a v a2 s . co  m
            pstmt.setInt(2, i);
            pstmt.execute();
        }
    }

    pollAndCheck(serverAddress, "/log/test/0", 10, 10);
}

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

public void deleteSkills(Connection con, int classIndex) throws SQLException {
    PreparedStatement statement = con
            .prepareStatement("DELETE FROM character_skills WHERE charId=? AND class_index=?");
    statement.setInt(1, getOwner().getObjectId());
    statement.setInt(2, classIndex);/*w ww.j  av  a 2 s .c o  m*/
    statement.execute();
    statement.close();

    _storedSkills.remove(classIndex);
}

From source file:mx.com.pixup.portal.dao.DisqueraDaoJdbc.java

@Override
public Disquera insertDisquera(Disquera disquera) {
    Connection connection = DBConecta.getConnection();
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;/*from  w w  w.jav a  2s . c o m*/
    String sql = "insert into disquera (nombre) values (?)";
    try {

        preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        preparedStatement.setString(1, disquera.getNombre());
        preparedStatement.execute();

        resultSet = preparedStatement.getGeneratedKeys();
        resultSet.next();
        disquera.setId(resultSet.getInt(1));

        return disquera;
    } catch (Exception e) {
        Logger.getLogger(DBConecta.class.getName()).log(Level.SEVERE, null, e);
        return null;
    } finally {
        if (preparedStatement != null) {
            try {
                preparedStatement.close();
            } catch (Exception e) {
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
            }
        }
    }
}

From source file:de.nim.wscr.dao.MemberDAO.java

@Override
public void addMember(Member member) {
    try {/*from w w w.java 2 s  .co  m*/
        PreparedStatement statement = connection
                .prepareStatement("INSERT INTO db1.member (FIRST_NAME, LAST_NAME, LICENSE) VALUES(?, ?, ?)");
        statement.setString(1, member.getFirstName());
        statement.setString(2, member.getLastName());
        statement.setBoolean(3, member.getDriverLicense());
        statement.execute();

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

From source file:org.ulyssis.ipp.snapshot.Event.java

public void setRemoved(Connection connection, boolean removed) throws SQLException {
    if (!isRemovable()) {
        assert false; // This is a programming error
        return;//from  w  ww. j a v  a 2 s .c  om
    }
    PreparedStatement statement = connection
            .prepareStatement("UPDATE \"events\" SET \"removed\"=? WHERE \"id\"=?");
    statement.setBoolean(1, removed);
    statement.setLong(2, id);
    boolean result = statement.execute();
    assert (!result);
    this.removed = true;
}

From source file:de.klemp.middleware.controller.Controller.java

/**
 * With this method the devices can log out. Their names will be deleted
 * from the tables./*from  w ww  . j a v a  2s  . c  o  m*/
 * 
 * @param component
 *            1 or 2
 * @param classes
 *            name of the class of the device
 * @param name
 *            name, the device had subscribed.
 */
@GET
@Path("/abmelden/{component}/{classes}/{name}")
public static synchronized String unsubscribe(@PathParam("component") int component,
        @PathParam("classes") String classes, @PathParam("name") String name) {
    createDBConnection();
    String ok = "ok";
    name.replaceAll("\"", "\\\"");
    try {
        if (component == 1) {
            Statement statement = conn.createStatement();
            statement.execute("delete from\"" + classes + "\"where name='" + name + "');");
            PreparedStatement st = conn
                    .prepareStatement("delete from \"InputDevices\" where name=" + name + ";");
            st.setString(1, classes);
            st.setString(2, name);
            st.execute();
            if (st.getUpdateCount() != 1) {
                ok = "class or method not found";
            }

        }
        if (component == 2) {
            PreparedStatement st = conn
                    .prepareStatement("delete from \"OutputDevices\" where \"class\"=? and \"topic\"=?;");
            st.setString(1, classes);
            st.setString(2, name);
            st.execute();
            deviceActive.remove(classes + "," + name);
            if (st.getUpdateCount() != 1) {
                ok = "class or method not found";
            }

        }
    } catch (SQLException e) {
        logger.error("SQL Exception", e);

    }
    closeDBConnection();

    return ok;
}

From source file:adalid.util.sql.SqlUtil.java

protected boolean executeStatement(String statement) throws SQLException {
    try {/*from www  .j av a2  s  .co  m*/
        logger.debug(statement);
        PreparedStatement prepareStatement = _connection.prepareStatement(statement);
        return prepareStatement.execute();
    } catch (SQLException ex) {
        logger.fatal(statement, ex);
        throw ex;
    }
}

From source file:com.github.brandtg.switchboard.TestMysqlLogServer.java

@Test
public void testRotateBinlog() throws Exception {
    try (Connection conn = DriverManager.getConnection(jdbc, "root", "")) {
        // Write some rows, so we have binlog entries
        PreparedStatement pstmt = conn.prepareStatement("INSERT INTO simple VALUES(?, ?)");
        for (int i = 0; i < 10; i++) {
            pstmt.setInt(1, i);/*from w  w w . jav  a  2s. co  m*/
            pstmt.setInt(2, i);
            pstmt.execute();
        }

        // Rotate logs
        Statement stmt = conn.createStatement();
        stmt.execute("FLUSH LOGS");

        // Write more
        for (int i = 10; i < 20; i++) {
            pstmt.setInt(1, i);
            pstmt.setInt(2, i);
            pstmt.execute();
        }
    }

    pollAndCheck(serverAddress, "/log/test/0?count=100", 20, 20);
}

From source file:com.wso2telco.dep.verificationhandler.verifier.DatabaseUtils.java

/**
 * Update subscription numbers.//from  ww  w.j  a va 2  s.c om
 *
 * @param subscriber the subscriber
 * @param app the app
 * @param api the api
 * @param updatedSubscriberCount the updated subscriber count
 * @throws APIManagementException the API management exception
 */
public static void UpdateSubscriptionNumbers(String subscriber, String app, String api,
        String updatedSubscriberCount) throws APIManagementException {
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet results = null;
    String sql = "update subscriptionCount set " + "subscriptionCount=? where " + "userId=? AND " + "api=? AND "
            + "applicationName=?;";
    try {
        try {
            connection = getStatsDBConnection();
        } catch (NamingException ex) {
            Logger.getLogger(DatabaseUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        ps = connection.prepareStatement(sql);
        ps.setString(1, updatedSubscriberCount);
        ps.setString(2, subscriber);
        ps.setString(3, api);
        ps.setString(4, app);

        ps.execute();

    } catch (SQLException e) {
        handleException("Error occurred while getting Invocation count for Application", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, results);
    }

}

From source file:Crawler.CrawlerClass.java

public void InsertToIndexDb(DBConnection Conn, String url, String[] Keywords) throws SQLException, IOException {

    String sql = "select * from contentdb where URL = '" + url + "'";
    ResultSet rs = Conn.executeStatement(sql);

    if (rs.next()) {
        //store the URL to database to avoid parsing again
        int ID = rs.getInt("ID");
        sql = "INSERT INTO `keyworddb`(`ID`, `Keyword`) " + "VALUES(?,?);";
        PreparedStatement stmt = Conn.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

        for (String words : Keywords) {
            stmt.setInt(1, ID);/*from w  w  w .ja  v a  2  s  .c  o  m*/
            stmt.setString(2, words.trim());
        }

        stmt.execute();

    }
}