-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
295d433
commit e539c4a
Showing
2 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
|