From e539c4aa65683b506c020c264ec6378e5a7dbe26 Mon Sep 17 00:00:00 2001 From: Robin Sevillano Date: Wed, 4 Dec 2019 22:30:45 +0100 Subject: [PATCH] Achievement 5: Database. --- src/model/Bdd/ConnectionDatabase.java | 128 +++++++++++++++++++++++++ src/model/Bdd/InformationDatabase.java | 65 +++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 src/model/Bdd/ConnectionDatabase.java create mode 100644 src/model/Bdd/InformationDatabase.java diff --git a/src/model/Bdd/ConnectionDatabase.java b/src/model/Bdd/ConnectionDatabase.java new file mode 100644 index 0000000..c574543 --- /dev/null +++ b/src/model/Bdd/ConnectionDatabase.java @@ -0,0 +1,128 @@ +/* + * Copyright (c) 04/12/2019 + * + * Auteurs : + * - Behm Guillaume + * - Claudel Adrien + * - Richez Guillaume + * - Sevillano Robin + */ + +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package model.Bdd; + +import java.sql.Connection; +import java.sql.DriverManager; + +/** + * + * @author castagno Classe utilisée pour établir une connexion avec la base de + * données, interroger la base et insérer de nouveaux tuples dans la base + */ +public class ConnectionDatabase { + + private String host, port, dbname, username, password; + private Connection con = null; + + public ConnectionDatabase(String h, String po, String dbn, String u, String p) { + this.host = h; + this.port = po; + this.dbname = dbn; + this.username = u; + this.password = p; + } + + + public InformationDatabase connexionBDD() { + this.openConnexion(); + return new InformationDatabase(this); + } + + /* + * Ouvre la connexion avec la base de données + */ + public void openConnexion() { + String connectUrl = "jdbc:mysql://" + this.host + ":" + this.port + "/" + this.dbname; + if (con != null) { + this.closeConnexion(); + } + try { + Class.forName("com.mysql.jdbc.Driver"); + con = DriverManager.getConnection(connectUrl, username, password); + System.out.println("Database connection established."); + } catch (ClassNotFoundException cnfe) { + System.out.println("Cannot load db driver: com.mysql.jdbc.Driver"); + cnfe.printStackTrace(); + } catch (Exception e) { + System.out.println("Erreur inattendue"); + e.printStackTrace(); + } + } + + /* + * Ferme la connexion avec la base de données + */ + public void closeConnexion() { + if (con != null) { + try { + con.close(); + System.out.println("Database connection terminated."); + } catch (Exception e) { + /* ignore close errors */ } + } + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public String getPort() { + return port; + } + + public void setPort(String port) { + this.port = port; + } + + public String getDbname() { + return dbname; + } + + public void setDbname(String dbname) { + this.dbname = dbname; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Connection getCon() { + return con; + } + + public void setCon(Connection con) { + this.con = con; + } + +} + diff --git a/src/model/Bdd/InformationDatabase.java b/src/model/Bdd/InformationDatabase.java new file mode 100644 index 0000000..7916d05 --- /dev/null +++ b/src/model/Bdd/InformationDatabase.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 04/12/2019 + * + * Auteurs : + * - Behm Guillaume + * - Claudel Adrien + * - Richez Guillaume + * - Sevillano Robin + */ + +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package model.Bdd; + +import java.sql.ResultSet; +import java.sql.Statement; + +/** + * + * @author utilisateur + */ +public class InformationDatabase { + + private final ConnectionDatabase connexion; + + public InformationDatabase(ConnectionDatabase connexion) { + this.connexion = connexion; + } + + public ResultSet requestSQLSelect(String query) { + ResultSet rst = null; + try { + Statement st = this.connexion.getCon().createStatement(); + rst = st.executeQuery(query); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return rst; + } + + public boolean requestSQLUpdate(String query) { + boolean b; + try { + Statement st = this.connexion.getCon().createStatement(); + st.executeUpdate(query); + b=true; + } catch (Exception ex) { + ex.printStackTrace(); + b=false; + } + + return b; + } + + + public ConnectionDatabase getConnexion() { + return connexion; + } + +} +