diff --git a/MariaDB/mariadb-java-client-3.1.0.jar b/MariaDB/mariadb-java-client-3.1.0.jar new file mode 100644 index 0000000..4bf78a8 Binary files /dev/null and b/MariaDB/mariadb-java-client-3.1.0.jar differ diff --git a/out/production/IDB-Hue-Projekt/Hinzufügen.class b/out/production/IDB-Hue-Projekt/Hinzufügen.class index ed7d49b..76ae37b 100644 Binary files a/out/production/IDB-Hue-Projekt/Hinzufügen.class and b/out/production/IDB-Hue-Projekt/Hinzufügen.class differ diff --git a/out/production/IDB-Hue-Projekt/Löschen.class b/out/production/IDB-Hue-Projekt/Löschen.class index 70f8c03..74efc0f 100644 Binary files a/out/production/IDB-Hue-Projekt/Löschen.class and b/out/production/IDB-Hue-Projekt/Löschen.class differ diff --git a/out/production/IDB-Hue-Projekt/Tabellen.class b/out/production/IDB-Hue-Projekt/Tabellen.class index 7ae7c1b..0a99733 100644 Binary files a/out/production/IDB-Hue-Projekt/Tabellen.class and b/out/production/IDB-Hue-Projekt/Tabellen.class differ diff --git a/src/Hinzufügen.java b/src/Hinzufügen.java index 6b636f4..c89eaa7 100644 --- a/src/Hinzufügen.java +++ b/src/Hinzufügen.java @@ -23,9 +23,6 @@ public class Hinzufügen extends JFrame implements ActionListener { this.setLayout(null); - String[][] temp = {{""}}; - t = new DefaultTableModel(); - table = new JTable(t); a = new JButton("Zurück"); a.setBounds(50, 30, 80, 50); @@ -70,12 +67,12 @@ public class Hinzufügen extends JFrame implements ActionListener { } } - if (i == z -1) { + if (i == z - 1) { spalten = spalten + "'" + b[i].getText() + "'"; - sname =sname+rm.getColumnName(i+1); + sname = sname + rm.getColumnName(i + 1); } else { spalten = spalten + "'" + b[i].getText() + "',"; - sname =sname+rm.getColumnName(i+1)+","; + sname = sname + rm.getColumnName(i + 1) + ","; } @@ -85,30 +82,23 @@ public class Hinzufügen extends JFrame implements ActionListener { } System.out.println(sname); System.out.println(spalten); - String sql = "Insert into " + tab + "("+sname+") Values (" + spalten + ")"; + String sql = "Insert into " + tab + "(" + sname + ") Values (" + spalten + ")"; Connection conn = null; try { conn = DriverManager.getConnection(url, "root", ""); Statement s = conn.createStatement(); ResultSet a = s.executeQuery(sql); this.dispose(); - // Tabellen.this.remove(table); Tabellen.einfügen(tab); - Tabellen.table.repaint(); } catch (SQLException ex) { throw new RuntimeException(ex); } - - - }); this.add(einf); } catch (SQLException e) { throw new RuntimeException(e); } - - } @Override diff --git a/src/Tabellen.java b/src/Tabellen.java index 0ef8419..e71a014 100644 --- a/src/Tabellen.java +++ b/src/Tabellen.java @@ -1,18 +1,19 @@ import javax.swing.*; +import javax.swing.event.TableModelEvent; +import javax.swing.event.TableModelListener; import javax.swing.table.DefaultTableModel; +import javax.swing.table.JTableHeader; import java.awt.event.*; import java.sql.*; -public class Tabellen extends JFrame implements ActionListener { +public class Tabellen extends JFrame implements TableModelListener { JScrollPane sc = null; JButton a = null; - - JButton änd = null; JButton einf = null; JButton del = null; static ResultSetMetaData rm = null; - static JTable table = null; - static DefaultTableModel t = null; + static DefaultTableModel t = new DefaultTableModel(); + static JTable table =new JTable(t); static ResultSet r = null; static String url = "jdbc:mariadb://127.0.0.1:3306/bundesliga"; @@ -24,6 +25,7 @@ public class Tabellen extends JFrame implements ActionListener { einfügen(tab); + sc = new JScrollPane(table); sc.setBounds(50, 100, 400, 350); sc.getHorizontalScrollBar(); @@ -43,17 +45,11 @@ public class Tabellen extends JFrame implements ActionListener { einf.addActionListener(e -> { Hinzufügen d = new Hinzufügen(r, tab); d.setVisible(true); - { - } + this.setFocusable(false); + }); this.add(einf); - änd = new JButton("Ändern"); - änd.setBounds(170, 480, 100, 50); - änd.addActionListener(e -> { - - }); - this.add(änd); del = new JButton("Löschen"); del.setBounds(290, 480, 100, 50); @@ -63,12 +59,44 @@ public class Tabellen extends JFrame implements ActionListener { }); this.add(del); + + t.addTableModelListener(e->{ + int zeile =table.getSelectedRow(); + int spalte = table.getSelectedColumn(); + if(spalte>0 && zeile >0){ + int response = JOptionPane.showConfirmDialog(null, "Wollen Sie dein Eintrag in Zeile " + (zeile+1) +", Spalte "+(spalte +1)+ " ändern?", "Bestätigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); + if (response == 0) { + try (Connection conn = DriverManager.getConnection(url, "root", "")) { + Statement s = conn.createStatement(); + System.out.println(t.getColumnName(spalte)+"\n"+rm.getColumnTypeName(spalte+1)); + if(rm.getColumnTypeName(spalte+1).equalsIgnoreCase("Varchar")){ + ResultSet r = s.executeQuery("Update " + tab + " set " + t.getColumnName(spalte) + "= '" + t.getValueAt(zeile, spalte).toString() + "' WHERE " + t.getColumnName(0) + " = " + t.getValueAt(zeile, 0)); + + } else if (rm.getColumnTypeName(spalte+1).equalsIgnoreCase("Date")) { + ResultSet r = s.executeQuery("Update " + tab + " set " + t.getColumnName(spalte) + "= '" + t.getValueAt(zeile, spalte).toString() + "' WHERE " + t.getColumnName(0) + " = " + t.getValueAt(zeile, 0)); + + }else { + ResultSet r = s.executeQuery("Update " + tab + " set " + t.getColumnName(spalte) + "= " + t.getValueAt(zeile, spalte).toString() + " WHERE " + t.getColumnName(0) + " = " + t.getValueAt(zeile, 0)); + + } + t.removeTableModelListener(this::tableChanged); + } catch (SQLException f) { + throw new RuntimeException(f); + } + t.fireTableDataChanged(); + } else if (response == 1) { + einfügen(tab); + t.fireTableDataChanged(); + } + + } + + }); } public static void einfügen(String tab) { String[][] temp = {{""}}; - t = new DefaultTableModel(); - table = new JTable(t); + try (Connection conn = DriverManager.getConnection(url, "root", "")) { Statement s = conn.createStatement(); @@ -91,8 +119,7 @@ public class Tabellen extends JFrame implements ActionListener { } t.addRow(rows); - t.fireTableRowsInserted(0, table.getRowCount()); - table.repaint(); + t.fireTableDataChanged(); } @@ -102,9 +129,11 @@ public class Tabellen extends JFrame implements ActionListener { } - @Override - public void actionPerformed(ActionEvent e) { + public void tableChanged(TableModelEvent e) { + System.out.println(table.getSelectedRow()+"" +table.getSelectedColumn()); + + } } diff --git a/src/Ändern.java b/src/Ändern.java new file mode 100644 index 0000000..119878b --- /dev/null +++ b/src/Ändern.java @@ -0,0 +1,2 @@ +public class Ändern { +}