Sera a tutti, sto seguendo un esercitazione di Basi Di Dati della mia facoltà. Ma non ho ben capito come implementare le seguenti informazioni:
Allora io ho scritto i vari metodi che sarebbero questi.
Ora non riesco a capire come dovrei implementare i possibili valori idcdl null. Il database è composto da due tabelle una corsodilaure(id,nome) ed una seconda studenti:
Creare la tabella Studente (matricola, nome, cognome, idcdl) dove idcdl è una foreign key. Aggiungere il vincolo di integrità referenziale.
Creare il corrispondente DAO StudenteDAO (prendere come esempio l’altro DAO) con i seguenti metodi:
public Studente get(Studente studente)
private Studente recordToStudente(ResultSet rs) throws SQLException
public Vector<Studente> getAll()
public boolean modifica(Studente studente)
public boolean salva(Studente studente)
public boolean elimina(Studente studente)
-Per la gestione del possibile valore null di idcdl, usare il seguente codice:
if (rs.getObject("idcdl")!=null)
studente.setIdcdl(rs.getInt("idcdl"));
-Per la gestione del parametro ? nel caso idcdl (che può essere null) usare il seguente codice:
ps.setNull(XXX, java.sql.Types.INTEGER); //dove XXX è l’ordine del parametro (1, 2, 3, …)
public Studente get(Studente studente)
private Studente recordToStudente(ResultSet rs) throws SQLException
public Vector<Studente> getAll()
public boolean modifica(Studente studente)
public boolean salva(Studente studente)
public boolean elimina(Studente studente)
-Per la gestione del possibile valore null di idcdl, usare il seguente codice:
if (rs.getObject("idcdl")!=null)
studente.setIdcdl(rs.getInt("idcdl"));
-Per la gestione del parametro ? nel caso idcdl (che può essere null) usare il seguente codice:
ps.setNull(XXX, java.sql.Types.INTEGER); //dove XXX è l’ordine del parametro (1, 2, 3, …)
Java:
package it.unirc.bd.beans;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import it.unirc.bd.utils.DBManager;
public class StudenteDAO {
private static Connection conn = null;
public Studente get(Studente studente) {
String query = "SELECT * FROM studente WHERE matricola = ?";
Studente res = null;
PreparedStatement ps;
conn = DBManager.startConnection();
try {
ps = conn.prepareStatement(query);
ps.setInt(1, studente.getMatricola());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
res = new Studente();
res.setMatricola(rs.getInt("matricola"));
res.setNome(rs.getString("nome"));
res.setCognome(rs.getString("cognome"));
res.setIdcdl(rs.getInt("idcdl"));
}
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.closeConnection();
return res;
}
public boolean salva(Studente studente) {
String query = "INSERT INTO Studente VALUES (?, ?,?,?)";
boolean esito = false;
conn = DBManager.startConnection();
try {
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, studente.getMatricola());
ps.setString(2, studente.getNome());
ps.setString(3, studente.getCognome());
ps.setInt(4, studente.getIdcdl().intValue());
int tmp = ps.executeUpdate();
if (tmp == 1)
esito = true;
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.closeConnection();
return esito;
}
public boolean elimina(Studente studente) {
String query = "DELETE FROM studente WHERE matricola = ?";
boolean esito = false;
conn = DBManager.startConnection();
try {
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, studente.getMatricola());
int tmp = ps.executeUpdate();
if (tmp == 1)
esito = true;
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.closeConnection();
return esito;
}
public boolean modifica(Studente studente) {
String query = "UPDATE studente SET nome=?,cognome=?,idcdl=? WHERE matricola=?";
boolean esito = false;
conn = DBManager.startConnection();
try {
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, studente.getNome());
ps.setString(2, studente.getCognome());
ps.setInt(3, studente.getIdcdl());
ps.setInt(4, studente.getMatricola());
int tmp = ps.executeUpdate();
if (tmp == 1)
esito = true;
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.closeConnection();
return esito;
}
protected Studente recordToStudente(ResultSet rs) throws SQLException {
Studente studente = new Studente();
studente.setMatricola(rs.getInt("matricola"));
studente.setNome(rs.getString("nome"));
studente.setCognome(rs.getString("cognome"));
studente.setIdcdl(rs.getInt("idcdl"));
return studente;
}
public Vector<Studente> getAll() {
String query = "SELECT * FROM studente";
Vector<Studente> list = new Vector<Studente>();
PreparedStatement ps;
conn = DBManager.startConnection();
try {
ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
list.add(recordToStudente(rs));
}
} catch (SQLException e) {
e.printStackTrace();
}
DBManager.closeConnection();
return list;
}
Creare la tabella Studente (matricola, nome, cognome, idcdl) dove idcdl è una foreign key. Aggiungere il vincolo di integrità referenziale.