Quando una webapplication utilizza un database per recuperare le informazioni di interesse, tutte le query sono gestite all'interno di un'unica classe Java che, utilizzando un insieme opportuno di componenti Java Data Beans, realizza un'interfaccia tra le servlet e il database.
Assumiamo di chiamare tale classe DBMS.java
. Un possibile
struttura di questa classe è la seguente:
Nota!
import java.sql.*;
import java.util.*;
/**
* Fornisce un'interfaccia al database.
*/
public class DBMS {
// parametri di connessione
String url = "jdbc:postgresql://arena.sci.univr.it/esercitazioni";
//DA AGGIUSTARE!!
String user = "xxxx";
String passwd = "";
//Per rendere la classe leggibile, i testi di TUTTE le query dovrebbero
//essere definiti come oggetti Stringhe privati di classe.
//DA AGGIUSTARE!!
private String getMuseoSql =
"SELECT * FROM museo WHERE nome = ? AND citta = ";
private String getOrarioMuseoSql =
"SELECT * FROM orario WHERE citta = ? AND museo = ? ";
/**
* Costruttore
*/
public DBMS() throws DBMSException {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
log("Driver jdbc non trovato: " + cnfe.getMessage());
throw new DBMSException(cnfe.getMessage());
}
}
/******************************************************************
* metodi makeXXXBean
*/
/**
* Popola il bean per un museo
*/
private MuseoBean makeMuseoBean(ResultSet rs)
throws DBMSException {
try {
MuseoBean museo = new MuseoBean();
museo.setNome(rs.getString("nome"));
museo.setCitta(rs.getString("citta"));
museo.setPrezzoAdulti(rs.getFloat("prezzo_adulti"));
if (rs.wasNull()) museo.setPrezzoAdulti(-1);
//DA COMPLETARE!!!
return museo;
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
/**
* Popola il bean per un orario
*/
private OrarioBean makeOrarioBean(ResultSet rs) {
//DA COMPLETARE!
}
/********************************************************************
* metodi getXXX
*/
/**
* Restituisce il MuseoBean associato ai parametri nome e citta
*/
public MuseoBean getMuseo(String nome, String citta)
throws UnknownKeyDBMSException, DBMSException {
ResultSet rs = null;
Connection con = null;
PreparedStatement pst = null;
try {
con = DriverManager.getConnection(url,user,passwd);
pst = con.prepareStatement(getMuseoSql);
pst.clearParameters();
pst.setString(1, nome);
//DA COMPLETARE!
rs = pst.executeQuery();
if (rs.next())
return makeMuseoBean(rs);
else
throw new UnknownKeyDBMSException("Museo nome=" + nome +
" della cittā= " + citta +
" non trovato!");
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
finally {
try {
con.close();
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
}
/**
* Restituisce un Vector di OrarioBean associato ai parametri nome e citta
* che identificano un museo. Se non ci sono orari definiti per il museo
indicato... allora restituisci un Vector vuoto (CHE NON č NULLO!)
*/
public Vector getOrarioMuseo(String nome, String citta)
throws UnknownKeyDBMSException, DBMSException {
//DA COMPLETARE
ResultSet rs = null;
Connection con = null;
PreparedStatement pst = null;
Vector orario = new Vector();
try {
con = DriverManager.getConnection(url,user,passwd);
pst = con.prepareStatement(getOrarioMuseoSql);
pst.clearParameters();
pst.setString(1, nome);
//DA COMPLETARE!
rs = pst.executeQuery();
while (rs.next())
orario.add(makeOrarioBean(rs));
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
finally {
try {
con.close();
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
}
}
Valida il documento
Copyright © 2002 by Roberto Posenato