How to Get Date, Time & Timestamp Data from Oracle

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Get Date, Time & Timestamp Data from Oracle

This Java example program demonstrates how to get data from Oracle.

[edit] Java Source Code

  • Package: example.datetimetimestamp
  • File: GetDateFromOracle.java
package example.datetimetimestamp;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class GetDateFromOracle {
    public static Connection getConnection() throws Exception {
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
	Class.forName(driver);
	return DriverManager.getConnection(url, "name", "password");
    }
 
    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;
	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();
	    }
	}
    }
}

[edit] What Result You Can Get

Coming soon...

[edit] Required External Libraries and/or Files for this Java Example

In order to run this Java example, one of the following libraries may be required: In order to run this example program, one of the following libraries may be required:


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools