Example usage for java.sql ResultSet getTimestamp

List of usage examples for java.sql ResultSet getTimestamp

Introduction

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

Prototype

java.sql.Timestamp getTimestamp(String columnLabel) throws SQLException;

Source Link

Document

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

Usage

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

/**
 * returns a list of terminal sessions for session id
 *
 * @param sessionId session id//from www  .j  ava 2s .  co m
 * @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.SessionAuditDB.java

/**
 * returns a list of terminal sessions for session id
 *
 * @param sessionId session id//from w w  w.  j  a v a 2  s  .co  m
 * @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.tethrnet.manage.db.SessionAuditDB.java

/**
 * returns sessions based on sort order defined
 *
 * @param sortedSet object that defines sort order
 * @return session list/*from   w w w  .  java2s.  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;

}

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

/**
 * returns sessions based on sort order defined
 *
 * @param sortedSet object that defines sort order
 * @return session list//w w  w . j  av  a2  s  .  co m
 */
public static SortedSet getSessions(SortedSet sortedSet) {
    //get db connection
    Connection con = null;
    List<SessionAudit> outputList = new LinkedList<>();

    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;

}

From source file:cn.clickvalue.cv2.model.rowmapper.BeanPropertyRowMapper.java

/**
 * Retrieve a JDBC column value from a ResultSet, using the most appropriate
 * value type. The returned value should be a detached value object, not having
 * any ties to the active ResultSet: in particular, it should not be a Blob or
 * Clob object but rather a byte array respectively String representation.
 * <p>Uses the <code>getObject(index)</code> method, but includes additional "hacks"
 * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
 * datatype and a <code>java.sql.Date</code> for DATE columns leaving out the
 * time portion: These columns will explicitly be extracted as standard
 * <code>java.sql.Timestamp</code> object.
 * @param rs is the ResultSet holding the data
 * @param index is the column index//from  ww w.j  a  v a 2s  .  c  om
 * @return the value object
 * @throws SQLException if thrown by the JDBC API
 * @see java.sql.Blob
 * @see java.sql.Clob
 * @see java.sql.Timestamp
 */
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
    Object obj = rs.getObject(index);
    if (obj instanceof Blob) {
        obj = rs.getBytes(index);
    } else if (obj instanceof Clob) {
        obj = rs.getString(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) {
        obj = rs.getTimestamp(index);
    } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) {
        String metaDataClassName = rs.getMetaData().getColumnClassName(index);
        if ("java.sql.Timestamp".equals(metaDataClassName)
                || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
            obj = rs.getTimestamp(index);
        } else {
            obj = rs.getDate(index);
        }
    } else if (obj != null && obj instanceof java.sql.Date) {
        if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
            obj = rs.getTimestamp(index);
        }
    }
    return obj;
}

From source file:com.sap.dirigible.repository.db.dao.DBMapper.java

public static DBFileVersion dbToFileVersion(Connection connection, DBRepository repository, ResultSet resultSet)
        throws SQLException, DBBaseException, IOException {

    String path = resultSet.getString("FV_FILE_PATH"); //$NON-NLS-1$
    int version = resultSet.getInt("FV_VERSION"); //$NON-NLS-1$
    byte[] bytes = dbToDataBinary(connection, resultSet, "FV_CONTENT"); //$NON-NLS-1$
    int type = resultSet.getInt("FV_TYPE"); //$NON-NLS-1$
    String content = resultSet.getString("FV_CONTENT_TYPE"); //$NON-NLS-1$
    String createdBy = resultSet.getString("FV_CREATED_BY"); //$NON-NLS-1$
    Date createdAt = new Date(resultSet.getTimestamp("FV_CREATED_AT") //$NON-NLS-1$
            .getTime());//w w w.  ja  va  2 s . c  o  m

    DBFileVersion dbFileVersion = new DBFileVersion(repository, (type == OBJECT_TYPE_BINARY), content, version,
            bytes);

    dbFileVersion.setPath(path);
    dbFileVersion.setCreatedBy(createdBy);
    dbFileVersion.setCreatedAt(createdAt);

    return dbFileVersion;
}

From source file:com.fpuna.preproceso.PreprocesoTS.java

public static HashMap<String, SessionTS> leerBDtrainingSet(String BD, String sensor) {
    HashMap<String, SessionTS> SessionsTotal = new HashMap<String, SessionTS>();
    String Consulta;//w w w.ja v a2 s  .c o  m
    Connection c = null;
    Statement stmt = null;
    Registro reg = new Registro();

    Consulta = "SELECT dat.statusId, dat.sensorName, dat.\"value\", dat.\"timestamp\", lb.\"name\"\n"
            + "FROM sensor_data AS dat, status AS st, label AS lb\n"
            + "WHERE dat.statusId = st.\"_id\" AND st.labelId = lb.\"_id\" ORDER BY dat.\"timestamp\"";
    try {
        Class.forName("org.sqlite.JDBC");
        c = DriverManager.getConnection("jdbc:sqlite:" + path + "sensor.db");
        c.setAutoCommit(false);
        System.out.println("Opened database successfully");
        stmt = c.createStatement();
        ResultSet rs = stmt.executeQuery(Consulta);

        while (rs.next()) {
            reg = new Registro();
            reg.setSensor(rs.getString("sensorName"));
            reg.setTiempo(rs.getTimestamp("timestamp"));

            String[] values = (rs.getString("value")).split("\\,");

            if (values.length == 3) {
                reg.setValor_x(Double.parseDouble(values[0].substring(1)));
                reg.setValor_y(Double.parseDouble(values[1]));
                reg.setValor_z(Double.parseDouble(values[2].substring(0, values[2].length() - 1)));
            } else if (values.length == 5) {
                reg.setValor_x(Double.parseDouble(values[0].substring(1)));
                reg.setValor_y(Double.parseDouble(values[1]));
                reg.setValor_z(Double.parseDouble(values[2]));
                reg.setM_1(Double.parseDouble(values[3]));
                reg.setM_2(Double.parseDouble(values[4].substring(0, values[4].length() - 1)));
            }

            if (SessionsTotal.containsKey(rs.getString("statusId"))) {
                SessionTS s = SessionsTotal.get(rs.getString("statusId"));
                s.addRegistro(reg);
                SessionsTotal.replace(rs.getString("statusId"), s);
            } else {
                SessionTS s = new SessionTS();
                s.setActividad(rs.getString("name"));
                s.addRegistro(reg);
                SessionsTotal.put(rs.getString("statusId"), s);
            }
        }
        rs.close();
        stmt.close();
        c.close();

    } catch (ClassNotFoundException | SQLException | NumberFormatException e) {
        System.err.println("Okapu:" + e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
    System.out.println("Operation done successfully");
    return SessionsTotal;
}

From source file:com.sql.Activity.java

/**
 * Gathers a list of tiles that are awaiting a timestamp
 * /*  w w  w .j  av  a  2 s.  c  om*/
 * @return List (ActivityModel)
 */
public static List<ActivityModel> getFilesToStamp() {
    List<ActivityModel> list = new ArrayList();
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DBConnection.connectToDB();
        String sql = "SELECT * FROM Activity WHERE awaitingTimestamp = 1";
        ps = conn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()) {
            ActivityModel type = new ActivityModel();
            type.setId(rs.getInt("id"));
            type.setCaseYear(rs.getString("caseYear"));
            type.setCaseType(rs.getString("caseType"));
            type.setCaseMonth(rs.getString("caseMonth"));
            type.setCaseNumber(rs.getString("caseNumber"));
            type.setDate(rs.getTimestamp("date"));
            type.setFileName(rs.getString("fileName"));
            list.add(type);
        }
    } catch (SQLException ex) {
        ExceptionHandler.Handle(ex);
    } finally {
        DbUtils.closeQuietly(conn);
        DbUtils.closeQuietly(ps);
        DbUtils.closeQuietly(rs);
    }
    return list;
}

From source file:de.sqlcoach.db.jdbc.DBAppUser.java

/**
 * Sets the model.// w ww  . j  a  v a2  s.co m
 * 
 * @param resultset
 *            the resultset
 * @param model
 *            the model
 * 
 * @throws SQLException
 *             the SQL exception
 */
private static void setModel(ResultSet resultset, AppUser model) throws SQLException {
    model.setId(resultset.getInt("id"));
    model.setNickname(resultset.getString("nickname"));
    model.setPassword(resultset.getString("password"));
    model.setTitle(resultset.getString("title"));
    model.setFirstname(resultset.getString("firstname"));
    model.setLastname(resultset.getString("lastname"));
    model.setEmail(resultset.getString("email"));
    model.setRole(resultset.getString("role"));
    model.setDatecreate(resultset.getTimestamp("datecreate"));
    model.setDatelastmod(resultset.getTimestamp("datelastmod"));
}

From source file:Data.java

/**
 * Creates a dataset, consisting of two series of monthly data.
 *
 * @return The dataset.// w  w  w.j  av a2  s  . c  o  m
 * @throws ClassNotFoundException 
 */
private static XYDataset createDataset(Statement stmt) throws ClassNotFoundException {

    TimeSeries s1 = new TimeSeries("Humidit");
    TimeSeries s2 = new TimeSeries("Temprature");
    ResultSet rs = null;

    try {
        String sqlRequest = "SELECT * FROM `t_temphum`";
        rs = stmt.executeQuery(sqlRequest);
        Double hum;
        Double temp;
        Timestamp date;

        while (rs.next()) {
            hum = rs.getDouble("tmp_humidity");
            temp = rs.getDouble("tmp_temperature");
            date = rs.getTimestamp("tmp_date");

            if (tempUnit == "F") {
                temp = celsiusToFahrenheit(temp.toString());
            }

            if (date != null) {
                s1.add(new Second(date), hum);
                s2.add(new Second(date), temp);
            } else {
                JOptionPane.showMessageDialog(panelPrincipal, "Il manque une date dans la dase de donne",
                        "Date null", JOptionPane.WARNING_MESSAGE);
            }
        }

        rs.close();
    } catch (SQLException e) {
        String exception = e.toString();

        if (e.getErrorCode() == 0) {
            JOptionPane.showMessageDialog(panelPrincipal,
                    "Le serveur met trop de temps  rpondre ! Veuillez rssayer plus tard ou contacter un administrateur",
                    "Connection timed out", JOptionPane.ERROR_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(panelPrincipal, "Voici l'exception : " + exception,
                    "Titre : exception", JOptionPane.ERROR_MESSAGE);
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (Exception e) {
        String exception = e.toString();
        JOptionPane.showMessageDialog(panelPrincipal, "Voici l'exception : " + exception, "Titre : exception",
                JOptionPane.ERROR_MESSAGE);
        e.printStackTrace();
    }

    // ******************************************************************
    //  More than 150 demo applications are included with the JFreeChart
    //  Developer Guide...for more information, see:
    //
    //  >   http://www.object-refinery.com/jfreechart/guide.html
    //
    // ******************************************************************

    TimeSeriesCollection dataset = new TimeSeriesCollection();
    dataset.addSeries(s1);
    dataset.addSeries(s2);

    return dataset;

}