import java.sql.*;
import oracle.jdbc.*;
public class DateTimeSample {
public DateTimeSample( ) throws SQLException {
DriverManager.registerDriver( new OracleDriver( ) );
}
public static void main( String[] args ) throws SQLException {
DateTimeSample sample = new DateTimeSample( );
int empno = 1111;
sample.insertDateTime( empno );
sample.getDateTime( empno );
}
public void insertDateTime( int empno ) throws SQLException {
Connection conn = this.getConnection( );
Timestamp hireDate = new Timestamp( new java.util.Date( ).getTime( ) );
PreparedStatement pstmt = conn.prepareStatement( " INSERT INTO EMP(EmpNo, HireDate) VALUES(?,?) " );
pstmt.setInt( 1, empno );
pstmt.setTimestamp( 2, hireDate );
pstmt.executeUpdate( );
System.out.println(" Employee record inserted : " + empno + " " + hireDate );
pstmt.close( );
conn.close( );
}
public void getDateTime( int empno ) throws SQLException {
Connection conn = this.getConnection( );
PreparedStatement pstmt = conn.prepareStatement( "SELECT HireDate FROM Emp WHERE EmpNo = ? " );
pstmt.setInt( 1, empno );
ResultSet rset = pstmt.executeQuery( );
while ( rset.next( ) )
System.out.println( " Get Employee HireDate : " + empno + " " + rset.getTimestamp( 1 ) );
rset.close( );
pstmt.close( );
conn.close( );
}
public Connection getConnection( ) throws SQLException {
String jdbcurl = "jdbc:oracle:thin:@localhost:1521:ias904";
return DriverManager.getConnection( jdbcurl, "scott", "tiger" );
}
}