From fc16e1a74e442f50248e78c878cdf85b21cfc61c Mon Sep 17 00:00:00 2001 From: LMAGallois Date: Tue, 15 Dec 2020 16:10:01 +0100 Subject: [PATCH] bdd --- .../Clavardage/src/controller/Historique.java | 118 ++++++++++++++++++ .../Clavardage/src/model/Database.java | 76 +++++++++++ 2 files changed, 194 insertions(+) create mode 100644 Application/Clavardage/src/controller/Historique.java create mode 100644 Application/Clavardage/src/model/Database.java diff --git a/Application/Clavardage/src/controller/Historique.java b/Application/Clavardage/src/controller/Historique.java new file mode 100644 index 0000000..da6599d --- /dev/null +++ b/Application/Clavardage/src/controller/Historique.java @@ -0,0 +1,118 @@ +import java.sql.Connection; +import java.sql.Date; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Calendar; + +public class Historique { + private Database bdd; + private ResultSet rs_received; + private ResultSet rs_sent; + private ResultSetMetaData rsmd_received; + private ResultSetMetaData rsmd_sent; + + public void setRSReceived(ResultSet rs) { + this.rs_received=rs; + } + + public void setRSSent(ResultSet rs) { + this.rs_sent=rs; + } + + public ResultSet getRSSent() { + return this.rs_sent; + } + + public ResultSet getRSReceived() { + return this.rs_received; + } + + public void setRSMDReceived(ResultSetMetaData rsmd) { + this.rsmd_received=rsmd; + } + public void setRSMDSent(ResultSetMetaData rsmd) { + this.rsmd_sent=rsmd; + } + public ResultSetMetaData getRSMDReceived() { + return this.rsmd_received; + } + public ResultSetMetaData getRSMDSent() { + return this.rsmd_sent; + } + + public void saveMessage(User user1, RemoteUser user2, String input, String date) { + System.out.println("coucou"); + + int nb_changed_rows=0; + try { + nb_changed_rows = this.bdd.getStatement().executeUpdate("INSERT INTO chat values ("+user1.getIPcode()+","+user2.addIP+","+input+","+date+")"); + } catch (SQLException e) { + System.out.println("insertion pas établit"); + e.printStackTrace(); + } + //nb_changed_rows = this.bdd.getStatement().executeUpdate("DELETE FROM table where name=‘noname’"); + if (nb_changed_rows==0){ + System.out.println("la modification n'a pas eu lieue"); + } + } + + public String retrieveMessage(User user1, RemoteUser user2) throws SQLException { + this.setRSSent(this.bdd.getStatement().executeQuery("SELECT"+user1.getIPcode()+","+user2.getIPcode()+",? ,? FROM table")); + this.setRSReceived(this.bdd.getStatement().executeQuery("SELECT"+user2.getIPcode()+","+user1.getIPcode()+",?, ? FROM table")); + //this.setRSMDSent(this.getRSSent().getMetaData()); + //this.setRSMDReceived(this.getRSReceived().getMetaData()); + boolean encore1 = this.getRSSent().next(); + boolean encore2 = this.getRSReceived().next(); + String message=null; + String[] string1=null; + String[] string2=null; + Calendar date1=Calendar.getInstance(); + Calendar date2=Calendar.getInstance(); + while(encore1 || encore2) { + while (this.getRSSent().getInt(1)!=user2.getIPcode()) { + encore1=this.getRSSent().next(); + } + string1=this.getRSSent().getString(3).split(" "); + date1.set(Integer.valueOf(string1[0]), Integer.valueOf(string1[1]), Integer.valueOf(string1[2]),Integer.valueOf(string1[3]), Integer.valueOf(string1[4]), Integer.valueOf(string1[5])); + + + while (this.getRSReceived().getInt(0)!=user1.getIPcode()) { + encore2=this.getRSReceived().next(); + } + string2=this.getRSReceived().getString(3).split(" "); + date2.set(Integer.valueOf(string2[0]), Integer.valueOf(string2[1]), Integer.valueOf(string2[2]),Integer.valueOf(string2[3]), Integer.valueOf(string2[4]), Integer.valueOf(string2[5])); + + if (date1.compareTo(date2)>=0) { + message=message+this.getRSReceived().getString(2); + message=message+this.getRSSent().getString(2); + }else { + message=message+this.getRSSent().getString(2); + message=message+this.getRSReceived().getString(2); + } + encore1=this.getRSSent().next(); + encore2=this.getRSReceived().next(); + + } + + return message; + } + + public void close_all() throws SQLException { + this.rs_received.close(); + this.rs_sent.close(); + this.bdd.close(); + } + //MAIN + public void Historique() { + + this.bdd=new Database(); + System.out.println("coucoudb"); + + + } + + +} diff --git a/Application/Clavardage/src/model/Database.java b/Application/Clavardage/src/model/Database.java new file mode 100644 index 0000000..9cc6fbe --- /dev/null +++ b/Application/Clavardage/src/model/Database.java @@ -0,0 +1,76 @@ +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class Database { + private Statement statement; + private Connection con; + + public Statement getStatement() { + return this.statement; + } + + public void close () throws SQLException { + this.statement.close(); + this.con.close(); + + } + + + public void Database() { + try { + Class.forName("java.sql.Driver"); + System.out.println("coucou"); + } catch (ClassNotFoundException e) { + System.out.println("probleme avec le driver"); + e.printStackTrace(); + } + try { + this.con=DriverManager.getConnection("jdbc:mysql://srv-bdens.insa-toulouse.fr:3306","tp_servlet_003","povu3Ma2"); + System.out.println("coucou"); + } catch (SQLException e) { + System.out.println("co pas établit"); + e.printStackTrace(); + } + + try { + this.statement=this.con.createStatement(); + System.out.println("coucou"); + } catch (SQLException e) { + System.out.println("statement pas établit"); + e.printStackTrace(); + } + + int nb_changed_rows=0; + try { + nb_changed_rows = statement.executeUpdate("CREATE DATABASE base"); + System.out.println("coucou"); + } catch (SQLException e) { + System.out.println("base de données pas établit"); + e.printStackTrace(); + } + try { + nb_changed_rows = statement.executeUpdate("USE base"); + System.out.println("coucou"); + } catch (SQLException e) { + System.out.println("use base pas établit"); + e.printStackTrace(); + } + + try { + nb_changed_rows = statement.executeUpdate("CREATE TABLE chat (user_IPcode1 INTEGER, user_IPcode2 INTEGER, Message STRING, date STRING)"); + System.out.println("coucou"); + } catch (SQLException e) { + System.out.println("table non créée"); + e.printStackTrace(); + } + + if (nb_changed_rows==0){ + System.out.println("la modification n'a pas eu lieue"); + }; + + } + +}