APL/DEV 3.1/TP3/Tableau/Tableau.java

105 lines
3.5 KiB
Java
Raw Normal View History

2022-09-21 11:14:37 +02:00
import java.sql.*;
import javax.swing.*;
import java.awt.*;
public class Tableau {
2022-09-21 15:40:01 +02:00
private static void changeConstraints(GridBagConstraints g, int gx, int gy, int gw, int gh, int anchor, int fill, double wx, double wy, Insets i) {
g.gridx = gx;
g.gridy = gy;
g.gridwidth = gw;
g.gridheight = gh;
g.anchor = anchor;
g.fill = fill;
g.weightx = wx;
g.weighty = wy;
g.insets = i;
}
2022-09-21 11:14:37 +02:00
public static void main(String[] args) {
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.err.println("MariaDB driver not found");
return;
}
Connection cnx;
try {
cnx = DriverManager.getConnection("jdbc:mariadb://dwarves.iut-fbleau.fr/horville", "horville", "a5gc95");
} catch (SQLException ex) {
System.err.println("Unable to access Database.");
return;
}
2022-09-21 15:34:09 +02:00
JFrame f = new JFrame();
f.setLayout(new GridBagLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(150, 500);
GridBagConstraints g = new GridBagConstraints();
2022-09-21 11:14:37 +02:00
try {
PreparedStatement req = cnx.prepareStatement("SELECT Module.code, Champ.code FROM Champ JOIN Module ON Champ.id = Module.id ORDER BY Module.code ASC;");
req.executeUpdate();
ResultSet rs = req.executeQuery();
2022-09-21 15:34:09 +02:00
String lastMod = null;
int length = 0;
int count = 0;
2022-09-21 11:14:37 +02:00
while (rs.next()) {
2022-09-21 15:34:09 +02:00
String moduleName = rs.getString(1);
String fieldName = rs.getString(2);
if (!moduleName.equals(lastMod) && lastMod != null) {
JLabel modLabel = new JLabel(lastMod);
modLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
2022-09-21 15:40:01 +02:00
changeConstraints(g, 1, count - length, 1, length, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 1f, 1f, new Insets(0, 0, 0, 0));
2022-09-21 15:34:09 +02:00
f.add(modLabel, g);
length = 0;
lastMod = moduleName;
} else {
lastMod = moduleName;
}
JLabel fieldLabel = new JLabel(fieldName);
fieldLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
2022-09-21 15:40:01 +02:00
changeConstraints(g, 2, count, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, 1f, 1f, new Insets(0, 0, 0, 0));
2022-09-21 15:34:09 +02:00
f.add(fieldLabel, g);
length++;
count++;
2022-09-21 11:14:37 +02:00
}
2022-09-21 15:34:09 +02:00
JLabel modLabel = new JLabel(lastMod);
modLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
g.gridx = 1;
g.gridy = count - length;
g.gridwidth = 1;
g.gridheight = length;
g.anchor = GridBagConstraints.CENTER;
g.fill = GridBagConstraints.BOTH;
g.weightx = 1f;
g.weighty = 1f;
g.insets = new Insets(0, 0, 0, 0);
f.add(modLabel, g);
2022-09-21 11:14:37 +02:00
rs.close();
req.close();
} catch (SQLException ex) {
System.err.println("SQL Request exception.");
System.err.println (ex);
}
2022-09-21 15:34:09 +02:00
f.setVisible(true);
2022-09-21 11:14:37 +02:00
try {
cnx.close();
} catch (SQLException ex) {
System.err.println("Unable to close connection.");
}
}
}