[clos] Java Client/Serveur avec hibernate

Fermé
manidis Messages postés 13 Date d'inscription vendredi 2 mai 2008 Statut Membre Dernière intervention 19 août 2012 - 13 mai 2012 à 00:27
momouna Messages postés 8 Date d'inscription lundi 7 mars 2016 Statut Membre Dernière intervention 7 mars 2016 - 7 mars 2016 à 13:16
Bonjour!!
J'ai un problème avec mon code avec mon application serveur pour l'insertion d'un client au niveau de la table. J'utilise hibernate au niveau du serveur.
Voici mon code:
Serveur:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package View;

import java.net.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import Modele.*;
import Controler.*;
import java.util.ArrayList;

/**
*
* @author Administrateur
*/
public class FrmServer extends JFrame implements ActionListener {

private JTextArea zonerecep;
private JButton qt;
private JPanel pan1, pan2;
ArrayList liste = null;

public FrmServer() {
zonerecep = new JTextArea(15, 40);
qt = new JButton("Quitter");
setTitle("Interface Serveur Multiclients");
pan1 = new JPanel();
pan2 = new JPanel();
pan1.add(new JScrollPane(zonerecep));
qt.addActionListener(this);
pan2.add(qt);
add(pan1, BorderLayout.CENTER);
add(pan2, BorderLayout.SOUTH);
setSize(500, 600);
setVisible(true);


try {
ServerSocket serv = new ServerSocket(8000);
zonerecep.append("Le Serveur a demarre " + "\n");
int numclient = 1;
while (true) {
Socket socket = serv.accept();
InetAddress adr = socket.getInetAddress();
String ip = adr.getHostAddress();
zonerecep.append("client n°:" + numclient + " adresse ip:" + ip + "\n");
Service s = new Service(socket);
s.start();
numclient++;
}

} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
//class interne

class Service extends Thread {


Socket socket;

public Service(Socket socket) {
this.socket = socket;

}

public void run() {

try {

OutputStream os = socket.getOutputStream();
InputStream is = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(is);
ObjectOutputStream oos = new ObjectOutputStream(os);
String mode;
do {
mode = (String) ois.readObject();
zonerecep.append("mode en cours d\'expoitation!!!: " + mode + "\n");
Persistence bd = new Persistence();

//------------------------------
//Opérations agence
//-----------------------------
if (mode.equals("ajout_Agence")) {
Agence a = (Agence) ois.readObject();

a.setNomag(a.getNomag());
a.setAdresseag(a.getAdresseag());
bd.SaveByHibernateAgence(a);



}

if (mode.equals("liste_Agence")) {


liste = bd.ViewRecordByHibernateAgence();
ArrayList la = new ArrayList();

for (Agence a : liste) {
Agence ag = new Agence();
ag.setCodeag(a.getCodeag());
ag.setNomag(a.getNomag());
ag.setAdresseag(a.getAdresseag());
la.add(ag);
}
oos.writeObject(la);
oos.flush();
//System.out.println("bonjour"+ liste.size());


//------------------------------
//Opérations client
//-----------------------------


if (mode.equals("ajout_Client")) {
Client c = (Client) ois.readObject();

Agence a=new Agence();

c.setAgence(c.getAgence());
c.setNomcli(c.getNomcli());
c.setPrenomcli(c.getPrenomcli());
c.setAdressecli(c.getAdressecli());
bd.SaveByHibernateClient(c);



}
if (mode.equals("fin")) {
zonerecep.append("Connexion terminee!!! pour un client" + "\n");
oos.flush();

}
}
} while (true);

} catch (Exception ex) {
System.out.println("****" + ex.getMessage());
}



}
}//fin classe interne

public void actionPerformed(ActionEvent e) {
dispose();
System.exit(0);
}

public static void main(String args[]) {
new FrmServer();
}
}

---------------------------------
Code client
----------------------------------


public class Ajout_Client extends javax.swing.JFrame implements ActionListener{
socket_methode sm=new socket_methode();
/** Creates new form Ajout_Client */
public Ajout_Client() {
initComponents();
TxtNomClient.requestFocus();
BtnAdd.addActionListener(this);
BtnQuit.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==BtnAdd)
{
Client c;
Agence a;
try
{
// if(TxtNomAgence==null)
// {
// JOptionPane.showMessageDialog(null, "Veuillez rensigner le nom de l\'agence");
// }
// else
// if(TxtAdresseAgence==null)
// {
// JOptionPane.showMessageDialog(null, "Veuillez rensigner l\'adresse de l\'agence");
// }
// else


sm.oos.writeObject("ajout_Client");
sm.oos.flush();
String code=new String(TxtCodeAgence.getText());
//String snomag=TxtNomAgence.getText();
String snom= new String(TxtNomClient.getText());
String sprenom=new String(TxtPrenomClient.getText());
String sAdresse=new String(TxtRAdresseClient.getText());
c=new Client();
a=new Agence();
a.setCodeag(Integer.parseInt(code));
c.setAgence(a);
c.setNomcli(snom);
c.setPrenomcli(sprenom);
c.setAdressecli(sAdresse);
sm.oos.writeObject(c);
sm.oos.flush();
TxtCodeAgence.setText("");
TxtNomAgence.setText("");
TxtNomClient.setText("");
TxtPrenomClient.setText("");
TxtRAdresseClient.setText("");


}
catch(Exception ex)
{
System.out.print(ex.getMessage());
}
}
else
if(e.getSource()==BtnQuit)
{
try
{
sm.oos.writeObject("End");
sm.oos.flush();
dispose();
System.exit(0);
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jPanel2 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
TxtNomClient = new javax.swing.JTextField();
TxtPrenomClient = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
TxtRAdresseClient = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
BtnAdd = new javax.swing.JButton();
BtnQuit = new javax.swing.JButton();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
TxtCodeAgence = new javax.swing.JTextField();
TxtNomAgence = new javax.swing.JTextField();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel2.setBackground(new java.awt.Color(153, 153, 255));

jLabel3.setText("Nom Client");

jLabel4.setText("Prénom Client");

jLabel5.setText("Adresse Client");

TxtRAdresseClient.setColumns(20);
TxtRAdresseClient.setRows(5);
jScrollPane1.setViewportView(TxtRAdresseClient);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3))
.addGap(21, 21, 21)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(TxtNomClient, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE)
.addComponent(TxtPrenomClient, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 166, Short.MAX_VALUE))))
.addGap(22, 22, 22))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(20, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(TxtNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(TxtPrenomClient, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(16, 16, 16))
);

jPanel3.setBackground(new java.awt.Color(153, 153, 255));

BtnAdd.setText("Ajouter");
BtnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
BtnAddActionPerformed(evt);
}
});

BtnQuit.setText("Quitter");

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(BtnAdd)
.addGap(54, 54, 54)
.addComponent(BtnQuit)
.addContainerGap(80, Short.MAX_VALUE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
.addContainerGap(31, Short.MAX_VALUE)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(BtnAdd)
.addComponent(BtnQuit))
.addGap(21, 21, 21))
);

jPanel1.setBackground(new java.awt.Color(204, 204, 255));

jLabel1.setText("Code Agence");

jLabel2.setText("Nom Agence");

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addGap(62, 62, 62)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(TxtCodeAgence)
.addComponent(TxtNomAgence, javax.swing.GroupLayout.DEFAULT_SIZE, 165, Short.MAX_VALUE))
.addContainerGap(86, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(21, 21, 21)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(TxtCodeAgence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(TxtNomAgence, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(33, Short.MAX_VALUE))
);

jMenu1.setText("File");
jMenuBar1.add(jMenu1);

jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);

setJMenuBar(jMenuBar1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(58, 58, 58)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(105, 105, 105)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(28, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(97, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(88, 88, 88))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(35, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void BtnAddActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Ajout_Client().setVisible(true);
}
});
}

4 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
14 mai 2012 à 09:09
Salut,

Personne ne lira tout ce code. Essaye de nous décrire ton problème précisément.
0
manidis Messages postés 13 Date d'inscription vendredi 2 mai 2008 Statut Membre Dernière intervention 19 août 2012
14 mai 2012 à 17:55
Merci pour la réponse.
Le problème se trouve au niveau du code que j'ai souligné en bleu.
C'est un application client/serveur avec hibernate pour la gestion bancaire.
J'ai une agence que j'ai déjà créé et je voulais ajout un client pour cette agence. Hibernate en gérant les clés étrangères, il amène tout l'objet.
J'ai l'objet Agence (int codeag,string Nomag, string adresseag).
Au niveau du client, comme déserialiser l'objet agence pour dire à hibernate qu'on insert comme clé étrangère dans la table client(int codecli (auto-increment),nomcli,prenomcli,codeag)) de l'objet agence?
Merci d'avance.
0
kaabi10aymen Messages postés 5 Date d'inscription jeudi 2 février 2012 Statut Membre Dernière intervention 15 mai 2012
15 mai 2012 à 22:54
bonjour;
svp j'ai un sujet suivant;

Sujet : Implémentation d'un simulateur de traceroute.
Description du sujet2 : L'objectif du travail est d'implémenter en Java un simulateur de traceroute. Traceroute est un outil réseau qui permet de découvrir le chemin qu'un paquet de données prend pour aller d'une machine S (la source ou moniteur) vers une machine D (la destination). Traceroute fonctionne comme suit: la source du traceroute, S, envoie dans le réseau des paquets en incrémentant pas à pas le champ time-to-live (TTL), le TTL étant un champ de l'en- tête du paquet IP. Le TTL a pour but d'indiquer combien de temps un paquet peut circuler dans le réseau. Chaque fois qu'un paquet entre dans un routeur, le routeur décrémente le TTL. Quand le TTL vaut 1, le routeur détermine que le paquet a consommé suffisamment de ressources dans le réseau, le jette et informe la source du paquet de l'incident en renvoyant un message ICMP time exceeded. En regardant l'adresse IP source du message ICMP, le moniteur peut apprendre l'adresse du routeur qui a jeté le paquet.
L'idée du travail est de simuler un tel comportement sur la base d'une architecture Client/Serveur.
Le client est considéré comme étant la source du traceroute. Il veut connaître le chemin entre lui- même et une destination. Pour ce faire, il adresse des requêtes traceroute au serveur. Ces requêtes comprennent trois informations:
• l'adresse (ou l'identifiant) du client.
• l'adresse (ou l'identifiant) de la destination.
• la valeur du TTL. A noter aussi que le client peut vouloir simplement connaître la longueur du chemin entre lui-même et une destination particulière. Par longueur de chemin, nous entendons le nombre de sauts. De son côté, le serveur charge en mémoire une topologie (fichier XML ou fichier texte) représentant Internet. Il doit être capable d'accepter des requêtes de la part de clients. Pour chaque requête traceroute, il fournit comme réponse l'adresse de la machine (routeur intermédiaire ou destination) se situant à TTL sauts du client en direction de la destination. Pour des requêtes sur la longueur du chemin, il renvoie simplement le nombre de sauts entre le client et la destination.
Dans ce travail, il est demandé d'implémenter:
• Un module permettant de représenter en mémoire un réseau. Des informations sur ce réseau sont fournies en entrée sous la forme d'un fichier XML ou d'un fichier texte (au choix).
• Un module permettant de connaître la longueur du chemin entre un client et une destination (i.e. nombre de sauts).
• Un module permettant de répondre à des requêtes « traceroute ».
• Le serveur utilisant ces différents modules. Le serveur doit pouvoir gérer simultanément plusieurs requêtes. Les requêtes sont transmises du client au serveur sous la forme de requête Java RMI( c’est obligatoire avec de requette RMI si ne trouve pas )
• Un exemple de client faisant des requêtes au serveur.
Ce simulateur doit être implémenté en Java.

Svp je besoin d'aide à ce application , vous pouvez donner le code java basé sur client/serveur qui réalise sa et merci
(vous pouvez le code sur l'email suivant: kaabi_aymen@hotmai.fr)
0
momouna Messages postés 8 Date d'inscription lundi 7 mars 2016 Statut Membre Dernière intervention 7 mars 2016
7 mars 2016 à 13:15
salut moi aussi je travaille sur le même sujet
Svp j'ai besoin de votre aide et merci d'avances
0
cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
16 mai 2012 à 07:57
0
momouna Messages postés 8 Date d'inscription lundi 7 mars 2016 Statut Membre Dernière intervention 7 mars 2016
7 mars 2016 à 13:16
salut moi aussi je travaille sur le même sujet
Svp j'ai besoin de votre aide et merci d'avance
0
Rejoignez-nous