This commit is contained in:
Paul Köster 2023-01-31 17:12:07 +01:00
parent bab3b9b231
commit ed2b089584
7 changed files with 307 additions and 23 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

270
src/Einzelansicht.java Normal file
View File

@ -0,0 +1,270 @@
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class Einzelansicht extends JFrame implements TableModelListener {
JScrollPane sc = null;
JButton a = null;
JButton einf = null;
JButton del = null;
static JButton zur = null;
static JButton vor = null;
static ResultSetMetaData rm = 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";
JTextField s = null;
String tab = null;
JLabel such = null;
Font font = new Font("Arial", Font.PLAIN, 12);
String sql = null;
String sq = "";
static int i = 0;
public Einzelansicht(String ta) {
this.setSize(500, 400);
this.setResizable(false);
this.setLayout(null);
tab = ta;
try (Connection conn = DriverManager.getConnection(url, "root", "")) {
Statement s = conn.createStatement();
r = s.executeQuery("Select * FROM " + tab);
rm = r.getMetaData();
sql = "Select * From " + tab + " Where " + rm.getColumnName(1) + " =" + 1;
einfügen(tab, sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
vor = new JButton("Weiter");
vor.setBounds(300, 150, 90, 50);
vor.setFont(font);
vor.setBackground(Color.white);
vor.setForeground(Color.black);
vor.addActionListener(e -> {
i++;
try {
sql = "Select * From " + tab + " Where " + rm.getColumnName(1) + " =" + i;
einfügen(tab, sql);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
});
this.add(vor);
zur = new JButton("Letzte");
zur.setBounds(150, 150, 90, 50);
zur.setFont(font);
zur.setBackground(Color.white);
zur.setForeground(Color.black);
zur.addActionListener(e -> {
if(i>1){
i--;
}
try {
sql = "Select * From " + tab + " Where " + rm.getColumnName(1) + " =" + i;
einfügen(tab, sql);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
});
this.add(zur);
sc = new JScrollPane(Einzelansicht.table);
sc.setBounds(50, 100, 400, 50);
sc.getHorizontalScrollBar();
this.add(sc);
a = new JButton("Zurück");
a.setBounds(50, 30, 70, 50);
a.setForeground(Color.BLACK);
a.setBackground(Color.white);
a.setFont(font);
a.addActionListener(e -> {
this.dispose();
Menu m = new Menu();
m.setVisible(true);
});
this.add(a);
einf = new JButton("Einfügen");
einf.setBounds(150, 280, 100, 50);
einf.setForeground(Color.BLACK);
einf.setBackground(Color.white);
einf.setFont(font);
einf.addActionListener(e -> {
Hinzufügen d = new Hinzufügen(r, tab);
d.setVisible(true);
this.setFocusable(false);
});
this.add(einf);
del = new JButton("Löschen");
del.setBounds(300, 280, 100, 50);
del.setForeground(Color.BLACK);
del.setBackground(Color.white);
del.setFont(font);
del.addActionListener(e -> {
System.out.println(t.getValueAt(0,0));
String wh = t.getValueAt(0, 0).toString();
int response = JOptionPane.showConfirmDialog(null, "Wollen Sie den Eintrag löschen?", "Bestätigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == 0) {
try (Connection conn = DriverManager.getConnection(url, "root", "")) {
Statement s = conn.createStatement();
ResultSet r = s.executeQuery("DELETE From " + tab + " WHERE " + t.getColumnName(0) + "=" + wh);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
Tabellen.einfügen(tab);
} else {
}
Einzelansicht.table.clearSelection();
table.clearSelection();
t.removeRow(0);
t.fireTableDataChanged();
});
this.add(del);
such = new JLabel("Suchen");
such.setBounds(300, 10, 50, 20);
this.add(such);
s = new JTextField();
s.setBounds(300, 30, 150, 40);
this.add(s);
suchen();
t.addTableModelListener(this);
}
public static void einfügen(String tab, String sql) {
String[][] temp = {{""}};
try (Connection conn = DriverManager.getConnection(url, "root", "")) {
Statement s = conn.createStatement();
r = s.executeQuery(sql);
rm = r.getMetaData();
rm.getColumnCount();
String column[] = new String[rm.getColumnCount()];
for (int i = 1; i <= rm.getColumnCount(); i++) {
column[i - 1] = rm.getColumnName(i);
t.addColumn(column[i - 1]);
}
t.setDataVector(temp, column);
t.removeRow(0);
Object rows[] = new Object[rm.getColumnCount()];
while (r.next()) {
for (int i = 1; i <= rm.getColumnCount(); i++) {
rows[i - 1] = r.getString(i);
}
}
t.addRow(rows);
t.fireTableDataChanged();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void suchen() {
s.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
search(s.getText());
}
@Override
public void removeUpdate(DocumentEvent e) {
search(s.getText());
}
@Override
public void changedUpdate(DocumentEvent e) {
search(s.getText());
}
public void search(String st) {
if (st.length() == 0) {
} else {
try {
for (int j = 1; j <= rm.getColumnCount(); j++) {
System.out.println(sq);
if (j == rm.getColumnCount()) {
sq = sq + rm.getColumnName(j) + " LIKE '%" + st + "%'";
} else {
sq = sq + rm.getColumnName(j) + " LIKE '%" + st + "%' or ";
}
}
sql="Select * FROm "+tab+ " Where "+sq;
System.out.println(sql);
einfügen(tab,sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
});
}
@Override
public void tableChanged(TableModelEvent e) {
int zeile = table.getSelectedRow();
int spalte = table.getSelectedColumn();
if (spalte > 0 && zeile > 0) {
int response = 0;
try {
response = JOptionPane.showConfirmDialog(null, "Wollen Sie dein Eintrag in Zeile " + (zeile + 1) + ", Spalte :" + (rm.getColumnName(spalte + 1)) + " ändern?", "Bestätigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
} catch (SQLException ex) {
throw new RuntimeException(ex);
}
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));
}
table.clearSelection();
} catch (SQLException f) {
throw new RuntimeException(f);
}
t.fireTableDataChanged();
} else {
table.clearSelection();
}
}
}
}

View File

@ -6,37 +6,52 @@ import java.sql.*;
public class Menu extends JFrame implements ActionListener { public class Menu extends JFrame implements ActionListener {
JScrollPane sc = null; JScrollPane sc = null;
JButton a = null; JButton a = null;
JButton v = null;
JLabel i = null;
JList list = null; JList list = null;
ImageIcon img =new ImageIcon("Bundesliga.jpg");
String url = "jdbc:mariadb://127.0.0.1:3306/bundesliga"; String url = "jdbc:mariadb://127.0.0.1:3306/bundesliga";
String table = null;
public static void main(String[] args) { public static void main(String[] args) {
Menu m = new Menu(); Menu m = new Menu();
m.setVisible(true); m.setVisible(true);
} }
public Menu() { public Menu() {
this.setSize(500, 600); this.setSize(500, 600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(null); this.setLayout(null);
this.setBackground(Color.WHITE); this.setBackground(Color.WHITE);
this.setIconImage(img.getImage());
JLabel lbl = new JLabel("");
ImageIcon img = new ImageIcon(new ImageIcon(Menu.class.getResource("/image/Bundesiga.jfif")).getImage());
lbl.setIcon(img);
lbl.setBounds(150, 50, 200,202);
this.getContentPane().add(lbl);
this.add(lbl);
v = new JButton("Einzelansicht");
v.setBounds(200, 420, 100, 60);
v.setFont(new Font("Arial", Font.PLAIN, 11));
v.setForeground(Color.black);
v.setBackground(Color.white);
v.addActionListener(e -> {
table = list.getSelectedValue().toString();
Einzelansicht a = new Einzelansicht(table);
a.setVisible(true);
this.dispose();
});
a = new JButton("Anzeigen"); a = new JButton("Anzeigen");
a.setBounds(200, 200, 90, 60); a.setBounds(200, 350, 90, 60);
a.setFont(new Font("Arial",Font.PLAIN,12)); a.setFont(new Font("Arial", Font.PLAIN, 12));
a.setForeground(Color.black); a.setForeground(Color.black);
a.setBackground(Color.white); a.setBackground(Color.white);
a.addActionListener(e -> { a.addActionListener(e -> {
try (Connection conn = DriverManager.getConnection(url, "root", "")){ try (Connection conn = DriverManager.getConnection(url, "root", "")) {
if (list.isSelectionEmpty()){ if (list.isSelectionEmpty()) {
JOptionPane.showMessageDialog(null,"Wählen sie eine Tabelle aus!"); JOptionPane.showMessageDialog(null, "Wählen sie eine Tabelle aus!");
}else { } else {
String table = list.getSelectedValue().toString(); table = list.getSelectedValue().toString();
Tabellen t = new Tabellen(url, table); Tabellen t = new Tabellen(url, table);
t.setVisible(true); t.setVisible(true);
t.setTitle(list.getSelectedValue().toString()); t.setTitle(list.getSelectedValue().toString());
@ -52,17 +67,19 @@ public class Menu extends JFrame implements ActionListener {
DefaultListModel liste = new DefaultListModel(); DefaultListModel liste = new DefaultListModel();
list = new JList(liste); list = new JList(liste);
list.addListSelectionListener(e->{ list.addListSelectionListener(e -> {
if (list.getSelectedValue().toString().equalsIgnoreCase("Verein")||list.getSelectedValue().toString().equalsIgnoreCase("Spieler")){ if (list.getSelectedValue().toString().equalsIgnoreCase("Verein") || list.getSelectedValue().toString().equalsIgnoreCase("test")) {
System.out.println(list.getSelectedValue()); this.add(v);
}
} else
this.remove(v);
}); });
sc = new JScrollPane(); sc = new JScrollPane();
sc.setViewportView(list); sc.setViewportView(list);
sc.setBounds(30, 150, 150, 200); sc.setBounds(30, 300, 150, 200);
this.add(sc); this.add(sc);
try (Connection conn = DriverManager.getConnection(url, "root", "")){ try (Connection conn = DriverManager.getConnection(url, "root", "")) {
Statement s = conn.createStatement(); Statement s = conn.createStatement();
ResultSet r = s.executeQuery("Show Tables"); ResultSet r = s.executeQuery("Show Tables");
while (r.next()) { while (r.next()) {
@ -77,9 +94,6 @@ public class Menu extends JFrame implements ActionListener {
public void paint(Graphics g) { public void paint(Graphics g) {
super.paint(g); super.paint(g);
i = new JLabel(img);
i.setBounds(150, 280, 200, 100);
this.add(i);
repaint(); repaint();
} }

BIN
src/image/Bundesiga.jfif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.7 KiB

BIN
src/image/Download.jfif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB