Files
BUT2/DEV/DEV3.1/TP03/Exercise2/Database.java
2025-10-02 10:48:05 +02:00

45 lines
1.3 KiB
Java

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.");
}
}
}