Ciao,

è da poco che mi diletto in java.

Sto vedendo questo codice ... ma diverse parti mi sono oscure..


codice:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package netsend;

/**
 *
 * @author fabio
 */

   
   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.sql.*;
   import java.util.*;
    
   public class NetSend extends JFrame implements ActionListener
       {
       JComboBox list = new JComboBox();
       JTextField message = new JTextField(20);
       JButton send = new JButton("Send");
       JTextField result = new JTextField(25);
       private static Connection cn = null;
       private static Statement st= null;
       private ResultSet r= null;
       
       public NetSend(String title){
        setTitle(title);
        initialise();
        Container c = getContentPane();
        c.setLayout(new FlowLayout(FlowLayout.CENTER));
        c.add(list);
        c.add(message);
        send.addActionListener(this);
        send.setMnemonic(KeyEvent.VK_S);
        c.add(send);
        result.setEditable(false);
        result.setBackground(Color.yellow);
        result.setForeground(Color.red);
        c.add(result);
       }
  
      public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==send){
        String dest = String.valueOf(list.getSelectedItem());
        String msg = message.getText();
        try{
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         cn = DriverManager.getConnection("jdbc:odbc:clients","fabio","fab");
         st=cn.createStatement();
         ResultSet r=st.executeQuery("xp_cmdshell 'net send "+dest+" "+msg+"'");
         result.setText("Message Sent");
        }catch(Exception e){result.setText("Error in Sending Message");}
       }
      }
  
      public void initialise(){
       try
       {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        cn = DriverManager.getConnection("jdbc:odbc:clients","fabio","fab");
        st=cn.createStatement();
        r=st.executeQuery("xp_cmdshell 'net view'");
        String temp = new String();
        int i = 1;
        ArrayList machNames = new ArrayList();
        while(r.next())
        {
         machNames.add(r.getString("output"));
        }
        for(int x=3;x<machNames.size()-3;x++){
         temp = String.valueOf(machNames.get(x));
         list.addItem(temp.substring(2,temp.length()));
        }
        result.setText("Type your message and press ALT and S");
       }
       catch(Exception e){result.setText("Not able to retrieve the hosts");}
      }
     }

1. Ho capito che "public NetSend" si occupa di costruire la finestra...

2. Posso intendere che "send.addActionListener(this);" è la chiamata a "public void actionPerformed(ActionEvent ae)" ??

3. Comprendo che le istruzioni
codice:
         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
         cn = DriverManager.getConnection("jdbc:odbc:clients","fabio","fab");
         st=cn.createStatement();
servono per caricare, aprire e avviare il collegamento al database.

MA

4. Ho il buio totale su quello che succede qua:
"ResultSet r=st.executeQuery("xp_cmdshell 'net send "+dest+" "+msg+"'");"
Io mi aspetto una Query, come dice il nome... una query sul database..
Invece si tratta dell'esecuzione di un codice CMD di windows... che è corretto si faccia... ma che non capisco nella fattezza di quella istruzione..


POI

5. Ho il buio totale su quel che è la funzione "public void initialise()".
Comprendo che DEVE esserci una funzione che si occupi di caricare i dati sul combobox.. ma non vedo la chiamata da nessuna parte.. e perchè poi si chiama "initialise()" e non "initialiZe()" ? è solo un fatto di sintassi... o si tratta di una parola chiave di java?

6. Anche qui io mi aspetto l'esecuzione di una query... dopo le istruzioni:
codice:
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        cn = DriverManager.getConnection("jdbc:odbc:clients","fabio","fab");
        st=cn.createStatement();
perchè "r=st.executeQuery("xp_cmdshell 'net view'");" non contiene un istruzione della forma "SELECT * etcetc..." e mette i dati dentro sul combobox??



Un aiutino per comprendere questi buchi neri....