// 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.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Check {
    public static void test(String ds) throws SQLException {
        try(Connection con = DriverManager.getConnection(ConfigJdbc.ds2url(ds), ConfigJdbc.UN, ConfigJdbc.PW)) {
            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);
                        if(id <= 0 || 1000 < id || !val.startsWith("Item #")) {
                            throw new RuntimeException("Ooops");
                        }
                        n++;
                    }
                    System.out.printf("Total in %s: %d\n", ds, n);
                }
            }
        }
    }
    public static void main(String[] args) throws SQLException {
        test("ds1");
        test("ds2");
    }
}
