Example usage for java.sql PreparedStatement setLong

List of usage examples for java.sql PreparedStatement setLong

Introduction

In this page you can find the example usage for java.sql PreparedStatement setLong.

Prototype

void setLong(int parameterIndex, long x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given Java long value.

Usage

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

/**
 * returns all system ids for user//  w  w w .ja va  2s  .  c om
 *
 * @param con    DB connection
 * @param userId user id
 * @return system
 */
public static List<Long> getAllSystemIdsForUser(Connection con, Long userId) {

    List<Long> systemIdList = new ArrayList<Long>();

    try {
        PreparedStatement stmt = con.prepareStatement(
                "select distinct system_id from  system_map m, user_map um where m.profile_id=um.profile_id and um.user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            systemIdList.add(rs.getLong("system_id"));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return systemIdList;

}

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

/**
 * returns public keys based on sort order defined
 *
 * @param sortedSet object that defines sort order
 * @param userId user id//  w w w  .  java  2  s  .co  m
 * @return sorted script list
 */
public static SortedSet getPublicKeySet(SortedSet sortedSet, Long userId) {

    ArrayList<PublicKey> publicKeysList = new ArrayList<>();

    String orderBy = "";
    if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) {
        orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection();
    }
    String sql = "select * from public_keys where user_id = ? and enabled=true " + orderBy;

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            PublicKey publicKey = new PublicKey();
            publicKey.setId(rs.getLong("id"));
            publicKey.setKeyNm(rs.getString(KEY_NM));
            publicKey.setPublicKey(rs.getString(PUBLIC_KEY));
            publicKey.setProfile(ProfileDB.getProfile(con, rs.getLong(PROFILE_ID)));
            publicKey.setType(SSHUtil.getKeyType(publicKey.getPublicKey()));
            publicKey.setFingerprint(SSHUtil.getFingerprint(publicKey.getPublicKey()));
            publicKey.setCreateDt(rs.getTimestamp(CREATE_DT));
            publicKeysList.add(publicKey);

        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    sortedSet.setItemList(publicKeysList);
    return sortedSet;
}

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

/**
 * returns all system ids for user/*w w w  .  j  a  v a  2s  .co m*/
 *
 * @param con    DB connection
 * @param userId user id
 * @return system
 */
public static List<Long> getAllSystemIdsForUser(Connection con, Long userId) {

    List<Long> systemIdList = new ArrayList<>();

    try {
        PreparedStatement stmt = con.prepareStatement(
                "select distinct system_id from  system_map m, user_map um where m.profile_id=um.profile_id and um.user_id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            systemIdList.add(rs.getLong("system_id"));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return systemIdList;

}

From source file:edu.lafayette.metadb.model.userman.UserManDAO.java

/**
 * Update the login time of a user./*  w ww  .  j  a v  a 2s. c  o m*/
 * @param userName the user name to update.
 * @param value the value of the login time, as a long integer.
 * @return true if the user's login time was updated successfully, false otherwise.
 */
public static boolean updateLoginTime(String userName, long value) {
    if (!MetaDbHelper.userExists(userName))
        return false;

    Connection conn = Conn.initialize(); // Establish connection
    if (conn != null) {
        try {
            PreparedStatement updateUser = conn.prepareStatement(UPDATE_LAST_LOGIN);

            updateUser.setLong(1, value);
            updateUser.setString(2, userName);
            updateUser.executeUpdate();

            updateUser.close();
            conn.close(); // Close statement and connection

            return true;

        } catch (Exception e) {
            MetaDbHelper.logEvent(e);

        }
    }
    return false;

}

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

/**
 * deletes all public keys for a profile
 *
 * @param profileId profile id/*from  ww  w .  j a v  a  2s . c o m*/
 */
public static void deleteProfilePublicKeys(Long profileId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("delete from public_keys where profile_id=?");
        stmt.setLong(1, profileId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * returns public key base on id/*from   w w  w .jav  a  2  s.c  o  m*/
 *
 * @param con         DB connection
 * @param publicKeyId key id
 * @return script object
 */
public static PublicKey getPublicKey(Connection con, Long publicKeyId) {

    PublicKey publicKey = null;
    try {
        PreparedStatement stmt = con.prepareStatement("select * from  public_keys where id=?");
        stmt.setLong(1, publicKeyId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            publicKey = new PublicKey();
            publicKey.setId(rs.getLong("id"));
            publicKey.setKeyNm(rs.getString("key_nm"));
            publicKey.setPublicKey(rs.getString("public_key"));
            publicKey.setProfile(ProfileDB.getProfile(con, rs.getLong("profile_id")));
            publicKey.setType(rs.getString("type"));
            publicKey.setFingerprint(rs.getString("fingerprint"));
            publicKey.setCreateDt(rs.getTimestamp("create_dt"));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return publicKey;
}

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

/**
  * re-enables SSH key/*  w w  w.jav a2 s  .co m*/
  * 
  * @param id key id
 */
public static void enableKey(Long id) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("update public_keys set enabled=true where id=?");
        stmt.setLong(1, id);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * disables SSH key//w ww  . j av  a 2 s .  c  o  m
 *
 * @param id key id
 */
public static void disableKey(Long id) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement("update public_keys set enabled=false where id=?");
        stmt.setLong(1, id);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}

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

/**
 * returns public key base on id//from   ww  w.  j ava  2 s . c o m
 *
 * @param con         DB connection
 * @param publicKeyId key id
 * @return script object
 */
public static PublicKey getPublicKey(Connection con, Long publicKeyId) {

    PublicKey publicKey = null;
    try {
        PreparedStatement stmt = con.prepareStatement("select * from  public_keys where id=?");
        stmt.setLong(1, publicKeyId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            publicKey = new PublicKey();
            publicKey.setId(rs.getLong("id"));
            publicKey.setKeyNm(rs.getString(KEY_NM));
            publicKey.setPublicKey(rs.getString(PUBLIC_KEY));
            publicKey.setProfile(ProfileDB.getProfile(con, rs.getLong(PROFILE_ID)));
            publicKey.setType(rs.getString("type"));
            publicKey.setFingerprint(rs.getString("fingerprint"));
            publicKey.setCreateDt(rs.getTimestamp(CREATE_DT));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return publicKey;
}

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

/**
 * deletes all public keys for user//from   www . j  a v a  2 s .  c  om
 *
 * @param userId  user id
 */
public static void deleteUserPublicKeys(Long userId) {

    Connection con = null;
    try {
        con = DBUtils.getConn();
        PreparedStatement stmt = con
                .prepareStatement("delete from public_keys where user_id=? and enabled=true");
        stmt.setLong(1, userId);
        stmt.execute();
        DBUtils.closeStmt(stmt);

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

}