Update src/fr/iutfbleau/papillon/model/BaseDeDonnees.java
Ajout de liaison dwarves et la machine dans le fichier pour la base de donnée
This commit is contained in:
@@ -1,38 +1,85 @@
|
|||||||
package fr.iutfbleau.papillon.model;
|
import java.sql.*;
|
||||||
import org.mariadb.jdbc.MariaDbDataSource;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class BaseDeDonnees {
|
public class BaseDeDonnees {
|
||||||
|
|
||||||
private static Connection connexion;
|
|
||||||
|
|
||||||
public static Connection getConnexion() {
|
private static final String URL = "jdbc:mariadb://dwarves.iut-fbleau.fr:3306/yolou";
|
||||||
if (connexion == null) {
|
private static final String USER = "yolou";
|
||||||
try {
|
private static final String PASS = "serikhaneyolou";
|
||||||
MariaDbDataSource dataSource = new MariaDbDataSource();
|
|
||||||
dataSource.setUrl("jdbc:mariadb://localhost:3307/papillon");
|
|
||||||
dataSource.setUser("root");
|
|
||||||
dataSource.setPassword("mdp");
|
|
||||||
|
|
||||||
connexion = dataSource.getConnection();
|
|
||||||
System.out.println(" Connexion réussie !");
|
/** Connexion */
|
||||||
} catch (SQLException e) {
|
public static Connection connecter() throws SQLException {
|
||||||
e.printStackTrace();
|
try {
|
||||||
}
|
Class.forName("org.mariadb.jdbc.Driver");
|
||||||
}
|
} catch (ClassNotFoundException ignore) {}
|
||||||
return connexion;
|
return DriverManager.getConnection(URL, USER, PASS);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fermer() {
|
/** INSERT renvoie l'id généré. */
|
||||||
try {
|
public static int ajouter(Rappel r) throws SQLException {
|
||||||
if (connexion != null && !connexion.isClosed()) {
|
String sql = "INSERT INTO rappel(titre, contenu, theme, rang) VALUES (?,?,?,?)";
|
||||||
connexion.close();
|
try (Connection cnx = connecter();
|
||||||
System.out.println(" Connexion fermée.");
|
PreparedStatement pst = cnx.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||||
|
pst.setString(1, r.getTitre());
|
||||||
|
pst.setString(2, r.getContenu());
|
||||||
|
pst.setString(3, r.getTheme());
|
||||||
|
pst.setInt(4, r.getRang());
|
||||||
|
pst.executeUpdate();
|
||||||
|
|
||||||
|
try (ResultSet rs = pst.getGeneratedKeys()) {
|
||||||
|
if (rs.next()) {
|
||||||
|
int id = rs.getInt(1);
|
||||||
|
r.setId(id);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
return 0;
|
||||||
e.printStackTrace();
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** SELECT * ORDER BY rang, id. */
|
||||||
|
public static List<Rappel> lister() throws SQLException {
|
||||||
|
String sql = "SELECT id, titre, contenu, theme, rang FROM rappel ORDER BY rang ASC, id ASC";
|
||||||
|
List<Rappel> res = new ArrayList<>();
|
||||||
|
try (Connection cnx = connecter();
|
||||||
|
PreparedStatement pst = cnx.prepareStatement(sql);
|
||||||
|
ResultSet rs = pst.executeQuery()) {
|
||||||
|
while (rs.next()) {
|
||||||
|
res.add(new Rappel(
|
||||||
|
rs.getInt("id"),
|
||||||
|
rs.getString("titre"),
|
||||||
|
rs.getString("contenu"),
|
||||||
|
rs.getString("theme"),
|
||||||
|
rs.getInt("rang")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** UPDATE par id. */
|
||||||
|
public static int modifier(int id, Rappel r) throws SQLException {
|
||||||
|
String sql = "UPDATE rappel SET titre=?, contenu=?, theme=?, rang=? WHERE id=?";
|
||||||
|
try (Connection cnx = connecter();
|
||||||
|
PreparedStatement pst = cnx.prepareStatement(sql)) {
|
||||||
|
pst.setString(1, r.getTitre());
|
||||||
|
pst.setString(2, r.getContenu());
|
||||||
|
pst.setString(3, r.getTheme());
|
||||||
|
pst.setInt(4, r.getRang());
|
||||||
|
pst.setInt(5, id);
|
||||||
|
return pst.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** DELETE par id. */
|
||||||
|
public static int supprimer(int id) throws SQLException {
|
||||||
|
String sql = "DELETE FROM rappel WHERE id=?";
|
||||||
|
try (Connection cnx = connecter();
|
||||||
|
PreparedStatement pst = cnx.prepareStatement(sql)) {
|
||||||
|
pst.setInt(1, id);
|
||||||
|
return pst.executeUpdate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user