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 / sol9JDBC1.java < prev    next >
Text File  |  2003-05-26  |  2KB  |  80 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 grN1
  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.     System.out.println(query);
  46.  
  47.     ResultSet rs = st.executeQuery(query);
  48.  
  49.     // analisi risultato con cursore
  50.  
  51.     while (rs.next())
  52.         {
  53.             System.out.println("Cognome: " + rs.getString(1) + " Nome: " + 
  54.                                rs.getString(2) + " saldo medio: " + rs.getFloat(3));            
  55.         }
  56.  
  57.     // chiusura connessione
  58.     
  59.         con.close();
  60.  
  61.       } 
  62.     catch(java.lang.ClassNotFoundException e) {
  63.             System.err.print("ClassNotFoundException: "); 
  64.             System.err.println(e.getMessage());
  65.         }
  66.     catch (SQLException e) {
  67.          while( e!=null){ 
  68.             System.out.println("SQLState: " + e.getSQLState());
  69.             System.out.println("    Code: " + e.getErrorCode());
  70.             System.out.println(" Message: " + e.getMessage());
  71.             e = e.getNextException();
  72.                 }
  73.                 }
  74.     
  75.  
  76.  
  77.    } 
  78. }
  79.