AtelierProDM/src/java/bdd/GradeMySQL.java
2025-01-09 21:29:22 +01:00

63 lines
2.2 KiB
Java

package bdd;
import beans.Grade;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/**
*
* @author noobm
*/
public class GradeMySQL {
private final Connection laConnection = Connexion.getConnect("172.20.10.5",
"sdis29",
"adminBDsdis",
"mdpBDsdis");
public Grade read(int id) {
Grade leGrade = null;
try {
PreparedStatement prepStmt = null;
String sql = "SELECT * FROM grade WHERE idGrade=?";
prepStmt = laConnection.prepareStatement(sql);
prepStmt.setInt(1, id);
ResultSet result = prepStmt.executeQuery();
if (result.next()) {
leGrade = new Grade(result.getInt(1), result.getString(2));
}
prepStmt.close();
} catch (SQLException ex) {
System.out.println("SQLExeption : " + ex.getMessage());
System.out.println("SQLState : " + ex.getSQLState());
System.out.println("Code erreur : " + ex.getErrorCode());
}
return leGrade;
}
// Lecture de tous les grades dans la base de données
public ArrayList<Grade> readGrades() {
ArrayList<Grade> lesGrades = new ArrayList<>();
try {
PreparedStatement prepStmt = null;
String sql = "SELECT * FROM grade";
prepStmt = laConnection.prepareStatement(sql);
ResultSet result = prepStmt.executeQuery();
while (result.next()) {
Grade grade = new Grade(result.getInt(1), result.getString(2));
lesGrades.add(grade);
}
prepStmt.close();
} catch (SQLException ex) {
System.out.println("SQLExeption : " + ex.getMessage());
System.out.println("SQLState : " + ex.getSQLState());
System.out.println("Code erreur : " + ex.getErrorCode());
}
return lesGrades;
}
}