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 / CataniaB / teach-act / DB3 / servlet-bean-DBMS / DBMS.java next >
Text File  |  2001-03-29  |  4KB  |  149 lines

  1. import java.sql.*;
  2. import java.util.*;
  3.  
  4. /**
  5.  * Fornisce l'interfaccia al database.
  6.  * @author Mirko Manea
  7. */
  8.  
  9.  
  10. /**
  11.  * Osservazione
  12.  *
  13.  * @author Barbara Catania
  14.  * 
  15.  * Si noti che la connessione al DBMS viene realizzata dagli stessi metodi che eseguono le query 
  16.  * (extractStudente, extractStudenti).
  17.  * Questo puo' essere ragionevole solo se si assume che tali metodi vengano utilizzati una sola volta 
  18.  * dall'applicazione (ipotesi non molto realistica in generale).
  19.  * PER ESERCIZIO: migliorare l'interfaccia al database, prevedendo un metodo che realizzi la connessione.
  20.  * Analizzare inoltre la gestione degli statement preparati e valutare se e come l'interfaccia potrebbe essere ottimizzata
  21.  * sotto l'ipotesi che l'applicazioni possa chiamare piu' volte i metodi extractStudente ed extractStudenti 
  22.  * ese
  23.  */
  24.  
  25.  
  26.  
  27.  
  28. public class DBMS {
  29.  
  30.     // parametri di connessione
  31.     String url = "jdbc:postgresql://arena.sci.univr.it/esercitazioni";
  32.     String user = "db01";
  33.     String passwd = "";
  34.  
  35.     private String getStudenteSql = 
  36.     "SELECT * FROM db01studenti WHERE matricola = ?";
  37.  
  38.     private String getStudentiSql = 
  39.     "SELECT * FROM db01studenti";
  40.  
  41.     /**
  42.      * Costruttore
  43.      */
  44.     public DBMS() 
  45.         throws DBMSException {
  46.  
  47.         try {
  48.                 Class.forName("postgresql.Driver");
  49.         } catch (ClassNotFoundException cnfe) {
  50.         throw new DBMSException(cnfe.getMessage());
  51.         }
  52.     }
  53.  
  54.     /******************************************************************
  55.      * metodi makeXXXBean
  56.      */
  57.  
  58.     /**
  59.      * Popola il bean per uno studente
  60.      */
  61.     private StudenteBean makeStudenteBean(ResultSet rs)
  62.         throws DBMSException {
  63.         try {
  64.             StudenteBean studente = new StudenteBean();
  65.  
  66.             studente.setMatricola(rs.getString("matricola"));
  67.             studente.setCognome(rs.getString("congnome"));
  68.             studente.setNome(rs.getString("nome"));
  69.            
  70.             return studente;
  71.         }
  72.         catch (SQLException e) {
  73.             throw new DBMSException(e.getMessage());
  74.         }
  75.     }
  76.  
  77.     /********************************************************************
  78.      * metodi extractXXX
  79.      */
  80.  
  81.     /**
  82.      * Restituisce lo StudenteBean associato al parametro matricola
  83.      */
  84.     public StudenteBean extractStudente(String matricola)
  85.         throws UnknownKeyDBMSException, DBMSException {
  86.  
  87.     ResultSet rs = null;
  88.     Connection con = null;
  89.     PreparedStatement pst = null;
  90.         try {
  91.             con = DriverManager.getConnection(url,user,passwd);
  92.  
  93.             pst = con.prepareStatement(getStudenteSql);
  94.         pst.clearParameters();
  95.         pst.setString(1, matricola);
  96.         rs = pst.executeQuery();
  97.         
  98.             if (rs.next())
  99.                 return makeStudenteBean(rs);
  100.             else
  101.                 throw new UnknownKeyDBMSException("Studente matricola=" + matricola + " non trovato!");
  102.         }
  103.         catch (SQLException e) {
  104.             throw new DBMSException(e.getMessage());
  105.         }
  106.       finally {
  107.         try {
  108.             con.close();
  109.         }
  110.             catch (SQLException e) {
  111.                 throw new DBMSException(e.getMessage());
  112.             }
  113.     }
  114.     }
  115.  
  116.     /**
  117.      * Restituisce un vettore di StudenteBean contenente TUTTI gli
  118.      * studenti
  119.      */
  120.     public Vector extractStudenti()
  121.         throws DBMSException {
  122.  
  123.     Vector lista = new Vector();
  124.     ResultSet rs = null;
  125.     Connection con = null;
  126.     Statement st = null;
  127.         try {
  128.             con = DriverManager.getConnection(url,user,passwd);
  129.             st = con.createStatement();
  130.         rs = st.executeQuery(getStudentiSql);
  131.         while (rs.next())
  132.                 lista.add(makeStudenteBean(rs));
  133.         return lista;
  134.         }
  135.         catch (SQLException e) {
  136.             throw new DBMSException(e.getMessage());
  137.         }
  138.       finally {
  139.         try {
  140.             con.close();
  141.         }
  142.             catch (SQLException e) {
  143.                 throw new DBMSException(e.getMessage());
  144.             }
  145.     }
  146.     }
  147.  
  148. }
  149.