// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package testprep;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Calendar;

public class TestPrep3 {
    public static void main(String[] args) throws Exception {
        Class.forName("oracle.jdbc.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:arnepc3", "arne", "xxxx");
        PreparedStatement ins = con.prepareStatement("INSERT INTO dtest VALUES (?, ?)");
        for(int i = 0; i < 10; i++) {
            ins.setInt(1, i);
            Timestamp ts = new Timestamp(Calendar.getInstance().getTimeInMillis());
            ins.setTimestamp(2, ts);
            ins.executeUpdate();
            Thread.sleep(1000);
        }
        ins.close();
        PreparedStatement sel = con.prepareStatement("SELECT * FROM dtest WHERE d > ?");
        Timestamp cut = new Timestamp(Calendar.getInstance().getTimeInMillis() - 5000);
        sel.setTimestamp(1, cut);
        ResultSet rs = sel.executeQuery();
        while(rs.next()) {
            int i = rs.getInt(1);
            Timestamp ts = rs.getTimestamp(2);
            System.out.println(i + " " + ts);
        }
        rs.close();
        sel.close();
        con.close();
    }
}
