Code snippets

comp_math

Mitglied
Java:
package group.msg.gby.persistence;

//import are hidden
...

public class ProtocolDAO {
    private static ProtocolDAO instance;

  private static ProtocolDAO instance;

    /**
     * Returns always the same instance of ProtocolDAO.
     *
     * @return an instance of ProtocolDAO.
     */
    public static ProtocolDAO getInstance() {
        if (null == instance) {
            instance = new ProtocolDAO();
        }
        return instance;
    }

    /**
     * Inserts a new protocol with protocol entries into the database.
     *
     * @param protocol the Protocol object to be inserted.
     * @throws SQLException if a database access error occurs.
     */
    public void createProtocolInDB(Protocol protocol) throws SQLException {

        String sql = "INSERT INTO protocol (protocol_id, meeting_date, start_time) VALUES (?, ?, ?)";

        int id = getNextValueOfSequenceForTableProtocol();

        try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql)) {
            statement.setInt(1, id);
            statement.setDate(2, getValidDate(protocol.getMeetingDate()));
            statement.setTime(3, getValidTime(protocol.getStartTime()));

            statement.executeUpdate();
        }

        String sql2 = "INSERT INTO protocolentry (protocol_entry_id, topic, agenda_item, " +
                "sub_agenda_item, resubmission_date, delete_date, remark, protocol_id)" +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        for (ProtocolEntry pe : protocol.getProtocolEntries()) {
            try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql2)) {
                statement.setInt(1, getNextValueOfSequenceForTableProtocolEntry());
                statement.setString(2, pe.getTopic());
                statement.setInt(3, pe.getItemNumber());
                statement.setInt(4, pe.getSubItemNumber());
                statement.setDate(5, getValidDate(pe.getResubmissionDate()));
                statement.setDate(6, getValidDate(pe.getDeleteDate()));
                statement.setString(7, pe.getRemark());
                statement.setInt(8, id);

                statement.executeUpdate();
            }
        }
    }

    /**
     * Searches for protocols in the database and returns a List of protocols that match the search criteria.
     *
     * @param meetingDate the meeting date to search for.
     * @param resubmissionDate is later than the resubmission date to search for.
     * @param deleteDate is later than the delete date to search for.
     * @param wordTopic is, in the column topic to search for.
     * @param wordRemark is, in the column remark to search for.
     * @return a List of Protocol objects that match the search criteria.
     * @throws SQLException if a database access error occurs.
     */
    public List<Protocol> searchInProtocolsInDBAsList(LocalDate meetingDate, LocalDate resubmissionDate, LocalDate deleteDate,
                                                String wordTopic, String wordRemark) throws SQLException {

        List<Protocol> myList = new ArrayList<>();

        String sql = "SELECT pe.protocol_id, pe.protocol_entry_id FROM protocol p, protocolentry pe " +
                "WHERE pe.protocol_id=p.protocol_id " +
                "AND topic ilike ? " +
                "AND remark ilike ? " +
                "AND (meeting_date = COALESCE(?, meeting_date)) " +
                "AND (COALESCE(resubmission_date,'9999-12-30') < COALESCE(?, resubmission_date + 1, '9999-12-31')) " +
                "AND (COALESCE(delete_date, '9999-12-30') < COALESCE(?, delete_date + 1, '9999-12-31')) " +
                "ORDER BY meeting_date, delete_date DESC";

        try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql)) {

            statement.setString(1,  (null != wordTopic ? ("%" + wordTopic.trim() + "%") : "%%" ));
            statement.setString(2, (null != wordRemark ? ("%" + wordRemark.trim() + "%") : "%%" ));
            statement.setDate(3, getValidDate(meetingDate));
            statement.setDate(4, getValidDate(resubmissionDate));
            statement.setDate(5, getValidDate(deleteDate));

            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                Protocol protocol = getProtocol(rs.getInt(1), false);
                ProtocolEntry protocolEntry = getProtocolEntry(rs.getInt(2));

                boolean found = false;
                for (Protocol p : myList) {
                    if (p.equals(protocol)) {
                        p.addEntry(protocolEntry);
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    myList.add(protocol);
                    protocol.addEntry(protocolEntry);
                }
            }
        }
        return myList;
    }
    
     /**
     * Retrieves all the {@link ProtocolEntry} objects associated with a particular protocol identified by the given
     * {@code protocolID}, sorted by their {@code delete_date} in descending order.
     *
     * @param protocolID the ID of the protocol to retrieve the entries for
     * @return a {@link Set} of {@link ProtocolEntry} objects associated with the protocol
     * @throws SQLException if an error occurs while accessing the database
     */
    public Set<ProtocolEntry> getAllProtocolEntriesOfAProtocol(int protocolID) throws SQLException {
        String sql = "SELECT * FROM protocolentry WHERE protocol_id = ? ORDER BY delete_date DESC";
        Set<ProtocolEntry> result = new HashSet<>();

        try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql)) {
            statement.setInt(1, protocolID);

            ResultSet rs = statement.executeQuery();

            while (rs.next()) {
                ProtocolEntry pe = new ProtocolEntry(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getInt(3),
                        rs.getInt(4),
                        getValidLocalDate(rs.getString(5)),
                        getValidLocalDate(rs.getString(6)),
                        rs.getString(7));
                result.add(pe);
            }
        }
        return result;
    }
    
    /**
     * Updates the protocol entry in the database with the given ProtocolEntry object.
     *
     * @param protocolEntry The ProtocolEntry object containing the updated information.
     * @throws SQLException If there is an error accessing the database.
     */
    public void updateProtocolEntry(ProtocolEntry protocolEntry) throws SQLException {
        String sql = "UPDATE protocolentry SET topic = ?, agenda_item = ?, sub_agenda_item = ?, " +
                "resubmission_date = ?, delete_date = ?, remark = ? WHERE protocol_entry_id = ?";

        try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql)) {
            statement.setString(1, protocolEntry.getTopic());
            statement.setInt(2, protocolEntry.getItemNumber());
            statement.setInt(3, protocolEntry.getSubItemNumber());
            statement.setDate(4, getValidDate(protocolEntry.getResubmissionDate()));
            statement.setDate(5, getValidDate(protocolEntry.getDeleteDate()));
            statement.setString(6, protocolEntry.getRemark());
            statement.setInt(7, protocolEntry.getProtocolEntryID());

            statement.executeUpdate();
        }
    }
    
    /**
     * Deletes a protocol entry from the database with the given protocol entry ID.
     *
     * @param protocolEntryID the ID of the protocol entry to be deleted
     * @throws SQLException if an SQL exception occurs while attempting to delete the protocol entry
     */
    public void deleteProtocolEntryInDB(int protocolEntryID) throws SQLException {
        String sql = "DELETE FROM protocolentry WHERE protocol_entry_id = ?";
        try (PreparedStatement statement = DB_Connection.getStatement().getConnection().prepareStatement(sql)) {
            statement.setInt(1, protocolEntryID);

            statement.executeUpdate();
        }
    }
    
}
 

KonradN

Super-Moderator
Mitarbeiter
Was man da anbringen kann: Das Singleton ist nicht Thread sicher. Wenn man ein Singleton haben will, dann sollte man eine Variante wählen, die auch thread sicher ist. Es gibt diesbezüglich genug Optionen, die auf diversen Seiten beschrieben werden, wobei der beste Weg wohl der Weg über ein Enum ist. Aber man kann natürlich auch die KLassenvariable direkt beim Laden initialisieren oder, wenn die Variable bei der Abfrage null ist, in einem synchronized Block erneut prüfen, ob die Variable noch null ist um diese dann zu erzeugen.

Und in dem Code fehlt der private Konstruktor.
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
G JavaFX Code in neues Projekt übernehmen AWT, Swing, JavaFX & SWT 0
princess_sara1997 .jar Datei führt den Code nicht richtig aus AWT, Swing, JavaFX & SWT 19
S Ich bringe Code mit JavaFX unter Apache NetBeans IDE 12.6 nicht zum laufen. AWT, Swing, JavaFX & SWT 14
U Kann man den Code umschreiben? AWT, Swing, JavaFX & SWT 8
U Code doch nicht austauschbar in 2DGraphics AWT, Swing, JavaFX & SWT 2
K Bekomme (u.a) javafx.fxml.LoadException trotz "korrektem" Code AWT, Swing, JavaFX & SWT 8
M JavaFX kann man da noch was am code vereinfachen design technisch sieht nicht richtig aus... AWT, Swing, JavaFX & SWT 15
S ActionEvent Press Enter per Code auslösen AWT, Swing, JavaFX & SWT 22
T FXML Datei in Java Code einbinden: javafx.fxml.LoadException AWT, Swing, JavaFX & SWT 2
CptK Ordentlicher Code & Panel ab bestimmter Komponentenzahl scrollbar machen AWT, Swing, JavaFX & SWT 12
T Anderen Java Code durch Code kompilieren und Fehler in Label ausgeben AWT, Swing, JavaFX & SWT 5
L Verbindung von Code und JFrame AWT, Swing, JavaFX & SWT 4
M Swing Code funktioniert auf Windows aber nicht Linux... AWT, Swing, JavaFX & SWT 3
T JavaFX Falscher Wert getLayoutBounds? + Tipps zum Code-Aufbau? AWT, Swing, JavaFX & SWT 8
S JavaFX JavaScript Code in WebView/WebEngine AWT, Swing, JavaFX & SWT 0
L JavaFX GUI mit JavaFX. Scene Builder source code? AWT, Swing, JavaFX & SWT 6
V JavaFX Button Controller Fehler, trotz Erfolg in einem anderem Code? AWT, Swing, JavaFX & SWT 7
Ernesto95 JavaFX FXML vs. Java Code AWT, Swing, JavaFX & SWT 3
T Hilfe bei Code AWT, Swing, JavaFX & SWT 2
B Wie erstelle ich eine JavaFX Anwendung von diesem Code? AWT, Swing, JavaFX & SWT 3
D JavaFX Beim Schließen (Rotes X) Code ausführen AWT, Swing, JavaFX & SWT 1
D JavaFX Dynamisch erzeugte Buttons sollen Code ausführen. AWT, Swing, JavaFX & SWT 2
T Tab per Code wechseln? AWT, Swing, JavaFX & SWT 3
C Swing GUI extrem langsam - GUI-Code richtig ausführen AWT, Swing, JavaFX & SWT 1
U Bild innerhalb hatml code anzeigen AWT, Swing, JavaFX & SWT 5
N JOGL-Code != C OpenGL-Code? AWT, Swing, JavaFX & SWT 9
R Source-Code, Javadoc AWT, Swing, JavaFX & SWT 16
S NetBeans GUI Builder - Code-Platzierung AWT, Swing, JavaFX & SWT 3
J Sauberer Gui-Code? AWT, Swing, JavaFX & SWT 22
B Color String Code AWT, Swing, JavaFX & SWT 3
D JFreeChart - aktuelle code beispiele AWT, Swing, JavaFX & SWT 11
T Bei Klick Code ausführen AWT, Swing, JavaFX & SWT 17
J JMenuBar ist geaddet, aber nicht im Frame. Wo ist der Fehler? (inkl. Code) AWT, Swing, JavaFX & SWT 2
B Swing Laden von serialisierten Objekten (invalid type code AC)) AWT, Swing, JavaFX & SWT 3
S Swing Scrollpanes: wie kann ich durch Java-Code an den "Anfang" scrollen AWT, Swing, JavaFX & SWT 8
J prinzipielles verständnis für Oberfläche/Code-trennung AWT, Swing, JavaFX & SWT 5
A Applet Applet aus Code neu starten AWT, Swing, JavaFX & SWT 2
C 2D-Grafik BufferedImage laden, Frage zum Code AWT, Swing, JavaFX & SWT 2
M JFileChooser setCurrentDirectory() - Verzeichnis relativ zum Code/binary AWT, Swing, JavaFX & SWT 14
L ListSelectionListener durch Code auslösen AWT, Swing, JavaFX & SWT 3
S 2D-Grafik Warum funktioniert dieser Code ? (GUI) AWT, Swing, JavaFX & SWT 9
M ActionListener mit code!!! auslösen AWT, Swing, JavaFX & SWT 3
hdi SWT Shell: Auslagerung von Code verändert Verhalten AWT, Swing, JavaFX & SWT 6
data89 Code-completion-window erzeugen - wie? AWT, Swing, JavaFX & SWT 2
C JTree per code selektierte Node wird nicht farbig hinterlegt AWT, Swing, JavaFX & SWT 2
K Swing GUI code tutorials? AWT, Swing, JavaFX & SWT 5
P Swing [gelöst/erledigt] Gleicher Code läuft unterschiedlich unter Linux und Windows AWT, Swing, JavaFX & SWT 5
G Code Blöcke AWT, Swing, JavaFX & SWT 6
S swt table mit checkboxen: wie nachträglich im code setzen? AWT, Swing, JavaFX & SWT 6
PAX Iconified JFrame per Code wieder anzeigen lassen (deiconify) AWT, Swing, JavaFX & SWT 2
O Source Code für "javax.swing.JLabel" ? AWT, Swing, JavaFX & SWT 2
T Öffnen von SWT-Dialogboxen durch HTML-Link in HTML-Code AWT, Swing, JavaFX & SWT 4
Z habe code, habe fehler, habe keine lösung AWT, Swing, JavaFX & SWT 7
P HTMLDocument in einem JTextPane. Anhängen von HTML-Code AWT, Swing, JavaFX & SWT 2
R Sun tutorial code von jcombobox mit image im combobox fehlt! AWT, Swing, JavaFX & SWT 3
S Unterschiedliche Optik trotz gleicher Look and Feel Code? AWT, Swing, JavaFX & SWT 11
M Code umbauen ? AWT, Swing, JavaFX & SWT 7
C GUI passt sich nicht dem Code an! AWT, Swing, JavaFX & SWT 4
7 Latex/MathML Code in JTextPane AWT, Swing, JavaFX & SWT 2
B Code Architektur verbessern. AWT, Swing, JavaFX & SWT 4
R Syntax Highlighting von Java-Code AWT, Swing, JavaFX & SWT 2
O Swing Event / in GUI oder im Code ausgelöst ? AWT, Swing, JavaFX & SWT 2
J eventhandling / Problemmeldung Bitte schaut euch den Code an AWT, Swing, JavaFX & SWT 2
M Code Schloss AWT, Swing, JavaFX & SWT 11
C wysiwyg Editor zum Einbinden in meinem Code AWT, Swing, JavaFX & SWT 14
m@nu JComboBox & ItemListener: per Code 1. Item wählen AWT, Swing, JavaFX & SWT 2
M Mit JTextPane erstelltes Dokument als HTML-Code ausgeben AWT, Swing, JavaFX & SWT 4
H Code nach dem schließen eines Fensters ausführen? AWT, Swing, JavaFX & SWT 3
M Was halter iht von diesem Code? AWT, Swing, JavaFX & SWT 7

Ähnliche Java Themen

Neue Themen


Oben