# Conflicts:
#	out/production/IDB-Hue-Projekt/Hinzufügen.class
#	out/production/IDB-Hue-Projekt/Tabellen.class
This commit is contained in:
PaulK 2023-01-25 15:50:22 +01:00
commit 0303e85db4
6 changed files with 54 additions and 33 deletions

Binary file not shown.

View File

@ -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

View File

@ -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());
}
}

2
src/Ändern.java Normal file
View File

@ -0,0 +1,2 @@
public class Ändern {
}