import org.mariadb.jdbc.*;
import java.awt.*;
import java.sql.*;

public class Donnees {

	private String[] modules;
	private String[] champs;
	private int[] idChamps;

	public Donnees() {
		// FAIRE UN TABLEAU PAR TABLE DE LA BASE DE DONNEES AU LIEU DE RASSEMBLER LES DEUX 

		try {
			Connection cnx = DriverManager.getConnection(
				"jdbc:mariadb://dwarves.iut-fbleau.fr/simoes",
				"simoes", "simoes"
			);

			try {
				Class.forName("org.mariadb.jdbc.Driver");
			} catch (ClassNotFoundException e2) {
				System.err.println("Problème de pilote.");
			}

			try {

				// QUERY 1
				PreparedStatement pst = cnx.prepareStatement(
					"SELECT * FROM module;"
				);

				ResultSet rs = pst.executeQuery();

				rs.last();

				this.modules = new String[rs.getInt(1)];

				rs.first();

				int i = 0;
				this.modules[i] = rs.getString(2);

				while (rs.next()) {
					i++;
					this.modules[i] = rs.getString(2);
				}

				rs.close();
				pst.close();

				// QUERY 2
				
				PreparedStatement pst2 = cnx.prepareStatement(
					"SELECT COUNT(*) FROM champ;"
				);

				ResultSet rs2 = pst2.executeQuery();

				rs2.last();

				this.idChamps = new int[rs2.getInt(1)];
				this.champs = new String[rs2.getInt(1)];

				rs2.close();
				pst2.close();

				// QUERY 3
				PreparedStatement pst3 = cnx.prepareStatement( 
					"SELECT * FROM champ;"
				);

				ResultSet rs3 = pst3.executeQuery();

				i = 0;

				while (rs3.next()) {
					this.idChamps[i] = rs3.getInt(1);
					this.champs[i] = rs3.getString(2);
					i++;
				}

				for (i = 0; i != this.champs.length; i++) {
					System.out.println(this.idChamps[i] + " | " + this.champs[i]);
				} 

				rs3.close();
				pst3.close();

				// TODO

			} catch (SQLException e3) {
				System.err.println("Erreur de syntaxe SQL.");
			}

			cnx.close();
		} catch (SQLException e1) {
			System.err.println("Erreur de connexion à la base de données.");
		}
	}


	public String[] getModules() {
		return this.modules;
	}

	public int[] getIdChamps() {
		return this.idChamps;
	}

	public String[] getChamps() {
		return this.champs;
	}
}