// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

public class RdbDemo implements Demo {
    public void findAbbreviation(Connection con) throws SQLException {
        String s = "IN";
        System.out.println("String (Rdb):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,term FROM abbr " +
                                                       "WHERE CONCAT(SUBSTRING(term FROM 1 FOR 1), UPPER(SUBSTRING(term FROM LENGTH(term) FOR 1))) = ?");
        pstmt.setString(1, s);
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            String term = rs.getString(2);
            System.out.printf("%d : %s\n", id, term);
        }
        rs.close();
        pstmt.close();
    }
    public void later(Connection con) throws SQLException {
        System.out.println("Later (Rdb):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE dt > CURRENT_TIMESTAMP");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            Timestamp dt = rs.getTimestamp(2);
            System.out.printf("%d %s\n", id, dt);
        }
        rs.close();
        pstmt.close();
    }
    public void thisMonth(Connection con) throws SQLException {
        System.out.println("This month (Rdb):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE EXTRACT(MONTH FROM dt) = EXTRACT(MONTH FROM CURRENT_TIMESTAMP) AND EXTRACT(YEAR FROM dt) = EXTRACT(YEAR FROM CURRENT_TIMESTAMP)");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            Timestamp dt = rs.getTimestamp(2);
            System.out.printf("%d %s\n", id, dt);
        }
        rs.close();
        pstmt.close();
    }
    public void getAutoIds(Connection con) throws SQLException {
        System.out.println("Id (Rdb):");
        Statement stmt = con.createStatement();
        PreparedStatement pstmt = con.prepareStatement("INSERT INTO aid (val) VALUES(?)");
        for(int i = 0; i < 3; i++) {
            pstmt.setString(1, "Bla bla bla");
            pstmt.executeUpdate();
            ResultSet rs = stmt.executeQuery("SELECT aid.CURRVAL FROM aid");
            if(rs.next()) {
                System.out.println(rs.getInt(1));
            } else {
                System.out.println("?");
            }
            rs.close();
        }
        pstmt.close();
        stmt.close();
    }
    public void limit(Connection con) throws SQLException {
        System.out.println("Limit (Rdb):");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id FROM customer FETCH FIRST 3 ROW ONLY");
        while(rs.next()) {
            int id = rs.getInt(1);
            System.out.printf("%d\n", id);
        }
        rs.close();
        stmt.close();
    }
}
