List of usage examples for java.sql ResultSet getLong
long getLong(String columnLabel) throws SQLException;
ResultSet
object as a long
in the Java programming language. From source file:ca.qc.adinfo.rouge.social.db.SocialDb.java
public static Collection<Long> getFriends(DBManager dbManager, long userId) { Collection<Long> friends = new ArrayList<Long>(); Connection connection = null; PreparedStatement stmt = null; ResultSet rs = null; String sql = "SELECT `friend_user_id` FROM rouge_social_friends WHERE `user_id` = ? "; try {// w ww.ja v a 2 s. c o m connection = dbManager.getConnection(); stmt = connection.prepareStatement(sql); stmt.setLong(1, userId); rs = stmt.executeQuery(); while (rs.next()) { friends.add(rs.getLong("friend_user_id")); } return friends; } catch (SQLException e) { log.error(stmt); log.error(e); return null; } finally { DbUtils.closeQuietly(rs); DbUtils.closeQuietly(stmt); DbUtils.closeQuietly(connection); } }
From source file:com.keybox.manage.db.SessionAuditDB.java
/** * returns terminal logs for user session for host system * * @param con DB connection/* ww w. j a v a2s . 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<>(); 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.tethrnet.manage.db.UserDB.java
/** * returns users based on sort order defined * @param sortedSet object that defines sort order * @return sorted user list/*from ww w . j ava 2 s . co m*/ */ public static SortedSet getUserSet(SortedSet sortedSet) { ArrayList<User> userList = new ArrayList<User>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from users where enabled=true " + orderBy; Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { User 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")); userList.add(user); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); sortedSet.setItemList(userList); return sortedSet; }
From source file:com.tethrnet.manage.db.UserDB.java
/** * returns all admin users based on sort order defined * @param sortedSet object that defines sort order * @return sorted user list// www .j a v a 2 s. co m */ public static SortedSet getAdminUserSet(SortedSet sortedSet) { ArrayList<User> userList = new ArrayList<User>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from users where enabled=true and user_type like '" + User.ADMINISTRATOR + "' " + orderBy; Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { User 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")); userList.add(user); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); sortedSet.setItemList(userList); return sortedSet; }
From source file:com.keybox.manage.db.UserDB.java
/** * returns user base on id//from ww w .jav a 2 s . c o 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.setFirstNm(rs.getString(FIRST_NM)); user.setLastNm(rs.getString(LAST_NM)); 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.keybox.manage.db.UserDB.java
/** * returns users based on sort order defined * @param sortedSet object that defines sort order * @return sorted user list/*from w w w.j a v a 2 s. c om*/ */ public static SortedSet getUserSet(SortedSet sortedSet) { ArrayList<User> userList = new ArrayList<>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from users where enabled=true " + orderBy; Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { User 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.setPassword(rs.getString(PASSWORD)); user.setAuthType(rs.getString(AUTH_TYPE)); user.setUserType(rs.getString(USER_TYPE)); userList.add(user); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); sortedSet.setItemList(userList); return sortedSet; }
From source file:com.keybox.manage.db.UserDB.java
/** * returns all admin users based on sort order defined * @param sortedSet object that defines sort order * @return sorted user list/*from w w w .j a v a 2 s.co m*/ */ public static SortedSet getAdminUserSet(SortedSet sortedSet) { ArrayList<User> userList = new ArrayList<>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = "order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from users where enabled=true and user_type like '" + User.ADMINISTRATOR + "' " + orderBy; Connection con = null; try { con = DBUtils.getConn(); PreparedStatement stmt = con.prepareStatement(sql); ResultSet rs = stmt.executeQuery(); while (rs.next()) { User 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.setPassword(rs.getString(PASSWORD)); user.setAuthType(rs.getString(AUTH_TYPE)); user.setUserType(rs.getString(USER_TYPE)); userList.add(user); } DBUtils.closeRs(rs); DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } DBUtils.closeConn(con); sortedSet.setItemList(userList); return sortedSet; }
From source file:com.cloudera.sqoop.manager.PostgresqlExportTest.java
public static void assertRowCount(long expected, String tableName, Connection connection) { Statement stmt = null;//ww w . ja va 2 s . co m ResultSet rs = null; try { stmt = connection.createStatement(); rs = stmt.executeQuery("SELECT count(*) FROM " + tableName); rs.next(); assertEquals(expected, rs.getLong(1)); } catch (SQLException e) { LOG.error("Can't verify number of rows", e); fail(); } finally { try { connection.commit(); if (stmt != null) { stmt.close(); } if (rs != null) { rs.close(); } } catch (SQLException ex) { LOG.info("Ignored exception in finally block."); } } }
From source file:dsd.dao.CalculatedDataDAO.java
public static ArrayList<CalculatedData> GetAllForPeriod(Calendar startDate, Calendar endDate, eCalculatedDataType dataType) {//from w w w . j a v a 2 s. com try { Connection con = DAOProvider.getDataSource().getConnection(); ArrayList<CalculatedData> calculatedDataList = new ArrayList<CalculatedData>(); try { String tableName = GetTableNameForDataType(dataType); Object[] parameters = new Object[2]; parameters[0] = new Timestamp(startDate.getTimeInMillis()); parameters[1] = new Timestamp(endDate.getTimeInMillis()); ResultSet results = DAOProvider.SelectTableSecure(tableName, "*", " timestamp >= ? and timestamp <= ? ", "", con, parameters); while (results.next()) { CalculatedData dataTuple = new CalculatedData(); dataTuple.setCalulcatedDataID(results.getLong("ID")); dataTuple.setWindSpeed(results.getFloat(fields[0])); dataTuple.setWindDirection(results.getFloat(fields[1])); dataTuple.setWindSpeedMax(results.getFloat(fields[2])); dataTuple.setWindDirectionMax(results.getFloat(fields[3])); dataTuple.setHydrometer(results.getFloat(fields[4])); dataTuple.setHydrometerVariance(results.getFloat(fields[5])); dataTuple.setSonar(results.getFloat(fields[6])); dataTuple.setSonarVariance(results.getFloat(fields[7])); dataTuple.setSonarPercCorrect(results.getFloat(fields[8])); dataTuple.setSonarPercWrong(results.getFloat(fields[9])); dataTuple.setSonarPercOutOfWater(results.getFloat(fields[10])); dataTuple.setSonarPercError(results.getFloat(fields[11])); dataTuple.setSonarPercUncertain(results.getFloat(fields[12])); dataTuple.setSafetyFactor00(results.getFloat(fields[13])); dataTuple.setSafetyFactor01(results.getFloat(fields[14])); dataTuple.setSafetyFactor10(results.getFloat(fields[15])); dataTuple.setSafetyFactor11(results.getFloat(fields[16])); dataTuple.setWaterSpeed(results.getFloat(fields[17])); dataTuple.setWaterFlowRate(results.getFloat(fields[18])); dataTuple.setTimestamp(results.getTimestamp(fields[19]).getTime()); calculatedDataList.add(dataTuple); } } catch (Exception exc) { exc.printStackTrace(); } con.close(); return calculatedDataList; } catch (Exception exc) { exc.printStackTrace(); } return null; }
From source file:com.tethrnet.manage.db.SessionAuditDB.java
/** * returns sessions based on sort order defined * * @param sortedSet object that defines sort order * @return session list//from ww w. j av a 2 s.c o m */ public static SortedSet getSessions(SortedSet sortedSet) { //get db connection Connection con = null; List<SessionAudit> outputList = new LinkedList<SessionAudit>(); String orderBy = ""; if (sortedSet.getOrderByField() != null && !sortedSet.getOrderByField().trim().equals("")) { orderBy = " order by " + sortedSet.getOrderByField() + " " + sortedSet.getOrderByDirection(); } String sql = "select * from session_log, users where users.id= session_log.user_id "; sql += StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER_ID)) ? " and session_log.user_id=? " : ""; sql += StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SYSTEM_ID)) ? " and session_log.id in ( select session_id from terminal_log where terminal_log.system_id=? ) " : ""; sql += orderBy; try { con = DBUtils.getConn(); deleteAuditHistory(con); PreparedStatement stmt = con.prepareStatement(sql); int i = 1; //set filters in prepared statement if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_USER_ID))) { stmt.setLong(i++, Long.valueOf(sortedSet.getFilterMap().get(FILTER_BY_USER_ID))); } if (StringUtils.isNotEmpty(sortedSet.getFilterMap().get(FILTER_BY_SYSTEM_ID))) { stmt.setLong(i++, Long.valueOf(sortedSet.getFilterMap().get(FILTER_BY_SYSTEM_ID))); } ResultSet rs = stmt.executeQuery(); while (rs.next()) { SessionAudit sessionAudit = new SessionAudit(); sessionAudit.setId(rs.getLong("session_log.id")); sessionAudit.setSessionTm(rs.getTimestamp("session_tm")); sessionAudit.setUser(UserDB.getUser(con, rs.getLong("user_id"))); outputList.add(sessionAudit); } DBUtils.closeStmt(stmt); } catch (Exception e) { log.error(e.toString(), e); } //close db connection DBUtils.closeConn(con); sortedSet.setItemList(outputList); return sortedSet; }