Example usage for java.sql ResultSet getLong

List of usage examples for java.sql ResultSet getLong

Introduction

In this page you can find the example usage for java.sql ResultSet getLong.

Prototype

long getLong(String columnLabel) throws SQLException;

Source Link

Document

Retrieves the value of the designated column in the current row of this ResultSet object as a long in the Java programming language.

Usage

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

/**
 * returns a list of terminal sessions for session id
 *
 * @param sessionId session id/*from   w w w  . ja  va2s.  c  om*/
 * @return terminal sessions with host information
 */
public static SessionAudit getSessionsTerminals(Long sessionId) {
    //get db connection
    Connection con = null;
    SessionAudit sessionAudit = new SessionAudit();

    String sql = "select * from session_log, users where users.id= session_log.user_id and session_log.id = ? ";
    try {

        con = DBUtils.getConn();
        PreparedStatement stmt = con.prepareStatement(sql);
        stmt.setLong(1, sessionId);

        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            sessionAudit.setId(rs.getLong("session_log.id"));
            sessionAudit.setSessionTm(rs.getTimestamp(SESSION_TM));
            sessionAudit.setUser(UserDB.getUser(con, rs.getLong(USER_ID)));
            sessionAudit.setHostSystemList(getHostSystemsForSession(con, sessionId));

        }

        DBUtils.closeStmt(stmt);

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

    return sessionAudit;

}

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

/**
 * auth user and return auth token if valid auth
 *
 * @param auth username and password object
 * @return auth token if success//from  w  w  w. java  2 s .c o m
 */
public static String login(Auth auth) {
    //check ldap first
    String authToken = ExternalAuthUtil.login(auth);

    if (StringUtils.isEmpty(authToken)) {

        Connection con = null;

        try {
            con = DBUtils.getConn();

            //get salt for user
            String salt = getSaltByUsername(con, auth.getUsername());
            //login
            PreparedStatement stmt = con
                    .prepareStatement("select * from users where enabled=true and username=? and password=?");
            stmt.setString(1, auth.getUsername());
            stmt.setString(2, EncryptionUtil.hash(auth.getPassword() + salt));
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {

                auth.setId(rs.getLong("id"));
                authToken = UUID.randomUUID().toString();
                auth.setAuthToken(authToken);
                auth.setAuthType(Auth.AUTH_BASIC);
                updateLogin(con, auth);

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

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

        DBUtils.closeConn(con);
    }

    return authToken;

}

From source file:io.mycat.server.packet.util.CharsetUtil.java

private static boolean getCharsetCollationFromMysql(DBHostConfig config) {
    String sql = "SELECT ID,CHARACTER_SET_NAME,COLLATION_NAME,IS_DEFAULT FROM INFORMATION_SCHEMA.COLLATIONS";
    try (Connection conn = getConnection(config)) {
        if (conn == null)
            return false;

        try (Statement statement = conn.createStatement()) {
            ResultSet rs = statement.executeQuery(sql);
            while (rs != null && rs.next()) {
                int collationIndex = new Long(rs.getLong(1)).intValue();
                String charsetName = rs.getString(2);
                String collationName = rs.getString(3);
                boolean isDefaultCollation = (rs.getString(4) != null
                        && "Yes".equalsIgnoreCase(rs.getString(4))) ? true : false;

                INDEX_TO_CHARSET.put(collationIndex, charsetName);
                if (isDefaultCollation) { // ? charsetName collationIndexcollationIndex
                    CHARSET_TO_INDEX.put(charsetName, collationIndex);
                }/*from  ww  w. j  ava2  s  . c om*/

                CharsetCollation cc = new CharsetCollation(charsetName, collationIndex, collationName,
                        isDefaultCollation);
                COLLATION_TO_CHARSETCOLLATION.put(collationName, cc);
            }
            if (COLLATION_TO_CHARSETCOLLATION.size() > 0)
                return true;
            return false;
        } catch (SQLException e) {
            logger.warn(e.getMessage());
        }

    } catch (SQLException e) {
        logger.warn(e.getMessage());
    }
    return false;
}

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

/**
 * auth user and return auth token if valid auth
 *
 * @param auth username and password object
 * @return auth token if success/* w w w . j  a  v  a2s.co  m*/
 */
public static String login(Auth auth) {
    String authToken = null;
    //admin just for locally
    if (!auth.getUsername().equals("admin")) {
        //check ldap first
        authToken = ExternalAuthUtil.login(auth);
    }

    if (StringUtils.isEmpty(authToken)) {

        Connection con = null;

        try {
            con = DBUtils.getConn();

            //get salt for user
            String salt = getSaltByUsername(con, auth.getUsername());
            //login
            PreparedStatement stmt = con
                    .prepareStatement("select * from users where enabled=true and username=? and password=?");
            stmt.setString(1, auth.getUsername());
            stmt.setString(2, EncryptionUtil.hash(auth.getPassword() + salt));
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {

                auth.setId(rs.getLong("id"));
                authToken = UUID.randomUUID().toString();
                auth.setAuthToken(authToken);
                auth.setAuthType(Auth.AUTH_BASIC);
                updateLogin(con, auth);

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

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

        DBUtils.closeConn(con);
    }

    return authToken;

}

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

/**
 * inserts new user//  w w  w. j  av a 2s .  co  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 (first_nm, last_nm, email, username, auth_type, user_type, password, salt) values (?,?,?,?,?,?,?,?)",
                Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, user.getFirstNm());
        stmt.setString(2, user.getLastNm());
        stmt.setString(3, user.getEmail());
        stmt.setString(4, user.getUsername());
        stmt.setString(5, user.getAuthType());
        stmt.setString(6, user.getUserType());
        if (StringUtils.isNotEmpty(user.getPassword())) {
            String salt = EncryptionUtil.generateSalt();
            stmt.setString(7, EncryptionUtil.hash(user.getPassword() + salt));
            stmt.setString(8, salt);
        } else {
            stmt.setString(7, null);
            stmt.setString(8, 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.tethrnet.manage.db.UserDB.java

/**
 * returns user base on id/*www .  j  a  v  a2  s.  co m*/
 * @param con DB connection
 * @param userId user id
 * @return user object
 */
public static User getUser(Connection con, Long userId) {

    User user = null;
    try {
        PreparedStatement stmt = con.prepareStatement("select * from  users where id=?");
        stmt.setLong(1, userId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            user = new User();
            user.setId(rs.getLong("id"));
            user.setEmail(rs.getString("email"));
            user.setUsername(rs.getString("username"));
            user.setPassword(rs.getString("password"));
            user.setAuthType(rs.getString("auth_type"));
            user.setUserType(rs.getString("user_type"));
            user.setSalt(rs.getString("salt"));
            user.setProfileList(UserProfileDB.getProfilesByUser(con, userId));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return user;
}

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

/**
 * returns terminal logs for user session for host system
 *
 * @param con       DB connection/*from ww  w .  jav a  2  s  .c  om*/
 * @param sessionId session id
 * @return session output for session
 */
public static List<HostSystem> getHostSystemsForSession(Connection con, Long sessionId) {

    List<HostSystem> hostSystemList = new ArrayList<HostSystem>();
    try {
        PreparedStatement stmt = con.prepareStatement(
                "select distinct instance_id, system_id from terminal_log where session_id=?");
        stmt.setLong(1, sessionId);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            HostSystem hostSystem = SystemDB.getSystem(con, rs.getLong("system_id"));
            hostSystem.setInstanceId(rs.getInt("instance_id"));
            hostSystemList.add(hostSystem);
        }

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

    } catch (Exception ex) {
        log.error(ex.toString(), ex);
    }
    return hostSystemList;

}

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

/**
 * returns a list of systems for a given profile
 *
 * @param con       DB connection/*from  ww w  . j a  v  a 2  s.c  o  m*/
 * @param profileId profile id
 * @return list of host systems
 */
public static List<HostSystem> getSystemsByProfile(Connection con, Long profileId) {

    List<HostSystem> hostSystemList = new ArrayList<HostSystem>();
    try {
        PreparedStatement stmt = con.prepareStatement(
                "select * from  system s, system_map m where s.id=m.system_id and m.profile_id=? order by display_nm asc");
        stmt.setLong(1, profileId);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            HostSystem hostSystem = new HostSystem();
            hostSystem.setId(rs.getLong("id"));
            hostSystem.setDisplayNm(rs.getString("display_nm"));
            hostSystem.setUser(rs.getString("user"));
            hostSystem.setHost(rs.getString("host"));
            hostSystem.setPort(rs.getInt("port"));
            hostSystem.setAuthorizedKeys(rs.getString("authorized_keys"));
            hostSystem.setEnabled(rs.getBoolean("enabled"));
            hostSystemList.add(hostSystem);
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return hostSystemList;
}

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

/**
 * returns user base on username// w w  w. j ava2  s.  c  om
 *
 * @param con DB connection
 * @param uid username id
 * @return user object
 */
public static User getUserByUID(Connection con, String uid) {

    User user = null;
    try {
        PreparedStatement stmt = con
                .prepareStatement("select * from  users where lower(username) like lower(?) and enabled=true");
        stmt.setString(1, uid);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            user = new User();
            user.setId(rs.getLong("id"));
            user.setEmail(rs.getString("email"));
            user.setUsername(rs.getString("username"));
            user.setUserType(rs.getString("user_type"));
            user.setProfileList(UserProfileDB.getProfilesByUser(con, user.getId()));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return user;
}

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

/**
 * returns user base on username/*from w w w.j a  v  a2  s  .c  o m*/
 *
 * @param con DB connection
 * @param uid username id
 * @return user object
 */
public static User getUserByUID(Connection con, String uid) {

    User user = null;
    try {
        PreparedStatement stmt = con
                .prepareStatement("select * from  users where lower(username) like lower(?) and enabled=true");
        stmt.setString(1, uid);
        ResultSet rs = stmt.executeQuery();

        while (rs.next()) {
            user = new User();
            user.setId(rs.getLong("id"));
            user.setFirstNm(rs.getString("first_nm"));
            user.setLastNm(rs.getString("last_nm"));
            user.setEmail(rs.getString("email"));
            user.setUsername(rs.getString("username"));
            user.setUserType(rs.getString("user_type"));
            user.setProfileList(UserProfileDB.getProfilesByUser(con, user.getId()));
        }
        DBUtils.closeRs(rs);
        DBUtils.closeStmt(stmt);

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

    return user;
}