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-jsp / DBMS.java next >
Text File  |  2001-04-02  |  8KB  |  285 lines

  1. import java.sql.*;
  2. import java.util.*;
  3. import java.text.*;
  4.  
  5. /**
  6.  * Fornisce l'interfaccia al database.
  7.  */
  8. public class DBMS {
  9.  
  10.     // parametri di connessione
  11.     String url = "jdbc:postgresql://arena.sci.univr.it/esercitazioni";
  12.     String user = "db01";
  13.     String passwd = "";
  14.  
  15.     GregorianCalendar calendar = new GregorianCalendar();
  16.  
  17.     private String StudenteSql = 
  18.     "SELECT * FROM db01studenti WHERE matricola = ?";
  19.  
  20.     private String StudentiSql = 
  21.     "SELECT * FROM db01studenti";
  22.  
  23.     private String EsamiStudenteSql = 
  24.     "SELECT E.*, C.denominazione "+
  25.     "FROM db01esami E, db01corsi C "+
  26.     "WHERE E.materia = C.codice AND E.studente = ? "+
  27.     "ORDER BY E.data DESC";
  28.  
  29.     private String insStudenteSql = 
  30.     "INSERT INTO db01studenti "+
  31.     "VALUES (?, ?, ?, ?, ?, ?)";
  32.  
  33.     /**
  34.      * Costruttore
  35.      */
  36.     public DBMS() 
  37.         throws DBMSException {
  38.  
  39.         try {
  40.                 Class.forName("org.postgresql.Driver");
  41.         } catch (ClassNotFoundException cnfe) {
  42.         throw new DBMSException(cnfe.getMessage());
  43.         }
  44.     }
  45.  
  46.     /******************************************************************
  47.      * metodi makeXXXBean
  48.      */
  49.  
  50.     /**
  51.      * Popola il bean per uno studente
  52.      */
  53.     private StudenteBean makeStudenteBean(ResultSet rs)
  54.         throws DBMSException {
  55.         try {
  56.             StudenteBean bean = new StudenteBean();
  57.  
  58.             bean.setMatricola(rs.getString("matricola"));
  59.             bean.setCognome(rs.getString("congnome"));
  60.             bean.setNome(rs.getString("nome"));
  61.             bean.setFacolta(rs.getString("facolta"));
  62.             bean.setDataNascita(rs.getDate("data_nascita"));
  63.  
  64.         bean.setCreditiOttenuti(rs.getInt("crediti_ottenuti"));
  65.         if (rs.wasNull()) bean.setCreditiOttenuti(-1);
  66.  
  67.             return bean;
  68.         }
  69.         catch (SQLException e) {
  70.             throw new DBMSException(e.getMessage());
  71.         }
  72.     }
  73.  
  74.     /**
  75.      * Popola il bean per un esame
  76.      */
  77.     private EsameBean makeEsameBean(ResultSet rs)
  78.         throws DBMSException {
  79.         try {
  80.             EsameBean bean = new EsameBean();
  81.  
  82.             bean.setStudente(rs.getString("studente"));
  83.             bean.setMateria(rs.getInt("materia"));
  84.             bean.setVoto(rs.getInt("voto"));
  85.             bean.setData(rs.getDate("data"));
  86.         if (rs.wasNull()) bean.setData(null);
  87.  
  88.             return bean;
  89.         }
  90.         catch (SQLException e) {
  91.             throw new DBMSException(e.getMessage());
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * Popola il bean per un esame
  97.      * aggiungendo il nome della materia
  98.      */
  99.     private EsameBean makeEsameBeanCompleto(ResultSet rs)
  100.         throws DBMSException {
  101.         try {
  102.             EsameBean bean = makeEsameBean(rs);
  103.  
  104.             bean.setDenominazione(rs.getString("denominazione"));
  105.  
  106.             return bean;
  107.         }
  108.         catch (SQLException e) {
  109.             throw new DBMSException(e.getMessage());
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Popola il bean per un corso
  115.      */
  116.     /*private CorsoBean makeCorsoBean(ResultSet rs)
  117.         throws DBMSException {
  118.         try {
  119.             CorsoBean bean = new CorsoBean();
  120.  
  121.             bean.setCodice(rs.getInt("codice"));
  122.             bean.setFacolta(rs.getString("facolta"));
  123.         if (rs.wasNull()) bean.setFacolta("");
  124.             bean.setDenominazione(rs.getString("denominazione"));
  125.             bean.setCrediti(rs.getInt("crediti"));
  126.             bean.setProfessore(rs.getString("professore"));
  127.  
  128.             return bean;
  129.         }
  130.         catch (SQLException e) {
  131.             throw new DBMSException(e.getMessage());
  132.         }
  133.     }*/
  134.  
  135.     /********************************************************************
  136.      * metodi extractXXX
  137.      */
  138.  
  139.     /**
  140.      * Restituisce lo StudenteBean associato al parametro matricola
  141.      */
  142.     public StudenteBean extractStudente(String matricola)
  143.         throws UnknownKeyDBMSException, DBMSException {
  144.  
  145.     ResultSet rs = null;
  146.     Connection con = null;
  147.     PreparedStatement pst = null;
  148.         try {
  149.             con = DriverManager.getConnection(url,user,passwd);
  150.  
  151.             pst = con.prepareStatement(StudenteSql);
  152.         pst.clearParameters();
  153.         pst.setString(1, matricola);
  154.         rs = pst.executeQuery();
  155.         
  156.             if (rs.next())
  157.                 return makeStudenteBean(rs);
  158.             else
  159.                 throw new UnknownKeyDBMSException("Studente matricola=" + matricola + " non trovato!");
  160.         }
  161.         catch (SQLException e) {
  162.             throw new DBMSException(e.getMessage());
  163.         }
  164.       finally {
  165.         try {
  166.             con.close();
  167.         }
  168.             catch (SQLException e) {
  169.                 throw new DBMSException(e.getMessage());
  170.             }
  171.     }
  172.     }
  173.  
  174.     /**
  175.      * Restituisce un vettore di StudenteBean contenente TUTTI gli
  176.      * studenti
  177.      */
  178.     public Vector extractStudenti()
  179.         throws DBMSException {
  180.  
  181.     Vector lista = new Vector();
  182.     ResultSet rs = null;
  183.     Connection con = null;
  184.     Statement st = null;
  185.         try {
  186.             con = DriverManager.getConnection(url,user,passwd);
  187.             st = con.createStatement();
  188.         rs = st.executeQuery(StudentiSql);
  189.         while (rs.next())
  190.                 lista.add(makeStudenteBean(rs));
  191.         return lista;
  192.         }
  193.         catch (SQLException e) {
  194.             throw new DBMSException(e.getMessage());
  195.         }
  196.       finally {
  197.         try {
  198.             con.close();
  199.         }
  200.             catch (SQLException e) {
  201.                 throw new DBMSException(e.getMessage());
  202.             }
  203.     }
  204.     }
  205.  
  206.     /**
  207.      * Restituisce un vettore di EsamiBean contenente TUTTI gli
  208.      * esami superati da uno studente
  209.      */
  210.     public Vector extractEsamiStudente(String matricola)
  211.         throws DBMSException {
  212.  
  213.     Vector lista = new Vector();
  214.     ResultSet rs = null;
  215.     Connection con = null;
  216.     PreparedStatement pst = null;
  217.         try {
  218.             con = DriverManager.getConnection(url,user,passwd);
  219.             pst = con.prepareStatement(EsamiStudenteSql);
  220.         pst.clearParameters();
  221.         pst.setString(1, matricola);
  222.         rs = pst.executeQuery();
  223.         
  224.         while (rs.next())
  225.                 lista.add(makeEsameBeanCompleto(rs));
  226.         return lista;
  227.         }
  228.         catch (SQLException e) {
  229.             throw new DBMSException(e.getMessage());
  230.         }
  231.       finally {
  232.         try {
  233.             con.close();
  234.         }
  235.             catch (SQLException e) {
  236.                 throw new DBMSException(e.getMessage());
  237.             }
  238.     }
  239.     }
  240.  
  241.     /********************************************************************
  242.      * metodi insXXX
  243.      */
  244.  
  245.     /**
  246.      * Inserisce uno studente
  247.      */
  248.     public void insStudente(StudenteBean bean)
  249.         throws UnknownKeyDBMSException, DBMSException {
  250.  
  251.     ResultSet rs = null;
  252.     Connection con = null;
  253.     PreparedStatement pst = null;
  254.         try {
  255.             con = DriverManager.getConnection(url,user,passwd);
  256.  
  257.             pst = con.prepareStatement(insStudenteSql);
  258.         pst.clearParameters();
  259.         pst.setString(1, bean.getMatricola());
  260.         pst.setString(2, bean.getCognome());
  261.         pst.setString(3, bean.getNome());
  262.         pst.setString(4, bean.getFacolta());
  263.  
  264.         calendar.setTime(bean.getDataNascita());
  265.         java.sql.Date data = new java.sql.Date(calendar.getTime().getTime());
  266.         pst.setDate(5, data);
  267.  
  268.         pst.setInt(6, bean.getCreditiOttenuti());
  269.         pst.executeUpdate();
  270.         }
  271.         catch (SQLException e) {
  272.             throw new DBMSException(e.getMessage());
  273.         }
  274.       finally {
  275.         try {
  276.             con.close();
  277.         }
  278.             catch (SQLException e) {
  279.                 throw new DBMSException(e.getMessage());
  280.             }
  281.     }
  282.     }
  283.  
  284. }
  285.