home *** CD-ROM | disk | FTP | other *** search
/ ftp.disi.unige.it / 2015-02-11.ftp.disi.unige.it.tar / ftp.disi.unige.it / pub / .person / MesitiM / teach / sol9JDBC2.java < prev    next >
Text File  |  2003-05-26  |  3KB  |  95 lines

  1. // Cognome e Nome: Cognome, Nome 
  2. // Numero di matricola: 
  3. // Data di Nascita: 
  4. // login su SQL server:
  5. // Password su SQL server:
  6.  
  7. import java.sql.*; 
  8. import java.io.*; 
  9.  
  10. class grN2
  11.   
  12. // subname deve essere sostituito con l'identificatore ODBC creato
  13.  
  14.  static String Con_URL = "jdbc:odbc:subname";
  15.    
  16.  public static void main (String args []) 
  17.  { 
  18.      try{
  19.  
  20.         Connection con;
  21.  
  22.     // caricamento driver
  23.  
  24.         Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver");
  25.     
  26.     // connessione: N deve essere sostituito con il numero del vostro gruppo
  27.     
  28.         con =DriverManager.getConnection(Con_URL,"login","password");
  29.  
  30.     // inserire chiamate per creare lo statement
  31.  
  32.     Statement st = con.createStatement();
  33.  
  34.     // inserire chiamate per eseguire lo statement
  35.  
  36.    String query = "select cognome, nome, avg(saldo) from persone, contoCorrente, Filiali where codiceFiscale = titolare and filiale = codice and citta = '" + args[0] + "' group by cognome, nome, filiale";
  37.   
  38.     // query precedente indentata
  39.     // select cognome, nome, avg(saldo) 
  40.     // from persone, contoCorrente, Filiali 
  41.     // where codiceFiscale = titolare and filiale = codice and 
  42.     //       citta = 'parametro' 
  43.     // group by cognome, nome, filiale
  44.  
  45.  
  46.     System.out.println(query);
  47.  
  48.     ResultSet rs = st.executeQuery(query);
  49.  
  50.     // analisi risultato con cursore
  51.  
  52.     while (rs.next())
  53.         {
  54.             System.out.println("Cognome: " + rs.getString(1) + " Nome: " + 
  55.                        rs.getString(2) + " saldo medio: " + rs.getFloat(3));            
  56.         }
  57.  
  58.  
  59.    // inserire chiamate per eseguire lo statement di modifica e cancellazione
  60.  
  61.     st.executeUpdate("update contoCorrente set saldo = saldo * .03 where filiale in (select codice from Filiali where citta = '" + args[0] + "')");    
  62.  
  63.     // query precedente indentata
  64.     // update contoCorrente 
  65.     // set saldo = saldo * .03 
  66.     // where filiale in (select codice 
  67.     //                   from Filiali 
  68.     //                   where citta = 'parametro') 
  69.  
  70.     st.executeUpdate("delete contoCorrente where saldo < 0");
  71.  
  72.     // chiusura connessione
  73.     
  74.         con.close();
  75.  
  76.       } 
  77.     catch(java.lang.ClassNotFoundException e) {
  78.             System.err.print("ClassNotFoundException: "); 
  79.             System.err.println(e.getMessage());
  80.         }
  81.     catch (SQLException e) {
  82.          while( e!=null){ 
  83.             System.out.println("SQLState: " + e.getSQLState());
  84.             System.out.println("    Code: " + e.getErrorCode());
  85.             System.out.println(" Message: " + e.getMessage());
  86.             e = e.getNextException();
  87.                 }
  88.                 }
  89.     
  90.  
  91.  
  92.    } 
  93. }
  94.