Hallo,
hier mein Erstlingswerk. Es funktioniert (fast) wie gewollt.
Daten werden in eine WebDB geschrieben, gelesen, gelöscht und geändert.
Nur die Tabelle wird nicht aktualisiert. Das Problem habe ich hier im Forum und auch im WWW hunderte mal gefunden und auch fast jede Lösung probiert. Leider hat nicht gefruchtet.
Vielleicht weil ich schon schon in den Grundlage etwas falsch gemacht habe?
Es funktioniert jedenfalls nicht ;(
hier mein Erstlingswerk. Es funktioniert (fast) wie gewollt.
Daten werden in eine WebDB geschrieben, gelesen, gelöscht und geändert.
Nur die Tabelle wird nicht aktualisiert. Das Problem habe ich hier im Forum und auch im WWW hunderte mal gefunden und auch fast jede Lösung probiert. Leider hat nicht gefruchtet.
Vielleicht weil ich schon schon in den Grundlage etwas falsch gemacht habe?
Es funktioniert jedenfalls nicht ;(
Java:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import java.util.Properties;
public class AppWindow extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField textfield_spalte1;
JTextField textfield_spalte2;
JTextField textfield_id;
JButton button_new;
JButton button_delete;
JButton button_refresh;
JTable tabelle;
DefaultTableModel tabelle_id;
DefaultTableModel tabelle_spalte1;
DefaultTableModel tabelle_spalte2;
JScrollPane view;
public AppWindow() throws ClassNotFoundException, SQLException {
this.getContentPane().setLayout(null);
this.initWindow();
this.addWindowListener(new WindowListener() {
public void windowClosed(WindowEvent arg0) {
}
public void windowActivated(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
}
public void initWindow() throws ClassNotFoundException, SQLException
{
// Instanzieren:
textfield_spalte1 = new JTextField();
textfield_spalte2 = new JTextField();
textfield_id = new JTextField();
button_new = new JButton("Neu");
button_delete = new JButton("Löschen");
button_refresh = new JButton("Refresh");
buttonClicked((char) 4);
tabelle = new JTable(tabelle_id);
view = new JScrollPane(tabelle);
button_new.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
buttonClicked((char) 1);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button_delete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
buttonClicked((char) 3);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
button_refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
try {
buttonClicked((char) 4);
} catch (ClassNotFoundException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// Positionen festlegen
textfield_id.setBounds(5,5,50,25);
textfield_spalte1.setBounds(5,35,400,25);
textfield_spalte2.setBounds(5,65,400,25);
button_new.setBounds(5,95,100,30);
button_delete.setBounds(120,95,100,30);
button_refresh.setBounds(235,95,100,30);
view.setBounds(5,140,400,400);
// Elemente dem Fenster hinzufügen:
this.getContentPane().add(textfield_id);
this.getContentPane().add(textfield_spalte1);
this.getContentPane().add(textfield_spalte2);
this.getContentPane().add(button_new);
this.getContentPane().add(button_delete);
this.getContentPane().add(button_refresh);
this.getContentPane().add(view);
this.pack();
}
public void buttonClicked(char auswahl) throws SQLException, ClassNotFoundException{
try {
Class.forName("de.root1.jpmdbc.Driver");
for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
System.out.println(e.nextElement().getClass().getName());
}
DriverManager.setLogWriter(new PrintWriter(System.out));
Properties connProperties = new Properties();
connProperties.setProperty("user", "****");
connProperties.setProperty("password", "****");
connProperties.setProperty("host", "localhost");
connProperties.setProperty("port", "3306");
//connProperties.setProperty("charset", "ISO-8859-1"); // UTF-8 is default
//connProperties.setProperty("charset", "utf8_unicode_ci");
Connection con = DriverManager.getConnection("jdbc:jpmdbc:http://******:80/jpmdbc.php?*****", connProperties);
String spalte1_neu = textfield_spalte1.getText();
String spalte2_neu = textfield_spalte2.getText();
String id = textfield_id.getText();
Statement statement = con.createStatement();
String[][] rowData;
switch(auswahl){
case 1: //new
statement.executeUpdate("insert into `test` (`spalte1` ,`spalte2`) values ('" + spalte1_neu + "', '" + spalte2_neu + "')");
break;
case 2: //update
break;
case 3: //delete
statement.executeUpdate("delete from `test` where `id` = '" + id + "'");
break;
case 4:
ResultSet rs = statement.executeQuery("SELECT * FROM `test`;");
rs.last();
int columns = rs.getRow();
rowData = new String[columns][columns];
rs.first();
int index = 1;
while(rs.next()) {
rowData[index][0] = rs.getString(1);
rowData[index][1] = rs.getString(2);
rowData[index][2] = rs.getString(3);
index++;
}
String spalten[] = { "ID", "Spalte 1", "Spalte 2"};
tabelle_id = new DefaultTableModel(rowData, spalten);
tabelle_spalte1 = new DefaultTableModel(rowData, spalten);
tabelle_spalte2 = new DefaultTableModel(rowData, spalten);
System.out.println("yyyy");
break;
default:
break;
}
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//return null;
}