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.dangdang.ddframe.rdb.sharding.example.config.spring.masterslave.repository.OrderRepositoryImpl.java

@Override
public void delete() {
    String orderSql = "DELETE FROM `t_order`";
    String orderItemSql = "DELETE FROM `t_order_item`";
    try (Connection connection = shardingDataSource.getConnection()) {
        PreparedStatement preparedStatement = connection.prepareStatement(orderSql);
        preparedStatement.execute();
        preparedStatement.close();/*  w ww .  j  ava 2s . c  om*/
        preparedStatement = connection.prepareStatement(orderItemSql);
        preparedStatement.execute();
        preparedStatement.close();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        ex.printStackTrace();
    }
}

From source file:com.tethrnet.manage.db.UserDB.java

/**
 * inserts new user/*  ww w  .j  a v a2  s . c o m*/
 * 
 * @param con DB connection 
 * @param user user object
 */
public static Long insertUser(Connection con, User user) {

    Long userId = null;

    try {
        PreparedStatement stmt = con.prepareStatement(
                "insert into users (email, username, auth_type, user_type, password, salt) values (?,?,?,?,?,?)",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, user.getEmail());
        stmt.setString(2, user.getUsername());
        stmt.setString(3, user.getAuthType());
        stmt.setString(4, user.getUserType());
        if (StringUtils.isNotEmpty(user.getPassword())) {
            String salt = EncryptionUtil.generateSalt();
            stmt.setString(5, EncryptionUtil.hash(user.getPassword() + salt));
            stmt.setString(6, salt);
        } else {
            stmt.setString(5, null);
            stmt.setString(6, null);
        }
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs != null && rs.next()) {
            userId = rs.getLong(1);
        }
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    }

    return userId;

}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void incrementPinAttempts(String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set attempts=attempts +1 where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, sessionId);//  w  w w. j  a va  2s.  c o  m
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

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

/**
 * Process a forgot password request. This generates a token that the
 * client is expected to return with the change password request.
 * @param requestParams//from   w  ww. j a v a  2  s  . c om
 * @throws SQLException 
 * @throws IOException 
 * @throws ClassNotFoundException 
 * @throws JSONException 
 * @throws NoSuchAlgorithmException 
 */

public static void processRequest(JSONObject requestParams)
        throws SQLException, ClassNotFoundException, IOException, NoSuchAlgorithmException, JSONException {
    String username = requestParams.optString("username");

    /*
     * Step 1: Convert username to the userid for this
     */
    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    int userID = 0;
    String retryID = UUID.randomUUID().toString();

    try {
        c = Database.get();
        ps = c.prepareStatement("SELECT userid " + "FROM Users " + "WHERE username = ?");
        ps.setString(1, username);
        rs = ps.executeQuery();
        if (rs.next()) {
            userID = rs.getInt(1);
        }

        if (userID == 0)
            return;
        ps.close();
        rs.close();

        /*
         * Step 2: Generate the retry token and insert into the forgot 
         * database with an expiration date 1 hour from now.
         */

        Timestamp ts = new Timestamp(System.currentTimeMillis() + 3600000);
        ps = c.prepareStatement("INSERT INTO ForgotPassword " + "    ( userid, token, expires ) " + "VALUES "
                + "    ( ?, ?, ?)");
        ps.setInt(1, userID);
        ps.setString(2, retryID);
        ps.setTimestamp(3, ts);
        ps.execute();
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }

    /*
     * Step 3: formulate a JSON string with the retry and send
     * to the user. The format of the command we send is:
     * 
     * { "cmd": "forgotpassword", "token": token }
     */

    JSONObject obj = new JSONObject();
    obj.put("cmd", "forgotpassword");
    obj.put("token", retryID);
    MessageQueue.getInstance().enqueueAdmin(userID, obj.toString(4));
}

From source file:h2backup.H2BackupTestBase.java

protected void insertRecordIntoTestTable(String description) throws SQLException {
    try (Connection conn = getConnection()) {
        PreparedStatement ps = conn.prepareStatement("INSERT INTO test_tbl (description) VALUES (?)");
        ps.setString(1, description);//from  ww w. j av  a 2  s. c om
        ps.execute();
    }
}

From source file:org.openxdata.test.BaseContextSensitiveTest.java

private void executeSql(String sql) throws SQLException {
    Connection connection = getConnection();
    PreparedStatement statement = connection.prepareStatement(sql);
    try {//from w w  w  . jav  a2 s .co m
        statement.execute();
    } finally {
        statement.close();
    }
}

From source file:com.wso2.raspberrypi.Util.java

public static void reservePi(String owner, String macAddress) {
    System.out.println("Changing owner of RPi " + macAddress + " to " + owner);
    BasicDataSource ds = getBasicDataSource();
    Connection dbConnection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;/*  w w  w  .ja  v  a 2  s .  c  om*/
    try {
        dbConnection = ds.getConnection();
        prepStmt = dbConnection.prepareStatement("SELECT * FROM RASP_PI where mac='" + macAddress + "'");
        rs = prepStmt.executeQuery();
        boolean alreadyOwned = false;
        while (rs.next()) {
            String oldOwner = rs.getString("owner");
            if (oldOwner != null && !oldOwner.isEmpty()) {
                alreadyOwned = true;
            }
        }
        if (!alreadyOwned) {
            prepStmt = dbConnection.prepareStatement(
                    "UPDATE RASP_PI SET owner='" + owner + "' where mac='" + macAddress + "'");
            prepStmt.execute();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (dbConnection != null) {
                dbConnection.close();
            }
            if (prepStmt != null) {
                prepStmt.close();
            }
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updateIdsRegStatus(String username, String status)
        throws SQLException, AuthenticatorException {
    Connection connection;/*from   w w w  .  j a  v a  2s  .  c om*/
    PreparedStatement ps;

    String sql = "update `regstatus` set " + "status=? where " + "username=?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, status);
    ps.setString(2, username);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}

From source file:h2backup.H2BackupTestBase.java

protected void createTestTable() throws SQLException {
    try (Connection conn = getConnection()) {
        PreparedStatement ps = conn.prepareStatement(
                "CREATE TABLE test_tbl ( id IDENTITY PRIMARY KEY, description VARCHAR(255) )");
        ps.execute();
    }/*from  w  w w .  ja  v  a2  s  . c  o  m*/
}

From source file:com.wso2telco.gsma.authenticators.DBUtils.java

public static void updatePin(int pin, String sessionId) throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "update multiplepasswords set pin=? where ussdsessionid = ?;";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setInt(1, pin);//from   ww w.  j  a  v  a  2 s. c  o  m
    ps.setString(2, sessionId);
    log.info(ps.toString());
    ps.execute();

    if (connection != null) {
        connection.close();
    }
}