some changes
This commit is contained in:
18
DEV/DEV3.1/TP03/Exercise2/Champ.java
Normal file
18
DEV/DEV3.1/TP03/Exercise2/Champ.java
Normal file
@@ -0,0 +1,18 @@
|
||||
public class Champ {
|
||||
|
||||
private String code;
|
||||
private String[] modules;
|
||||
|
||||
public Champ(String code, String[] modules) {
|
||||
this.code = code;
|
||||
this.modules = modules;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return this.code;
|
||||
}
|
||||
|
||||
public String[] getModules() {
|
||||
return this.modules;
|
||||
}
|
||||
}
|
44
DEV/DEV3.1/TP03/Exercise2/Database.java
Normal file
44
DEV/DEV3.1/TP03/Exercise2/Database.java
Normal file
@@ -0,0 +1,44 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Database {
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public Database() {
|
||||
try {
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
|
||||
this.connection = DriverManager.getConnection(
|
||||
"jdbc:mariadb://dwarves.iut-fbleau.fr/baudrier",
|
||||
"baudrier",
|
||||
"baudrier"
|
||||
);
|
||||
} catch(ClassNotFoundException exception) {
|
||||
System.err.println("Class 'org.mariadb.jdbc.Driver' not found.");
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to connect to the database");
|
||||
}
|
||||
}
|
||||
|
||||
public Champ[] getAllData() {
|
||||
PreparedStatement query = this.connection.prepareStatement("SELECT id, code FROM Champ");
|
||||
ResultSet result = query.executeQuery();
|
||||
|
||||
while(result.next()) {
|
||||
query = this.connection.prepareStatement("SELECT code FROM Modules WHERE champId = " + result.getInt("id"));
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
this.connection.close();
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to close the connection.");
|
||||
}
|
||||
}
|
||||
}
|
1
DEV/DEV3.1/TP03/Exercise2/Main.java
Normal file
1
DEV/DEV3.1/TP03/Exercise2/Main.java
Normal file
@@ -0,0 +1 @@
|
||||
|
21
DEV/DEV3.1/TP03/Exercise2/schema.sql
Normal file
21
DEV/DEV3.1/TP03/Exercise2/schema.sql
Normal file
@@ -0,0 +1,21 @@
|
||||
CREATE TABLE Champ (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
code VARCHAR(20)
|
||||
);
|
||||
|
||||
CREATE TABLE Module (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
champId INT REFERENCES Champ(id),
|
||||
code VARCHAR(20)
|
||||
);
|
||||
|
||||
INSERT INTO Champ(code) VALUES ("APL");
|
||||
INSERT INTO Champ(code) VALUES ("WIM");
|
||||
|
||||
INSERT INTO Module(champId, code) VALUES (1, "M1102");
|
||||
INSERT INTO Module(champId, code) VALUES (1, "M1103");
|
||||
INSERT INTO Module(champId, code) VALUES (1, "M3103");
|
||||
INSERT INTO Module(champId, code) VALUES (2, "M1105");
|
||||
INSERT INTO Module(champId, code) VALUES (2, "M3104");
|
||||
INSERT INTO Module(champId, code) VALUES (2, "M4103C");
|
||||
INSERT INTO Module(champId, code) VALUES (2, "M4104C");
|
Reference in New Issue
Block a user