package tutorialJava.capitulo9_AWT_SWING.v05_JListConDefaultListModel.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import tutorialJava.capitulo9_AWT_SWING.v05_JListConDefaultListModel.model.Provincia;


public class ControladorProvincia {

	
	public static List<Provincia> findAll() {
		List<Provincia> l = new ArrayList<Provincia>();
		
		try {
			ResultSet rs = ConnectionManager.getConnection().createStatement()
					.executeQuery("Select * from provincia");
			while (rs.next()) {
				Provincia p = new Provincia();
				p.setId(rs.getInt("id"));
				p.setDescripcion(rs.getString("descripcion"));
				l.add(p);
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return l;		
	}
	
	
	
	public static Provincia findById(int id) {
		Provincia p = null;
		
		try {
			ResultSet rs = ConnectionManager.getConnection().createStatement()
					.executeQuery("Select * from provincia where id = " + id);
			if (rs.next()) {
				p = new Provincia();
				p.setId(rs.getInt("id"));
				p.setDescripcion(rs.getString("descripcion"));
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return p;		
	}
	

}










