This commit is contained in:
LMAGallois 2020-12-15 16:10:01 +01:00
parent 9a77e9149b
commit fc16e1a74e
2 changed files with 194 additions and 0 deletions

View file

@ -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");
}
}

View file

@ -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");
};
}
}