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.hangum.tadpole.db.bander.oracle.OracleExecutePlanUtils.java

/**
 * oracle query plan? . //from w w  w .  j av a 2s . co m
 * 
 * @param userDB
 * @param sql
 * @param planTableName
 * @throws Exception
 */
public static void plan(UserDBDAO userDB, String sql, String planTableName, java.sql.Connection javaConn,
        PreparedStatement stmt, String statement_id) throws Exception {

    String query = PartQueryUtil.makeExplainQuery(userDB, sql);
    query = StringUtils.replaceOnce(query, PublicTadpoleDefine.STATEMENT_ID, statement_id);
    query = StringUtils.replaceOnce(query, PublicTadpoleDefine.DELIMITER, planTableName);

    stmt = javaConn.prepareStatement(query);
    stmt.execute();

}

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

public static boolean processRequest(Login.UserInfo userinfo, JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String newPassword = requestParams.getString("password");
    String requestToken = requestParams.getString("token");

    /*//from   ww  w. j  a  v  a2  s.  c om
     * Determine if the token matches for this user record. We are in the
     * unique situation of having a logged in user, but he doesn't know
     * his password. We also ignore any requests with an expired
     * token.
     */

    Connection c = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        /*
         * Delete old requests
         */
        c = Database.get();
        ps = c.prepareStatement("DELETE FROM forgotpassword WHERE expires < LOCALTIMESTAMP");
        ps.execute();

        ps.close();
        ps = null;

        /*
         * Verify the token we passed back was correct
         */
        ps = c.prepareStatement(
                "SELECT token " + "FROM forgotpassword " + "WHERE userid = ? " + "AND token = ?");
        ps.setInt(1, userinfo.getUserID());
        ps.setString(2, requestToken);
        rs = ps.executeQuery();
        if (!rs.next())
            return false; // token does not exist or expired.

        rs.close();
        rs = null;
        ps.close();
        ps = null;

        /*
         * Step 2: Modify the password.
         */

        ps = c.prepareStatement("UPDATE Users SET password = ? WHERE userid = ?");
        ps.setString(1, newPassword);
        ps.setInt(2, userinfo.getUserID());
        ps.execute();

        return true;
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}

From source file:com.wso2telco.dao.TransactionDAO.java

/**
 * Insert sub value./*from  ww w  .j  av  a  2s  .  com*/
 *
 * @param token the context id
 * @param sub   the status code
 * @throws Exception the exception
 */
public static void insertTokenScopeLog(String token, String sub) throws Exception {
    Connection conn = null;
    PreparedStatement ps = null;
    try {
        conn = DbUtil.getConnectDBConnection();
        String query = "INSERT INTO scope_log ( access_token,sub) VALUES " + "(? ,?);";
        ps = conn.prepareStatement(query);
        ps.setString(1, token);
        ps.setString(2, sub);
        ps.execute();
        log.debug("Sub value inserted successfully");
    } catch (SQLException e) {
        handleException("Error in inserting transaction log record : " + e.getMessage(), e);
    } finally {
        DbUtil.closeAllConnections(ps, conn, null);
    }
}

From source file:com.keybox.manage.db.SystemStatusDB.java

/**
 * deletes all records from status table for user
 *
 * @param con DB connection object/*from   w w  w  .j av  a2 s  .com*/
 * @param userId user id
 */
private static void deleteAllSystemStatus(Connection con, Long userId) {

    try {

        PreparedStatement stmt = con.prepareStatement("delete from status where user_id=?");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

From source file:com.freemedforms.openreact.db.DbSchema.java

/**
 * Determine if a patch has been applied yet.
 * /*  w  w w.  ja v  a 2 s  .  c om*/
 * @param patchName
 * @return Success.
 */
public static boolean isPatchApplied(String patchName) {
    Connection c = Configuration.getConnection();

    int found = 0;

    PreparedStatement cStmt = null;
    try {
        cStmt = c.prepareStatement("SELECT COUNT(*) FROM tPatch " + " WHERE patchName = ? " + ";");
        cStmt.setString(1, patchName);

        boolean hadResults = cStmt.execute();
        if (hadResults) {
            ResultSet rs = cStmt.getResultSet();
            rs.next();
            found = rs.getInt(1);
            rs.close();
        }
    } catch (NullPointerException npe) {
        log.error("Caught NullPointerException", npe);
    } catch (Throwable e) {
    } finally {
        DbUtil.closeSafely(cStmt);
        DbUtil.closeSafely(c);
    }

    return (boolean) (found > 0);
}

From source file:com.freemedforms.openreact.db.DbSchema.java

/**
 * Record record of patch into tPatch table so that patches only run once.
 * //from w w  w.  j ava  2s  .c  o m
 * @param patchName
 * @return Success.
 */
public static boolean recordPatch(String patchName) {
    Connection c = Configuration.getConnection();

    boolean status = false;
    PreparedStatement cStmt = null;
    try {
        cStmt = c.prepareStatement(
                "INSERT INTO tPatch " + " ( patchName, stamp ) " + " VALUES ( ?, NOW() ) " + ";");
        cStmt.setString(1, patchName);

        cStmt.execute();
        status = true;
    } catch (NullPointerException npe) {
        log.error("Caught NullPointerException", npe);
    } catch (SQLException sq) {
        log.error("Caught SQLException", sq);
    } catch (Throwable e) {
    } finally {
        DbUtil.closeSafely(cStmt);
        DbUtil.closeSafely(c);
    }

    return status;
}

From source file:com.keybox.manage.db.ProfileDB.java

/**
 * inserts new profile/*  w  ww  . j a  v a2s  .c  o m*/
 *
 * @param profile profile object
 */
public static void insertProfile(Profile profile) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("insert into profiles (nm, desc) values (?,?)");
        stmt.setString(1, profile.getNm());
        stmt.setString(2, profile.getDesc());
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        log.error(e.toString(), e);
    } finally {
        DBUtils.closeConn(con);
    }
}

From source file:com.dynamobi.ws.util.DB.java

@SuppressWarnings(value = { "unchecked" })
public static <T extends DBLoader> void execute(String query, T obj, List<T> list) {
    Connection conn = null;// w w  w. j a  v a2 s  .  c o m
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        conn = getConnection();
        ps = conn.prepareStatement(query);
        ps.setMaxRows(0);
        if (ps.execute()) {
            rs = ps.getResultSet();
        }

        while (rs != null && rs.next()) {
            obj.loadRow(rs);
            if (list != null) {
                list.add((T) obj.copy());
            }
        }
        obj.finalize();
    } catch (SQLException ex) {
        obj.exception(ex);
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (conn != null) {
            releaseConnection();
        }
        try {
            if (ps != null) {
                ps.close();
            }
        } catch (SQLException ex1) {
            ex1.printStackTrace();
        }
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException ex3) {
            ex3.printStackTrace();
        }
    }
}

From source file:io.apiman.manager.api.jdbc.JdbcMetricsAccessorTest.java

/**
 * Initialize the DB with the apiman gateway DDL.
 * @param connection/*ww  w.ja va 2s  .c om*/
 */
private static void initDB(Connection connection) throws Exception {
    ClassLoader cl = JdbcMetricsAccessorTest.class.getClassLoader();
    URL resource = cl.getResource("ddls/apiman-gateway_h2.ddl");
    try (InputStream is = resource.openStream()) {
        System.out.println("=======================================");
        System.out.println("Initializing database.");
        DdlParser ddlParser = new DdlParser();
        List<String> statements = ddlParser.parse(is);
        for (String sql : statements) {
            System.out.println(sql);
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.execute();
        }
        System.out.println("=======================================");
    }

    System.out.println("--------------------------------------");
    System.out.println("Adding test data to the database.");
    resource = cl.getResource("JdbcMetricsAccessorTest/bulk-data.ddl");
    try (InputStream is = resource.openStream()) {
        DdlParser ddlParser = new DdlParser();
        List<String> statements = ddlParser.parse(is);
        for (String sql : statements) {
            System.out.println(sql);
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.execute();
        }
    }
    System.out.println("--------------------------------------");
}

From source file:FacultyAdvisement.StudentRepository.java

public static void updateAllToNotAdvised(DataSource ds) throws SQLException {
    Connection conn = ds.getConnection();
    if (conn == null) {
        throw new SQLException("conn is null; Can't get db connection");
    }//from  ww  w . j  ava 2  s  . c o m
    try {
        PreparedStatement ps = conn.prepareStatement("update student set advised = \'false\'");

        ps.execute();

    } finally {
        conn.close();
    }
}