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 >
Wrap
Text File
|
2001-03-29
|
4KB
|
149 lines
import java.sql.*;
import java.util.*;
/**
* Fornisce l'interfaccia al database.
* @author Mirko Manea
*/
/**
* Osservazione
*
* @author Barbara Catania
*
* Si noti che la connessione al DBMS viene realizzata dagli stessi metodi che eseguono le query
* (extractStudente, extractStudenti).
* Questo puo' essere ragionevole solo se si assume che tali metodi vengano utilizzati una sola volta
* dall'applicazione (ipotesi non molto realistica in generale).
* PER ESERCIZIO: migliorare l'interfaccia al database, prevedendo un metodo che realizzi la connessione.
* Analizzare inoltre la gestione degli statement preparati e valutare se e come l'interfaccia potrebbe essere ottimizzata
* sotto l'ipotesi che l'applicazioni possa chiamare piu' volte i metodi extractStudente ed extractStudenti
* ese
*/
public class DBMS {
// parametri di connessione
String url = "jdbc:postgresql://arena.sci.univr.it/esercitazioni";
String user = "db01";
String passwd = "";
private String getStudenteSql =
"SELECT * FROM db01studenti WHERE matricola = ?";
private String getStudentiSql =
"SELECT * FROM db01studenti";
/**
* Costruttore
*/
public DBMS()
throws DBMSException {
try {
Class.forName("postgresql.Driver");
} catch (ClassNotFoundException cnfe) {
throw new DBMSException(cnfe.getMessage());
}
}
/******************************************************************
* metodi makeXXXBean
*/
/**
* Popola il bean per uno studente
*/
private StudenteBean makeStudenteBean(ResultSet rs)
throws DBMSException {
try {
StudenteBean studente = new StudenteBean();
studente.setMatricola(rs.getString("matricola"));
studente.setCognome(rs.getString("congnome"));
studente.setNome(rs.getString("nome"));
return studente;
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
/********************************************************************
* metodi extractXXX
*/
/**
* Restituisce lo StudenteBean associato al parametro matricola
*/
public StudenteBean extractStudente(String matricola)
throws UnknownKeyDBMSException, DBMSException {
ResultSet rs = null;
Connection con = null;
PreparedStatement pst = null;
try {
con = DriverManager.getConnection(url,user,passwd);
pst = con.prepareStatement(getStudenteSql);
pst.clearParameters();
pst.setString(1, matricola);
rs = pst.executeQuery();
if (rs.next())
return makeStudenteBean(rs);
else
throw new UnknownKeyDBMSException("Studente matricola=" + matricola + " non trovato!");
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
finally {
try {
con.close();
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
}
/**
* Restituisce un vettore di StudenteBean contenente TUTTI gli
* studenti
*/
public Vector extractStudenti()
throws DBMSException {
Vector lista = new Vector();
ResultSet rs = null;
Connection con = null;
Statement st = null;
try {
con = DriverManager.getConnection(url,user,passwd);
st = con.createStatement();
rs = st.executeQuery(getStudentiSql);
while (rs.next())
lista.add(makeStudenteBean(rs));
return lista;
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
finally {
try {
con.close();
}
catch (SQLException e) {
throw new DBMSException(e.getMessage());
}
}
}
}