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.keybox.manage.db.SessionAuditDB.java

/**
 * deletes audit history for users if after time set in properties file
 *
 * @param con DB connection/*from   w  w  w .ja va  2 s  .c  o m*/
 */
public static void deleteAuditHistory(Connection con) {

    try {

        //delete logs with no terminal entries
        PreparedStatement stmt = con.prepareStatement(
                "delete from session_log where id not in (select session_id from terminal_log)");
        stmt.execute();

        //take today's date and subtract how many days to keep history
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, (-1 * Integer.parseInt(AppConfig.getProperty("deleteAuditLogAfter")))); //subtract
        java.sql.Date date = new java.sql.Date(cal.getTimeInMillis());

        stmt = con.prepareStatement("delete from session_log where session_tm < ?");
        stmt.setDate(1, date);
        stmt.execute();

        DBUtils.closeStmt(stmt);

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

}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Just installs the jar by file name, default in sys_network schema.
 *//*from w  w  w  .ja  v  a  2 s .com*/
public static void installJar(String jarFile) throws SQLException {
    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String name = jarFile.replaceAll("\\.jar", "");
    String query = "CREATE or REPLACE JAR localdb.sys_network.\"" + name + "\"\n"
            + "LIBRARY 'file:${FARRAGO_HOME}/plugin/" + jarFile + "'\n" + "OPTIONS(1)";
    PreparedStatement ps = conn.prepareStatement("set schema 'localdb.sys_network'");
    ps.execute();
    ps = conn.prepareStatement(query);
    ps.execute();
    ps.close();
}

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

/**
 * adds a host system to profile//  w w w  .  java  2  s .  co m
 *
 * @param profileId profile id
 * @param systemId  host system id
 */
public static void addSystemToProfile(Long profileId, Long systemId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con
                .prepareStatement("insert into system_map (profile_id, system_id) values (?,?)");
        stmt.setLong(1, profileId);
        stmt.setLong(2, systemId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

    } catch (Exception e) {
        e.printStackTrace();
    }
    DBUtils.closeConn(con);
}

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

/**
 * inserts into the status table to keep track of key placement status
 *
 * @param con                DB connection object
 * @param hostSystem systems for authorized_keys replacement
 * @param userId user id//w  w w  .ja  v  a 2 s.com
 */
private static void insertSystemStatus(Connection con, HostSystem hostSystem, Long userId) {

    try {

        PreparedStatement stmt = con
                .prepareStatement("insert into status (id, status_cd, user_id) values (?,?,?)");
        stmt.setLong(1, hostSystem.getId());
        stmt.setString(2, hostSystem.getStatusCd());
        stmt.setLong(3, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Procedure to avoid an explicit insert yourself.
 *//*from   w ww.ja va2 s  .  c  o  m*/
public static void addRepo(String repoUrl) throws SQLException {
    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String query = "INSERT INTO localdb.sys_network.repositories (repo_url) " + "VALUES (?)";
    PreparedStatement ps = conn.prepareStatement(query);
    ps.setString(1, repoUrl);
    ps.execute();
    ps.close();
}

From source file:com.dynamobi.network.DynamoNetworkUdr.java

/**
 * Does the delete for you.// w ww.  j a  v  a  2 s.c o m
 */
public static void removeRepo(String repoUrl) throws SQLException {
    Connection conn = DriverManager.getConnection("jdbc:default:connection");
    String query = "DELETE FROM localdb.sys_network.repositories WHERE " + "repo_url = ?";
    PreparedStatement ps = conn.prepareStatement(query);
    ps.setString(1, repoUrl);
    ps.execute();
    ps.close();
}

From source file:com.wso2telco.util.DbUtil.java

public static void insertPinAttempt(String msisdn, int attempts, String sessionId)
        throws SQLException, AuthenticatorException {

    Connection connection = null;
    PreparedStatement ps = null;

    String sql = "insert into multiplepasswords(username, attempts, ussdsessionid) values  (?,?,?);";

    connection = getConnectDBConnection();

    ps = connection.prepareStatement(sql);

    ps.setString(1, msisdn);/*from   w  w w  .ja v  a  2s  . c  om*/
    ps.setInt(2, attempts);
    ps.setString(3, sessionId);
    ps.execute();

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

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

/**
 * insert new session record for user//ww w  . j  av a  2 s. c  om
 *
 * @param con    DB connection
 * @param userId user id
 * @return session id
 */
public static Long createSessionLog(Connection con, Long userId) {
    Long sessionId = null;
    try {

        //insert
        PreparedStatement stmt = con.prepareStatement("insert into session_log (user_id) values(?)",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setLong(1, userId);
        stmt.execute();
        ResultSet rs = stmt.getGeneratedKeys();
        if (rs != null && rs.next()) {
            sessionId = rs.getLong(1);
        }

        DBUtils.closeStmt(stmt);

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

}

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

/**
 * updates shared secret based on auth token
 *
 * @param secret    OTP shared secret//from  ww  w.  j  a  va  2s  .c o m
 * @param authToken auth token
 */
public static void updateSharedSecret(String secret, String authToken) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("update users set otp_secret=? where auth_token=?");
        stmt.setString(1, EncryptionUtil.encrypt(secret));
        stmt.setString(2, authToken);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * Process the create account request. This should receive the following
 * objects: the username, the password, the device ID and the public key
 * for the device. This adds a new entry in the account database, and
 * creates a new device./* w  w  w  .ja v a  2  s.c o m*/
 * 
 * If the user account cannot be created, this returns nil.
 * @param requestParams
 * @return
 */
public static UserInfo processRequest(JSONObject requestParams)
        throws ClassNotFoundException, SQLException, IOException {
    String username = requestParams.optString("username");
    String password = requestParams.optString("password");
    String deviceid = requestParams.optString("deviceid");
    String pubkey = requestParams.optString("pubkey");

    /*
     * Attempt to insert a new user into the database
     */

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

    try {
        c = Database.get();
        ps = c.prepareStatement("INSERT INTO Users " + "    ( username, password ) " + "VALUES "
                + "    ( ?, ? ); SELECT currval('Users_userid_seq')");
        ps.setString(1, username);
        ps.setString(2, password);

        try {
            ps.execute();
        } catch (SQLException ex) {
            return null; // Can't insert; duplicate username?
        }
        int utc = ps.getUpdateCount();
        int userid = 0;
        if ((utc == 1) && ps.getMoreResults()) {
            rs = ps.getResultSet();
            if (rs.next()) {
                userid = rs.getInt(1);
            }
            rs.close();
            rs = null;
        }

        ps.close();
        ps = null;

        /*
         * We now have the user index. Insert the device. Note that it is
         * highly unlikely we will have a UUID collision, but we verify
         * we don't by deleting any rows in the device table with the
         * specified UUID. The worse case scenario is a collision which
         * knocks someone else off the air. (The alternative would be
         * to accidentally send the wrong person duplicate messages.)
         * 
         * Note that we don't actually use a device-identifying identifer,
         * choosing instead to pick a UUID, so we need to deal with
         * the possibility (however remote) of duplicate UUIDs.
         * 
         * In the off chance we did have a collision, we also delete all
         * old messages to the device; that prevents messages from being
         * accidentally delivered.
         */

        ps = c.prepareStatement("DELETE FROM Messages " + "WHERE messageid IN "
                + "    (SELECT Messages.messageid " + "     FROM Messages, Devices "
                + "     WHERE Messages.deviceid = Devices.deviceid " + "     AND Devices.deviceuuid = ?)");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("DELETE FROM Devices WHERE deviceuuid = ?");
        ps.setString(1, deviceid);
        ps.execute();
        ps.close();
        ps = null;

        ps = c.prepareStatement("INSERT INTO Devices " + "    ( userid, deviceuuid, publickey ) " + "VALUES "
                + "    ( ?, ?, ?)");
        ps.setInt(1, userid);
        ps.setString(2, deviceid);
        ps.setString(3, pubkey);
        ps.execute();

        /*
         * Complete; return the user info record
         */

        return new Login.UserInfo(userid);
    } finally {
        if (rs != null)
            rs.close();
        if (ps != null)
            ps.close();
        if (c != null)
            c.close();
    }
}