// 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 DerbyDemo implements Demo {
    public void findAbbreviation(Connection con) throws SQLException {
        String s = "IN";
        System.out.println("String (Derby):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,term FROM abbr " +
                                                       "WHERE SUBSTR(term, 1, 1) || UPPER(SUBSTR(term, LENGTH(term), 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 (Derby):");
        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 (Derby):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE MONTH(dt) = MONTH(CURRENT_TIMESTAMP) AND YEAR(dt) = YEAR(CURRENT_TIMESTAMP)");
        ResultSet rs = pstmt.executeQuery();
        while(rs.next()) {
            int id = rs.getInt(1);
            String dt = rs.getString(2); // Timestamp dt = rs.getTimestamp(2); does not work
            System.out.printf("%d %s\n", id, dt);
        }
        rs.close();
        pstmt.close();
    }
    public void getAutoIds(Connection con) throws SQLException {
        System.out.println("Id (Derby):");
        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 IDENTITY_VAL_LOCAL() FROM aid");
            if(rs.next()) {
                System.out.println(rs.getBigDecimal(1));
            } else {
                System.out.println("?");
            }
            rs.close();
        }
        pstmt.close();
        stmt.close();
    }
    public void limit(Connection con) throws SQLException {
        System.out.println("Limit (Derby):");
        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();
    }
}
