Ejemplo n.º 1
0
  public List<Corso> findCorsiByMatricolaStudente(String matricola) {
    Connection conn = null;

    try {
      InitialContext cxt = new InitialContext();
      DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/PrenotazioniDB");
      conn = ds.getConnection();

    } catch (NamingException e) {
      System.out.println("Driver non trovato" + e);
      e.printStackTrace();

    } catch (SQLException e) {
      System.out.println("Connessione non trovata" + e);
      e.printStackTrace();
    }
    List<Corso> ritorno = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    Corso corso = new Corso();

    String sql =
        "SELECT materia FROM corsi c, prenotazioni p, studenti s WHERE  AND=?"; // COMPLETARE LA
                                                                                // QUERY

    try {
      ritorno = new Vector<Corso>();
      ps = conn.prepareStatement(sql);
      ps.setString(1, matricola);

      rs = ps.executeQuery();

      while (rs.next()) {
        corso.setIdCorso(rs.getString("id"));
        corso.setMateria(rs.getString("materia"));
      }
    } catch (Exception e) {
      System.out.println("Errore " + e.getMessage());
      e.printStackTrace();
    } finally {
      try {
        ps.close();
      } catch (Exception e) {
        System.out.println("imposibile chiudere il PreparedStatement ");
        e.printStackTrace();
      }
      try {
        conn.close();
      } catch (Exception e) {
        System.out.println("impossibile chiudere la Connection ");
        e.printStackTrace();
      }
    }
    return ritorno;
  }
Ejemplo n.º 2
0
  public boolean insertPrenotazione(Studente studente, Corso corso) {

    Connection conn = null;

    try {
      InitialContext cxt = new InitialContext();
      DataSource ds = (DataSource) cxt.lookup("java:/comp/env/jdbc/PrenotazioniDB");
      conn = ds.getConnection();

    } catch (NamingException e) {
      System.out.println("Driver non trovato" + e);
      e.printStackTrace();

    } catch (SQLException e) {
      System.out.println("Connessione non trovata" + e);
      e.printStackTrace();
    }

    PreparedStatement ps = null;
    boolean ret = false;

    String sql = "INSERT INTO prenotazioni (cod_studente, cod_corso) values (?,?)";

    try {
      ps = conn.prepareStatement(sql);
      ps.setString(1, studente.getMatricola());
      ps.setString(2, corso.getIdCorso());

      int ritorno = ps.executeUpdate();

      if (ritorno > 0) {
        ret = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        ps.close();
      } catch (Exception e) {
        System.out.println("imposibile chiudere il PreparedStatement ");
        e.printStackTrace();
      }
      try {
        conn.close();
      } catch (Exception e) {
        System.out.println("impossibile chiudere la Connection ");
        e.printStackTrace();
      }
    }
    return ret;
  }