This commit is contained in:
Paul Köster 2023-03-08 09:07:35 +01:00
parent 2997a7cd0f
commit 7ce4820475
6 changed files with 103 additions and 32 deletions

51
src/Datail.java Normal file
View File

@ -0,0 +1,51 @@
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.sql.*;
public class Datail {
public static ResultSet spieler(int verein, Connection con) {
try {
Statement s = con.createStatement();
ResultSet r = s.executeQuery("SELECT * FROM Spieler WHERE Vereins_ID = " + verein);
return r;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public static DefaultTableModel detailmodel(int verein, String url) {
String[][] temp = {{""}};
DefaultTableModel t = new DefaultTableModel();
System.out.println(verein);
try (Connection conn = DriverManager.getConnection(url, "root", "")) {
ResultSet r = spieler(verein, conn);
ResultSetMetaData 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);
}
return t;
}
}

View File

@ -2,7 +2,6 @@ import javax.swing.*;
import javax.swing.event.*; import javax.swing.event.*;
import javax.swing.table.DefaultTableModel; import javax.swing.table.DefaultTableModel;
import java.awt.*; import java.awt.*;
import java.awt.Menu;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.sql.*; import java.sql.*;
@ -35,9 +34,14 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
JMenu adm = null; JMenu adm = null;
JMenuItem i1, i2, i3, i4, i5, i6, i7; JMenuItem i1, i2, i3, i4, i5, i6, i7;
JTable detail;
JScrollPane dsc;
int vid=1;
public Einzelansicht(String ta) { public Einzelansicht(String ta) {
this.setSize(500, 400); this.setSize(600, 800);
this.setResizable(false); this.setResizable(false);
this.setLayout(null); this.setLayout(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setDefaultCloseOperation(EXIT_ON_CLOSE);
@ -101,6 +105,7 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
sql = "Select * From " + tab; sql = "Select * From " + tab;
r.first(); r.first();
einfügen(r); einfügen(r);
detailtab(vid);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -140,7 +145,7 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
this.add(a); this.add(a);
einf = new JButton("Einfügen"); einf = new JButton("Einfügen");
einf.setBounds(150, 280, 100, 50); einf.setBounds(470, 50, 100, 50);
einf.setForeground(Color.BLACK); einf.setForeground(Color.BLACK);
einf.setBackground(Color.white); einf.setBackground(Color.white);
einf.setFont(font); einf.setFont(font);
@ -150,7 +155,7 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
del = new JButton("Löschen"); del = new JButton("Löschen");
del.setBounds(300, 280, 100, 50); del.setBounds(470, 120, 100, 50);
del.setForeground(Color.BLACK); del.setForeground(Color.BLACK);
del.setBackground(Color.white); del.setBackground(Color.white);
del.setFont(font); del.setFont(font);
@ -169,6 +174,10 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
t.addTableModelListener(this); t.addTableModelListener(this);
} }
public static void einfügen(ResultSet r) { public static void einfügen(ResultSet r) {
@ -233,6 +242,8 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
r = s.executeQuery(sql); r = s.executeQuery(sql);
r.first(); r.first();
einfügen(r); einfügen(r);
vid=r.getInt("V_ID");
detailtab(vid);
sq = ""; sq = "";
sql = ""; sql = "";
@ -290,6 +301,8 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
try { try {
if (r.next()) { if (r.next()) {
einfügen(r); einfügen(r);
vid=r.getInt("V_ID");
detailtab(vid);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
@ -299,16 +312,17 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
try { try {
if (r.previous()) { if (r.previous()) {
einfügen(r); einfügen(r);
vid=r.getInt("V_ID");
detailtab(vid);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
break; break;
case "einf": case "einf":
Hinzufügen d = new Hinzufügen(r, tab); Hinzufügen d = new Hinzufügen(r, tab,this,true);
d.setVisible(true); d.setVisible(true);
this.setFocusable(false);
this.setFocusableWindowState(false);
break; break;
case "änd": case "änd":
break; break;
@ -343,4 +357,12 @@ public class Einzelansicht extends JFrame implements TableModelListener, ActionL
} }
} }
public void detailtab(int vid){
detail = new JTable(Datail.detailmodel(vid,url));
dsc = new JScrollPane(detail);
dsc.setBounds(20, 250, 550, 400);
dsc.getHorizontalScrollBar();
this.add(dsc);
}
} }

View File

@ -4,27 +4,24 @@ import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.sql.*; import java.sql.*;
public class Hinzufügen extends JFrame implements ActionListener { public class Hinzufügen extends Dialog implements ActionListener {
JFrame frame;
JButton a = null; JButton a = null;
ResultSetMetaData rm = null; ResultSetMetaData rm = null;
JButton einf = null; JButton einf = null;
JTable table = null;
DefaultTableModel t = null;
String url = "jdbc:mariadb://127.0.0.1:3306/bundesliga"; String url = "jdbc:mariadb://127.0.0.1:3306/bundesliga";
String spalten = ""; String spalten = "";
String sname = ""; String sname = "";
Font font = new Font("Arial",Font.PLAIN,12); Font font = new Font("Arial", Font.PLAIN, 12);
public Hinzufügen(ResultSet r, String tab, JFrame owner,boolean modal) {
super(owner,modal);
public Hinzufügen(ResultSet r, String tab) {
this.setSize(500, 600); this.setSize(500, 600);
this.setLayout(null); this.setLayout(null);
owner.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
a = new JButton("Zurück"); a = new JButton("Zurück");
a.setBounds(50, 30, 80, 50); a.setBounds(50, 30, 80, 50);
@ -64,17 +61,6 @@ public class Hinzufügen extends JFrame implements ActionListener {
einf.addActionListener(e -> { einf.addActionListener(e -> {
for (int i = 1; i < z; i++) { for (int i = 1; i < z; i++) {
try { try {
System.out.println(b[i].getText());
if (b[i].getText().length() == 0) {
int response = JOptionPane.showConfirmDialog(null, "Soll der Wert für " + rm.getColumnName(i + 1) + " 'NULL' übernommen werden?", "Bestätigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(response);
if (response == 0) {
//null
} else {
break;
}
}
if (i == z - 1) { if (i == z - 1) {
spalten = spalten + "'" + b[i].getText() + "'"; spalten = spalten + "'" + b[i].getText() + "'";
sname = sname + rm.getColumnName(i + 1); sname = sname + rm.getColumnName(i + 1);
@ -82,6 +68,18 @@ public class Hinzufügen extends JFrame implements ActionListener {
spalten = spalten + "'" + b[i].getText() + "',"; spalten = spalten + "'" + b[i].getText() + "',";
sname = sname + rm.getColumnName(i + 1) + ","; sname = sname + rm.getColumnName(i + 1) + ",";
} }
System.out.println(b[i].getText());
if (b[i].getText().length() == 0) {
int response = JOptionPane.showConfirmDialog(null, "Soll der Wert für " + rm.getColumnName(i + 1) + " 'NULL' übernommen werden?", "Bestätigen", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
System.out.println(response);
if (response == 0) {
spalten= spalten+" Null";
} else {
break;
}
}
} catch (Exception ex) { } catch (Exception ex) {
@ -91,13 +89,13 @@ public class Hinzufügen extends JFrame implements ActionListener {
System.out.println(sname); System.out.println(sname);
System.out.println(spalten); System.out.println(spalten);
String sql = "Insert into " + tab + "(" + sname + ") Values (" + spalten + ")"; String sql = "Insert into " + tab + "(" + sname + ") Values (" + spalten + ")";
System.out.println(sql);
Connection conn = null; Connection conn = null;
try { try {
conn = DriverManager.getConnection(url, "root", ""); conn = DriverManager.getConnection(url, "root", "");
Statement s = conn.createStatement(); Statement s = conn.createStatement();
ResultSet a = s.executeQuery(sql); ResultSet a = s.executeQuery(sql);
this.dispose(); this.dispose();
Tabellen.einfügen(tab);
} catch (SQLException ex) { } catch (SQLException ex) {
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }

View File

@ -57,7 +57,7 @@ public class Tabellen extends JFrame implements TableModelListener {
einf.setBackground(Color.white); einf.setBackground(Color.white);
einf.setFont(font); einf.setFont(font);
einf.addActionListener(e -> { einf.addActionListener(e -> {
Hinzufügen d = new Hinzufügen(r, tab); Hinzufügen d = new Hinzufügen(r, tab,this,true);
d.setVisible(true); d.setVisible(true);
this.setFocusable(false); this.setFocusable(false);