|
| 1 | +package gui; |
| 2 | + |
| 3 | +import java.awt.EventQueue; |
| 4 | +import java.awt.Toolkit; |
| 5 | + |
| 6 | +import javax.swing.JDialog; |
| 7 | +import javax.swing.GroupLayout; |
| 8 | +import javax.swing.GroupLayout.Alignment; |
| 9 | +import javax.swing.JLabel; |
| 10 | +import javax.swing.JOptionPane; |
| 11 | +import javax.swing.JTextField; |
| 12 | +import javax.swing.LayoutStyle.ComponentPlacement; |
| 13 | + |
| 14 | +import fachklasse.DBManager; |
| 15 | + |
| 16 | +import javax.swing.JScrollPane; |
| 17 | +import javax.swing.JTextArea; |
| 18 | +import javax.swing.JComboBox; |
| 19 | +import javax.swing.JButton; |
| 20 | +import java.awt.event.ActionListener; |
| 21 | +import java.sql.ResultSet; |
| 22 | +import java.sql.SQLException; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.awt.event.ActionEvent; |
| 25 | +import java.awt.Color; |
| 26 | + |
| 27 | +public class TerminBearbeiten extends JDialog { |
| 28 | + private JTextField textFieldDatum; |
| 29 | + private JTextField textFieldUhrzeit; |
| 30 | + |
| 31 | + public TerminBearbeiten(DBManager dbm, String terminNr) { |
| 32 | + setTitle("Termin bearbeiten"); |
| 33 | + setBounds(100, 100, 216, 357); |
| 34 | + setModal(true); |
| 35 | + setIconImage(Toolkit.getDefaultToolkit().getImage(AnmeldungGui.class.getResource("/img/icon.png"))); |
| 36 | + |
| 37 | + JLabel lblNewLabel = new JLabel("Datum"); |
| 38 | + |
| 39 | + textFieldDatum = new JTextField(); |
| 40 | + textFieldDatum.setColumns(10); |
| 41 | + |
| 42 | + JLabel lblUhrzeit = new JLabel("Uhrzeit"); |
| 43 | + |
| 44 | + textFieldUhrzeit = new JTextField(); |
| 45 | + textFieldUhrzeit.setColumns(10); |
| 46 | + |
| 47 | + JLabel lblBemerkung = new JLabel("Bemerkung"); |
| 48 | + |
| 49 | + JScrollPane scrollPane = new JScrollPane(); |
| 50 | + |
| 51 | + JLabel lblKunde = new JLabel("Kunde"); |
| 52 | + |
| 53 | + JComboBox comboBox = new JComboBox(); |
| 54 | + |
| 55 | + JButton btnErstellen = new JButton("Termin aktualisieren"); |
| 56 | + |
| 57 | + JButton btnLoeschen = new JButton("Termin Löschen"); |
| 58 | + btnLoeschen.addActionListener(new ActionListener() { |
| 59 | + public void actionPerformed(ActionEvent e) { |
| 60 | + int dialogButton = JOptionPane.YES_NO_OPTION; |
| 61 | + int dialogResult = JOptionPane.showConfirmDialog (null, "Sind Sie sicher, dass Sie diesen Termin Löschen möchten?", "Warnung" , dialogButton, JOptionPane.WARNING_MESSAGE); |
| 62 | + if(dialogResult == JOptionPane.YES_OPTION){ |
| 63 | + dbm.startConnect("crm"); |
| 64 | + try { |
| 65 | + dbm.getStatement().executeUpdate("delete FROM crm.termine where termineNr = "+terminNr+";" ); |
| 66 | + |
| 67 | + JOptionPane.showMessageDialog(null, "Löschung war erfolgreich", "Info", JOptionPane.INFORMATION_MESSAGE); |
| 68 | + dispose(); |
| 69 | + } catch (SQLException e1) { |
| 70 | + JOptionPane.showMessageDialog(null, e1, "Fehler", JOptionPane.ERROR_MESSAGE); |
| 71 | + e1.printStackTrace(); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + dbm.closeConnection(); |
| 76 | + } |
| 77 | + }); |
| 78 | + btnLoeschen.setBackground(new Color(204, 51, 51)); |
| 79 | + btnLoeschen.setForeground(new Color(204, 51, 51)); |
| 80 | + |
| 81 | + GroupLayout groupLayout = new GroupLayout(getContentPane()); |
| 82 | + groupLayout.setHorizontalGroup( |
| 83 | + groupLayout.createParallelGroup(Alignment.LEADING) |
| 84 | + .addGroup(groupLayout.createSequentialGroup() |
| 85 | + .addContainerGap() |
| 86 | + .addGroup(groupLayout.createParallelGroup(Alignment.LEADING) |
| 87 | + .addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE) |
| 88 | + .addComponent(textFieldDatum, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE) |
| 89 | + .addComponent(lblNewLabel) |
| 90 | + .addComponent(lblUhrzeit) |
| 91 | + .addComponent(textFieldUhrzeit, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE) |
| 92 | + .addComponent(lblBemerkung) |
| 93 | + .addComponent(lblKunde) |
| 94 | + .addComponent(comboBox, 0, 180, Short.MAX_VALUE) |
| 95 | + .addComponent(btnErstellen, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE) |
| 96 | + .addComponent(btnLoeschen, GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)) |
| 97 | + .addContainerGap()) |
| 98 | + ); |
| 99 | + groupLayout.setVerticalGroup( |
| 100 | + groupLayout.createParallelGroup(Alignment.LEADING) |
| 101 | + .addGroup(groupLayout.createSequentialGroup() |
| 102 | + .addContainerGap() |
| 103 | + .addComponent(lblNewLabel) |
| 104 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 105 | + .addComponent(textFieldDatum, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) |
| 106 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 107 | + .addComponent(lblUhrzeit) |
| 108 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 109 | + .addComponent(textFieldUhrzeit, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) |
| 110 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 111 | + .addComponent(lblBemerkung) |
| 112 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 113 | + .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE) |
| 114 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 115 | + .addComponent(lblKunde) |
| 116 | + .addPreferredGap(ComponentPlacement.UNRELATED) |
| 117 | + .addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) |
| 118 | + .addPreferredGap(ComponentPlacement.UNRELATED) |
| 119 | + .addComponent(btnErstellen) |
| 120 | + .addPreferredGap(ComponentPlacement.RELATED) |
| 121 | + .addComponent(btnLoeschen) |
| 122 | + .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) |
| 123 | + ); |
| 124 | + |
| 125 | + JTextArea textArea = new JTextArea(); |
| 126 | + scrollPane.setViewportView(textArea); |
| 127 | + getContentPane().setLayout(groupLayout); |
| 128 | + |
| 129 | + |
| 130 | + ArrayList<String> kundenNr = new ArrayList<>(); |
| 131 | + dbm.startConnect("crm"); |
| 132 | + try { |
| 133 | + ResultSet rs = dbm.getStatement().executeQuery("SELECT kundenNr, vorname, name FROM crm.kunden;"); |
| 134 | + while (rs.next()) { |
| 135 | + String val = rs.getString(2)+ " " +rs.getString(3); |
| 136 | + kundenNr.add(rs.getString(1)); |
| 137 | + comboBox.addItem(val); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + } catch (SQLException e) { |
| 142 | + e.printStackTrace(); |
| 143 | + } |
| 144 | + dbm.closeConnection(); |
| 145 | + |
| 146 | + btnErstellen.addActionListener(new ActionListener() { |
| 147 | + public void actionPerformed(ActionEvent e) { |
| 148 | + dbm.startConnect("crm"); |
| 149 | + try { |
| 150 | + String kundenNrIndex = kundenNr.get(comboBox.getSelectedIndex()).toString(); |
| 151 | + String datum = textFieldDatum.getText().isEmpty()? "0000-00-00":textFieldDatum.getText(); |
| 152 | + String uhrzeit = textFieldUhrzeit.getText().isEmpty()? "00:00":textFieldUhrzeit.getText(); |
| 153 | + |
| 154 | + dbm.getStatement().executeUpdate("update crm.termine set datum ='"+datum+"', uhrzeit ='"+uhrzeit+"', bemerkung ='"+textArea.getText()+"', kundenNr ="+kundenNrIndex+" where termineNr = "+terminNr+";"); |
| 155 | + JOptionPane.showMessageDialog(null, "Termin erfolgreich geändert!"); |
| 156 | + } catch (SQLException e1) { |
| 157 | + JOptionPane.showMessageDialog(null, e1, "Fehler", JOptionPane.ERROR_MESSAGE); |
| 158 | + e1.printStackTrace(); |
| 159 | + } |
| 160 | + dbm.closeConnection(); |
| 161 | + } |
| 162 | + }); |
| 163 | + |
| 164 | + |
| 165 | + dbm.startConnect("crm"); |
| 166 | + try { |
| 167 | + ResultSet rs = dbm.getStatement().executeQuery("SELECT termine.*, kunden.vorname, kunden.name FROM crm.termine, crm.kunden where termine.kundenNr = kunden.kundenNr and termineNr = '"+terminNr+"';"); |
| 168 | + while(rs.next()){ |
| 169 | + textFieldDatum.setText(rs.getString(2)); |
| 170 | + textFieldUhrzeit.setText(rs.getString(3)); |
| 171 | + textArea.setText(rs.getString(4)); |
| 172 | + comboBox.setSelectedItem(rs.getString(6) + " " + rs.getString(7)); |
| 173 | + } |
| 174 | + } catch (SQLException e1) { |
| 175 | + e1.printStackTrace(); |
| 176 | + } |
| 177 | + dbm.closeConnection(); |
| 178 | + } |
| 179 | +} |
0 commit comments