alin
alin

Reputation: 145

How to use Socket in my case?

I made an application, that uses Swing and Hibernate facilities and my teacher told me that I have to make a Client/Server communication using Socket(it's a homework for my university). For example the application is doing insert/update/delete for different tables and I have a JFrame, where the user is writing the data and after pressing the insert/update/delete button, changes to the database are being made. But I really don't know how to change this program so that it uses Socket. On the client has to be the button with the actionperformed that calls a method on the server, that makes changes to the database and then returns the result to the client? Or how should I use Socket in this program ?

Maybe someone could help me with some ideas. Thanks in advance!

Here is the code, where I'm using Swing and Hibernate:

public class AdaugaProdus extends javax.swing.JFrame {
private final Session session;

public AdaugaProdus() {
    session = HibernateUtil.getSessionFactory().openSession();
    initComponents();
    initComboBoxes();
}                       

private void initComboBoxes() {
    IdFurnComboBox.removeAllItems();
    IdCatComboBox.removeAllItems();
    IdRaftComboBox.removeAllItems();

    List<Furnizor> furnizori = session.createQuery("from Furnizor").list();
    for (Furnizor furnizor : furnizori)
        IdFurnComboBox.addItem(furnizor);

    List<Categorie> categorii = session.createQuery("from Categorie").list();
    for (Categorie categorie : categorii)
        IdCatComboBox.addItem(categorie);

    List<Istoricprod> rafturi = session.createQuery("from Istoricprod").list();
    for (Istoricprod raft : rafturi)
        IdRaftComboBox.addItem(raft);

}

private void InsereazaButtonActionPerformed(java.awt.event.ActionEvent evt)    {                                                
    runQueryBasedOnInsert();
}                                               

private void runQueryBasedOnInsert(){

try{
    org.hibernate.Transaction tx = session.beginTransaction();

    Produs produs = new Produs();

    int idFurn = ((Furnizor)IdFurnComboBox.getSelectedItem()).getIdfurn();
    Furnizor furnizor = new Furnizor(idFurn);
    produs.setFurnizor(furnizor);

    int idCat = ((Categorie)IdCatComboBox.getSelectedItem()).getIdcat();
    Categorie categorie = new Categorie(idCat);
    produs.setCategorie(categorie);

    int idRaft = ((Istoricprod)IdRaftComboBox.getSelectedItem()).getIdraft();
    Istoricprod istoricprod = new Istoricprod(idRaft);
    produs.setIstoricprod(istoricprod);

    produs.setDenumire(DenumireTextField.getText());
    produs.setCantitate(Integer.parseInt(CantitateTextField.getText()));
    produs.setUnitmas(UnitMasTextField.getText());

    session.save(produs);

    tx.commit();
  }
  catch(Exception e){
  System.out.println(e.getMessage());
  } 
   /*finally{
   session.close();
   }*/
   }

public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new AdaugaProdus().setVisible(true);
        }
    });
}

Upvotes: 1

Views: 204

Answers (1)

Bernd Elkemann
Bernd Elkemann

Reputation: 23550

On the client has to be the button with the actionperformed that calls a method on the server, that makes changes to the database and then returns the result to the client?

Yes, but it doesn't call the method directly (you are not expected to use RMI). Your teacher wants you to make a program that allows multiple clients to work on the database through java sockets (instead of through say odbc).

If you have never written a client-server program in Java before read these tutorials:

http://download.oracle.com/javase/tutorial/networking/sockets/

This involves many changes to your code unfortunately, e.g. org.hibernate.Transaction tx = session.beginTransaction(); has to be on the server while IdFurnComboBox.getSelectedItem() has to be on the client. As a rule of thumb you can place everything that accesses the DB in the server-program and everything that works with the user (the UI) in the client.

The server has to have a listening socket to which the client(s) then connect. The clients send commands to the server: items that are selected in the GUI, the server then updates the DB.

Upvotes: 2

Related Questions