Skip to content

Commit

Permalink
Achievement 5: Database.
Browse files Browse the repository at this point in the history
  • Loading branch information
SevillanoRobin authored and Prometheos2 committed Dec 7, 2019
1 parent 295d433 commit e539c4a
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
128 changes: 128 additions & 0 deletions src/model/Bdd/ConnectionDatabase.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

65 changes: 65 additions & 0 deletions src/model/Bdd/InformationDatabase.java
Original file line number Diff line number Diff line change
@@ -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;
}

}

0 comments on commit e539c4a

Please sign in to comment.