Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Soluzione #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 77 additions & 14 deletions Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/CorsoDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,45 +29,108 @@ public List<Corso> getTuttiICorsi() {

while (rs.next()) {

String codins = rs.getString("codins");
int numeroCrediti = rs.getInt("crediti");
String nome = rs.getString("nome");
int periodoDidattico = rs.getInt("pd");
//String codins = rs.getString("codins");
//int numeroCrediti = rs.getInt("crediti");
//String nome = rs.getString("nome");
//int periodoDidattico = rs.getInt("pd");

System.out.println(codins + " " + numeroCrediti + " " + nome + " " + periodoDidattico);
//System.out.println(codins + " " + numeroCrediti + " " + nome + " " + periodoDidattico);

// Crea un nuovo JAVA Bean Corso
// Aggiungi il nuovo oggetto Corso alla lista corsi
Corso s = new Corso(rs.getString("codins"), rs.getInt("crediti"), rs.getString("nome"), rs.getInt("pd"));
corsi.add(s);
}

return corsi;

conn.close();
} catch (SQLException e) {
// e.printStackTrace();
throw new RuntimeException("Errore Db", e);
}
return corsi;
}

/*
* Dato un codice insegnamento, ottengo il corso
*/
public void getCorso(Corso corso) {
// TODO
final String sql = "SELECT * FROM corso where codins=?";

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, corso.getCodins());

ResultSet rs = st.executeQuery();

if (rs.next()) {
corso.setCrediti(rs.getInt("crediti"));
corso.setNome(rs.getString("nome"));
corso.setPd(rs.getInt("pd"));
}

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db", e);
}
}

/*
* Ottengo tutti gli studenti iscritti al Corso
*/
public void getStudentiIscrittiAlCorso(Corso corso) {
// TODO
final String sql = "SELECT * FROM iscrizione, studente WHERE iscrizione.matricola=studente.matricola AND codins=?";

List<Studente> studentiIscrittiAlCorso = new ArrayList<Studente>();

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, corso.getCodins());

ResultSet rs = st.executeQuery();

while (rs.next()) {

studentiIscrittiAlCorso.add(new Studente(rs.getInt("matricola"), rs.getString("nome"), rs.getString("cognome"), rs.getString("cds")));
}

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db", e);
}

return studentiIscrittiAlCorso;
}

/*
* Data una matricola ed il codice insegnamento, iscrivi lo studente al corso.
*/
public boolean inscriviStudenteACorso(Studente studente, Corso corso) {
// TODO
// ritorna true se l'iscrizione e' avvenuta con successo
return false;
String sql = "INSERT IGNORE INTO `iscritticorsi`.`iscrizione` (`matricola`, `codins`) VALUES(?,?)";
boolean returnValue = false;

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setInt(1, studente.getMatricola());
st.setString(2, corso.getCodins());

int res = st.executeUpdate();

if (res == 1)
returnValue = true;

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db", e);
}

return returnValue;
}
}
100 changes: 100 additions & 0 deletions Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/StudenteDAO.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,105 @@
package it.polito.tdp.lab04.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import it.polito.tdp.lab04.model.Corso;
import it.polito.tdp.lab04.model.Studente;

public class StudenteDAO {

/*
* Controllo se uno studente (matricola) è iscritto ad un corso (codins)
*/
public boolean isStudenteIscrittoACorso(Studente studente, Corso corso) {

final String sql = "SELECT * FROM iscrizione where codins=? and matricola=?";
boolean returnValue = false;

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setString(1, corso.getCodins());
st.setInt(2, studente.getMatricola());

ResultSet rs = st.executeQuery();

if (rs.next())
returnValue = true;

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db");
}

return returnValue;
}

/*
* Data una matricola ottengo la lista dei corsi (codins) a cui è iscritto
*/
public List<Corso> getCorsiFromStudente(Studente studente) {

final String sql = "SELECT * FROM iscrizione, corso WHERE iscrizione.codins=corso.codins AND matricola=?";

List<Corso> corsi = new LinkedList<Corso>();

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setInt(1, studente.getMatricola());
ResultSet rs = st.executeQuery();

while (rs.next()) {

Corso corso = new Corso(rs.getString("codins"), rs.getInt("crediti"), rs.getString("nome"),
rs.getInt("pd"));
corsi.add(corso);
}

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db");
}

return corsi;
}

/*
* Data una matricola ottengo lo studente.
*/
public Studente getStudente(int matricola) {

final String sql = "SELECT * FROM studente where matricola=?";
Studente studente = null;

try {
Connection conn = ConnectDB.getConnection();
PreparedStatement st = conn.prepareStatement(sql);
st.setInt(1, matricola);

ResultSet rs = st.executeQuery();

if (rs.next()) {
studente = new Studente(matricola, rs.getString("nome"), rs.getString("cognome"), rs.getString("cds"));
}

conn.close();

} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("Errore Db");
}

return studente;
}

}
10 changes: 8 additions & 2 deletions Lab04_SegreteriaStudenti/src/it/polito/tdp/lab04/DAO/TestDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ public static void main(String[] args) {
* This is a main to check the DB connection
*/

CorsoDAO cdao = new CorsoDAO();
cdao.getTuttiICorsi();
try {
CorsoDAO cdao = new CorsoDAO();
cdao.getTuttiICorsi();
System.out.println("TestDB passed");

} catch (RuntimeException e) {
System.err.println("TestDB failed");
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.polito.tdp.lab04.controller;

import it.polito.tdp.lab04.model.Model;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
Expand All @@ -16,11 +17,8 @@ public void start(Stage primaryStage) {
BorderPane root = (BorderPane) loader.load();

SegreteriaStudentiController controller = loader.getController();

/*
* Create and set the model here!
*/
// controller.setModel();
Model model = new Model();
controller.setModel(model);

Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<BorderPane prefHeight="442.0" prefWidth="561.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="it.polito.tdp.lab04.controller.SegreteriaStudentiController">
<top>
Expand All @@ -11,4 +18,99 @@
<Insets bottom="20.0" top="10.0" />
</BorderPane.margin></Label>
</top>
<center>
<VBox prefHeight="200.0" prefWidth="256.0" BorderPane.alignment="CENTER">
<children>
<HBox prefHeight="47.0" prefWidth="478.0">
<children>
<Label text="Corso :">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Label>
<ComboBox fx:id="comboCorso" prefHeight="25.0" prefWidth="275.0" promptText="Corsi">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</ComboBox>
</children>
<VBox.margin>
<Insets bottom="15.0" left="20.0" right="20.0" />
</VBox.margin>
</HBox>
<HBox prefHeight="100.0" prefWidth="200.0">
<children>
<Button fx:id="btnCercaIscrittiCorso" mnemonicParsing="false" onAction="#doCercaIscrittiCorso" prefHeight="27.0" prefWidth="148.0" text="Cerca iscritti corso" />
</children>
<VBox.margin>
<Insets bottom="20.0" left="20.0" right="20.0" />
</VBox.margin>
</HBox>
<HBox prefHeight="48.0" prefWidth="478.0">
<children>
<Label text="Studente:">
<HBox.margin>
<Insets left="20.0" />
</HBox.margin></Label>
<TextField fx:id="txtMatricola" prefHeight="25.0" prefWidth="75.0">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin></TextField>
<Button id="btnCss1" fx:id="btnCercaNome" mnemonicParsing="false" onAction="#doCercaNome" style="-fx-background-color: green; -fx-text-fill: white;" text="√">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
<font>
<Font name="System Bold" size="12.0" />
</font>
</Button>
<TextField fx:id="txtNome" editable="false" prefHeight="30.0" prefWidth="112.0" promptText="Nome">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</TextField>
<TextField fx:id="txtCognome" editable="false" prefHeight="30.0" prefWidth="149.0" promptText="Cognome">
<HBox.margin>
<Insets left="10.0" />
</HBox.margin>
</TextField>
</children>
</HBox>
<HBox prefHeight="35.0" prefWidth="478.0">
<children>
<Button fx:id="btnCerca" mnemonicParsing="false" onAction="#doCercaCorsi" prefHeight="25.0" prefWidth="97.0" text="Cerca corsi">
<HBox.margin>
<Insets right="330.0" />
</HBox.margin></Button>
<Button fx:id="btnIscrivi" mnemonicParsing="false" onAction="#doIscrivi" prefHeight="25.0" prefWidth="86.0" text="Iscrivi">
<HBox.margin>
<Insets left="10.0" right="10.0" />
</HBox.margin>
</Button>
</children>
<VBox.margin>
<Insets left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</HBox>
<TextArea fx:id="txtResult" editable="false" prefHeight="251.0" prefWidth="478.0">
<padding>
<Insets left="10.0" right="10.0" />
</padding>
<VBox.margin>
<Insets bottom="5.0" left="20.0" right="20.0" top="20.0" />
</VBox.margin>
</TextArea>
</children>
</VBox>
</center>
<bottom>
<Button fx:id="btnReset" mnemonicParsing="false" onAction="#doReset" prefHeight="21.0" prefWidth="80.0" text="Reset" BorderPane.alignment="CENTER_RIGHT">
<padding>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</padding>
<BorderPane.margin>
<Insets bottom="20.0" left="20.0" right="20.0" top="10.0" />
</BorderPane.margin>
</Button>
</bottom>
</BorderPane>
Loading