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:Main.java

public static final void main(String[] argv) throws Exception {
    Class.forName("oracle.jdbc.OracleDriver");
    Connection conn = DriverManager.getConnection("your_connection_string", "your_user_name", "your_password");
    Date nowDate = new Date();
    Timestamp nowTimestamp = new Timestamp(nowDate.getTime());
    PreparedStatement insertStmt = conn.prepareStatement(
            "INSERT INTO MyTable" + " (os_name, ts, ts_with_tz, ts_with_local_tz)" + " VALUES (?, ?, ?, ?)");
    insertStmt.setString(1, System.getProperty("os.name"));
    insertStmt.setTimestamp(2, nowTimestamp);
    insertStmt.setTimestamp(3, nowTimestamp);
    insertStmt.setTimestamp(4, nowTimestamp);
    insertStmt.executeUpdate();/*from w ww. j ava  2  s. c  o  m*/
    insertStmt.close();
    System.out.println("os_name, ts, ts_with_tz, ts_with_local_tz");
    PreparedStatement selectStmt = conn
            .prepareStatement("SELECT os_name, ts, ts_with_tz, ts_with_local_tz" + " FROM MyTable");
    ResultSet result = null;
    result = selectStmt.executeQuery();
    while (result.next()) {
        System.out.println(String.format("%s,%s,%s,%s", result.getString(1), result.getTimestamp(2).toString(),
                result.getTimestamp(3).toString(), result.getTimestamp(4).toString()));
    }
    result.close();
    selectStmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    String sqlQuery = "SELECT uid, name, duration from EVENTS";

    ResultSet rs = stmt.executeQuery(sqlQuery);

    while (rs.next()) {
        rs.updateString("Name", "new Name");

        rs.updateRow();/*from w w w.jav a 2s .  c o m*/
    }

    rs.first();
    while (rs.next()) {
        String name = rs.getString(2);
        Timestamp hireDate = rs.getTimestamp(5);
        System.out.println("Name: " + name + " Hire Date: " + hireDate);
    }

    rs.close();

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {//from ww w.  j a  v  a 2 s  .  co m
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement stmt = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                ResultSet.CONCUR_READ_ONLY);

        String sqlQuery = "SELECT EMPNO, EName, Job, MGR, HIREDATE FROM EMP";
        ResultSet rs = stmt.executeQuery(sqlQuery);
        rs.last();

        // Move the cursor backwards through the ResultSet
        while (rs.previous()) {
            String nbr = rs.getString(1);
            String name = rs.getString(2);
            String job = rs.getString(3);
            String mgr = rs.getString(4);
            Timestamp hireDate = rs.getTimestamp(5);
            System.out.println(name);
        }

        rs.close();
        stmt.close();
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:GetDateFromOracle.java

public static void main(String args[]) {
    String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?";
    ResultSet rs = null;
    Connection conn = null;/*from w  ww .ja  v  a  2 s.  c  om*/
    PreparedStatement pstmt = null;
    try {
        conn = getConnection();
        pstmt = conn.prepareStatement(GET_RECORD);
        pstmt.setString(1, "0001");
        rs = pstmt.executeQuery();
        while (rs.next()) {
            java.sql.Date dbSqlDate = rs.getDate(1);
            java.sql.Time dbSqlTime = rs.getTime(2);
            java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
            System.out.println("dbSqlDate=" + dbSqlDate);
            System.out.println("dbSqlTime=" + dbSqlTime);
            System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            pstmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:GetDateFromMySql.java

public static void main(String args[]) {
    ResultSet rs = null;
    Connection conn = null;/*  w ww  .j  a v a  2  s.c  o m*/
    Statement stmt = null;
    try {
        conn = getMySQLConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable");
        while (rs.next()) {
            java.sql.Time dbSqlTime = rs.getTime(1);
            java.sql.Date dbSqlDate = rs.getDate(2);
            java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3);
            System.out.println("dbSqlTime=" + dbSqlTime);
            System.out.println("dbSqlDate=" + dbSqlDate);
            System.out.println("dbSqlTimestamp=" + dbSqlTimestamp);

            java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime());
            java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime());
            System.out.println("in standard date");
            System.out.println(dbSqlTimeConverted);
            System.out.println(dbSqlDateConverted);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            rs.close();
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static ZonedDateTime fromTimestamp(ResultSet rs, String column) throws SQLException {
    Timestamp timestamp = rs.getTimestamp(column);
    return getDateTime(timestamp);
}

From source file:Main.java

public static long[] getEntryExit(Double id, Calendar date, Connection con, PreparedStatement stmt)
        throws Exception {
    stmt.setDate(1, new java.sql.Date(date.getTimeInMillis()));
    // stmt.setDate(2, new java.sql.Date(date.getTimeInMillis()+1000000));
    stmt.execute();/*from w w w .  j a v  a 2 s  . c om*/
    ResultSet rs = stmt.getResultSet();
    if (rs != null) {

        if (rs.next()) {
            Timestamp d1 = rs.getTimestamp(1);
            Timestamp d2 = rs.getTimestamp(2);
            if (d1 != null && d2 != null) {
                System.out.println(id + ":" + new SimpleDateFormat("dd/MM/yyyy").format(date.getTime()) + ":"
                        + d1.toString());
                long[] res = new long[] { d1.getTime(), d2.getTime() };
                return res;
            }
        }
        rs.close();
    }
    return null;
}

From source file:cn.edu.zju.acm.onlinejudge.persistence.sql.Database.java

/**
 * Gets a date value from given ResultSet.
 * //from w  w  w  . j  a  va  2  s  .  c  om
 * @param rs
 *            the ResultSet
 * @param column
 *            the column name
 * @return a date value from given ResultSet.
 * @throws SQLException
 */
public static Date getDate(ResultSet rs, String column) throws SQLException {
    Timestamp date = rs.getTimestamp(column);
    if (date == null) {
        return null;
    }
    return new Date(date.getTime());
}

From source file:dsd.dao.RawDataDAO.java

public static long GetMaxTimestamp() {
    long timestamp = 0;
    try {//from   w  ww .ja  va  2 s . c  o m
        Connection con = DAOProvider.getDataSource().getConnection();
        try {
            ResultSet results = DAOProvider.SelectTableSecure(tableName, " max(timestamp) ", "", "", con, null);
            while (results.next()) {
                timestamp = results.getTimestamp(1).getTime();
            }
        } catch (Exception exc) {
            //            exc.printStackTrace();
            timestamp = 0;
        }
        con.close();
    } catch (Exception exc) {
        exc.printStackTrace();
    }
    return timestamp;
}

From source file:dsd.dao.RawDataDAO.java

public static long GetMinTimestamp() {
    long timestamp = 0;
    try {//  w ww .j  a  v  a  2 s  . co m
        Connection con = DAOProvider.getDataSource().getConnection();
        try {
            ResultSet results = DAOProvider.SelectTableSecure(tableName, " min(timestamp) ", "", "", con, null);
            while (results.next()) {
                timestamp = results.getTimestamp(1).getTime();
            }
        } catch (Exception exc) {
            //            exc.printStackTrace();
            timestamp = 0;
        }
        con.close();
    } catch (Exception exc) {
        exc.printStackTrace();
        timestamp = 0;
    }
    return timestamp;

}