GetDateFromOracle.java Source code

Java tutorial

Introduction

Here is the source code for GetDateFromOracle.java

Source

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();
            }
        }
    }
}