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

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.sql.DataSource;

public class SelectJdbc {
    public static void main(String[] args) throws SQLException, IOException {
        Logger.getLogger("").setLevel(Level.OFF);
        DataSource ds = Config.getDataSource();
        try(Connection con = ds.getConnection()) {
            try(PreparedStatement sel = con.prepareStatement("SELECT id,val FROM t WHERE id = ?")) {
                int n = 0;
                for(int i = 0; i < 200; i++) {
                    sel.setInt(1, i + 1);
                    try(ResultSet rs = sel.executeQuery()) {
                        if(rs.next() ) {
                            int id = rs.getInt(1);
                            String val = rs.getString(2);
                            System.out.printf("%d %s\n", id, val);
                            n++;
                        }
                    }
                }
                System.out.printf("Total individual: %d\n", n);
            }
            try(PreparedStatement sel = con.prepareStatement("SELECT id,val FROM t")) {
                try(ResultSet rs = sel.executeQuery()) {
                    int n = 0;
                    while(rs.next() ) {
                        int id = rs.getInt(1);
                        String val = rs.getString(2);
                        System.out.printf("%d %s\n", id, val);
                        n++;
                    }
                    System.out.printf("Total unordered: %d\n", n);
                }
            }
            try(PreparedStatement sel = con.prepareStatement("SELECT id,val FROM t WHERE id BETWEEN ? AND ?")) {
                sel.setInt(1, 10);
                sel.setInt(2, 20);
                try(ResultSet rs = sel.executeQuery()) {
                    int n = 0;
                    while(rs.next() ) {
                        int id = rs.getInt(1);
                        String val = rs.getString(2);
                        System.out.printf("%d %s\n", id, val);
                        n++;
                    }
                    System.out.printf("Total interval: %d\n", n);
                }
            }
            try(PreparedStatement sel = con.prepareStatement("SELECT id,val FROM t ORDER BY id")) {
                try(ResultSet rs = sel.executeQuery()) {
                    int n = 0;
                    while(rs.next() ) {
                        int id = rs.getInt(1);
                        String val = rs.getString(2);
                        System.out.printf("%d %s\n", id, val);
                        n++;
                    }
                    System.out.printf("Total ordered: %d\n", n);
                }
            }
        }
    }
}
