package szakdolgozat.server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;

import szakdolgozat.shared.User;
import szakdolgozat.shared.UserStats;

public class DBLister {

    public static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";

    public static final String PROTOCOL = "jdbc:derby:users;";

    public static void main(String[] args) throws Exception {
        try {
            Class.forName(DRIVER).newInstance();
            Connection con = DriverManager.getConnection(PROTOCOL);
            Statement s = con.createStatement();
            ResultSet rs = s.executeQuery("SELECT * FROM users");
            List result = ObjectFactory.convertToObjects(rs, User.class);
            for (Iterator itr = result.iterator(); itr.hasNext();) {
                User t = (User) itr.next();
                System.out.println(t);
            }
            rs = s.executeQuery("SELECT * FROM userStats");
            result = ObjectFactory.convertToObjects(rs, UserStats.class);
            for (Iterator itr = result.iterator(); itr.hasNext();) {
                UserStats t = (UserStats) itr.next();
                System.out.println(t);
            }
        } finally {
            try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
            } catch (SQLException ignore) {}
        }
    }

}
