clean structure
This commit is contained in:
67
DEV/DEV3.1/TP_JDBC/Exercise1/Database.java
Normal file
67
DEV/DEV3.1/TP_JDBC/Exercise1/Database.java
Normal file
@@ -0,0 +1,67 @@
|
||||
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 Votes getVotesFromCompetitor(String country) {
|
||||
try {
|
||||
PreparedStatement query = this.connection.prepareStatement(
|
||||
"SELECT Voters.name, Votes.points " +
|
||||
"FROM Votes " +
|
||||
"JOIN Countries AS Competitors ON Votes.competitor_id = Competitors.id " +
|
||||
"JOIN Countries AS Voters ON Votes.voter_id = Voters.id " +
|
||||
"WHERE Competitors.name = ?;"
|
||||
);
|
||||
query.setString(1, country);
|
||||
|
||||
ResultSet result = query.executeQuery();
|
||||
ArrayList<Vote> voteList = new ArrayList<>();
|
||||
|
||||
while(result.next()) {
|
||||
voteList.add(new Vote(result.getString("name"), result.getInt("points")));
|
||||
}
|
||||
|
||||
try {
|
||||
query.close();
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to close the query.");
|
||||
}
|
||||
|
||||
return new Votes(country, voteList);
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to prepare or execute the query (" + exception.getMessage() + ").");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
this.connection.close();
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to close the connection.");
|
||||
}
|
||||
}
|
||||
}
|
||||
18
DEV/DEV3.1/TP_JDBC/Exercise1/Main.java
Normal file
18
DEV/DEV3.1/TP_JDBC/Exercise1/Main.java
Normal file
@@ -0,0 +1,18 @@
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Database database = new Database();
|
||||
|
||||
Votes votesForItaly = database.getVotesFromCompetitor("Pays-Bas");
|
||||
|
||||
System.out.println("Vote " + votesForItaly.getCompetitorName() + " :");
|
||||
for(Vote vote : votesForItaly.getVoters()) {
|
||||
System.out.println(vote.getVoterName() + " " + vote.getPoints());
|
||||
}
|
||||
|
||||
System.out.println("-------------------");
|
||||
System.out.println("Total " + votesForItaly.getTotalPoints());
|
||||
|
||||
database.close();
|
||||
}
|
||||
}
|
||||
18
DEV/DEV3.1/TP_JDBC/Exercise1/Vote.java
Normal file
18
DEV/DEV3.1/TP_JDBC/Exercise1/Vote.java
Normal file
@@ -0,0 +1,18 @@
|
||||
public class Vote {
|
||||
|
||||
private String voter;
|
||||
private int points;
|
||||
|
||||
public Vote(String voter, int points) {
|
||||
this.voter = voter;
|
||||
this.points = points;
|
||||
}
|
||||
|
||||
public String getVoterName() {
|
||||
return this.voter;
|
||||
}
|
||||
|
||||
public int getPoints() {
|
||||
return this.points;
|
||||
}
|
||||
}
|
||||
30
DEV/DEV3.1/TP_JDBC/Exercise1/Votes.java
Normal file
30
DEV/DEV3.1/TP_JDBC/Exercise1/Votes.java
Normal file
@@ -0,0 +1,30 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Votes {
|
||||
|
||||
private String competitor;
|
||||
private ArrayList<Vote> voters;
|
||||
|
||||
public Votes(String competitor, ArrayList<Vote> voters) {
|
||||
this.competitor = competitor;
|
||||
this.voters = voters;
|
||||
}
|
||||
|
||||
public String getCompetitorName() {
|
||||
return this.competitor;
|
||||
}
|
||||
|
||||
public ArrayList<Vote> getVoters() {
|
||||
return this.voters;
|
||||
}
|
||||
|
||||
public int getTotalPoints() {
|
||||
int sum = 0;
|
||||
|
||||
for(Vote vote : this.voters) {
|
||||
sum += vote.getPoints();
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
23
DEV/DEV3.1/TP_JDBC/Exercise1/data.sql
Normal file
23
DEV/DEV3.1/TP_JDBC/Exercise1/data.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
CREATE TABLE Countries (
|
||||
id INT PRIMARY KEY,
|
||||
name VARCHAR(30)
|
||||
);
|
||||
|
||||
CREATE TABLE Votes (
|
||||
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||
competitor_id INT REFERENCES Countries(id),
|
||||
voter_id INT REFERENCES Countries(id),
|
||||
points INT NOT NULL,
|
||||
UNIQUE (competitor_id, voter_id)
|
||||
)
|
||||
|
||||
INSERT INTO Countries VALUES (1, "France");
|
||||
INSERT INTO Countries VALUES (2, "Pays-Bas");
|
||||
INSERT INTO Countries VALUES (3, "Albanie");
|
||||
INSERT INTO Countries VALUES (4, "Italie");
|
||||
INSERT INTO Countries VALUES (5, "Russie");
|
||||
INSERT INTO Votes VALUES (2, 4, 5);
|
||||
INSERT INTO Votes VALUES (2, 5, 5);
|
||||
INSERT INTO Votes VALUES (4, 2, 16);
|
||||
INSERT INTO Votes VALUES (5, 2, 5);
|
||||
INSERT INTO Votes VALUES (5, 4, 8);
|
||||
3
DEV/DEV3.1/TP_JDBC/Exercise1/start.sh
Executable file
3
DEV/DEV3.1/TP_JDBC/Exercise1/start.sh
Executable file
@@ -0,0 +1,3 @@
|
||||
rm -rf *.class
|
||||
javac Main.java;
|
||||
java -cp ".:/export/documents/mariadb-client.jar" Main;
|
||||
68
DEV/DEV3.1/TP_JDBC/Exercise2/Database.java
Normal file
68
DEV/DEV3.1/TP_JDBC/Exercise2/Database.java
Normal file
@@ -0,0 +1,68 @@
|
||||
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 Votes getVotesFromCompetitor(String country) {
|
||||
try {
|
||||
PreparedStatement query = this.connection.prepareStatement(
|
||||
"SELECT VoterVotes.points " +
|
||||
"FROM Votes " +
|
||||
"JOIN Countries AS Competitors ON Votes.competitor_id = Competitors.id " +
|
||||
"JOIN Countries AS Voters ON Votes.voter_id = Voters.id " +
|
||||
"WHERE Competitors.name = ?;"
|
||||
);
|
||||
query.setString(1, country);
|
||||
|
||||
ResultSet result = query.executeQuery();
|
||||
ArrayList<Vote> voteList = new ArrayList<>();
|
||||
|
||||
while(result.next()) {
|
||||
voteList.add(new Vote(result.getString("name"), result.getInt("points")));
|
||||
}
|
||||
|
||||
try {
|
||||
query.close();
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to close the query.");
|
||||
}
|
||||
|
||||
return new Votes(country, voteList);
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to prepare or execute the query (" + exception.getMessage() + ").");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
try {
|
||||
this.connection.close();
|
||||
} catch(SQLException exception) {
|
||||
System.err.println("Error while trying to close the connection.");
|
||||
}
|
||||
}
|
||||
}
|
||||
3
DEV/DEV3.1/TP_JDBC/Exercise2/Main.java
Normal file
3
DEV/DEV3.1/TP_JDBC/Exercise2/Main.java
Normal file
@@ -0,0 +1,3 @@
|
||||
public class Main {
|
||||
|
||||
}
|
||||
5
DEV/DEV3.1/TP_JDBC/Exercise2/ScoreBoardWindow.java
Normal file
5
DEV/DEV3.1/TP_JDBC/Exercise2/ScoreBoardWindow.java
Normal file
@@ -0,0 +1,5 @@
|
||||
import javax.swing.JFrame;
|
||||
|
||||
public class ScoreBoardWindow extends JFrame {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user