// 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 MySQLDemo implements Demo {
    public void findAbbreviation(Connection con) throws SQLException {
        String s = "IN";
        System.out.println("String (MySQL):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,term FROM abbr " +
                                                       "WHERE CONCAT(LEFT(term, 1), UPPER(RIGHT(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 (MySQL):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE dt > NOW()");
        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 (MySQL):");
        PreparedStatement pstmt = con.prepareStatement("SELECT id,dt FROM ts WHERE MONTH(dt) = MONTH(NOW()) AND YEAR(dt) = YEAR(NOW())");
        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 (MySQL):");
        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 LAST_INSERT_ID()");
            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 (MySQL):");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id FROM customer LIMIT 3");
        while(rs.next()) {
            int id = rs.getInt(1);
            System.out.printf("%d\n", id);
        }
        rs.close();
        stmt.close();
    }
}
