Hey, I'm new to this forum so please be considerate if I did not pay attention to anything.
I'm writing a program for my dad. The program should store maintenance contracts in a database and be controllable via a GUI. I have the GUI ready and the database is created. Now I'm trying to show the entries of the database in a JTextArea - but I fail miserably. Have already found out that I probably need a "SwingWorker", but even after video tutorials I have not the slightest idea how it works.
I have 3 classes: Gui, Window and DBConnect
Have already tried around two hours - accordingly, the code now looks synonymous.
I try to call the data in the DBConnect class, but the TextArea is in the Window Class. Sit there today so long tuned, I do not even remember if that is even solvable.
the essential code of my DBConnect class
public static void addContract() {
try {
name = Window.getEnteredName();
address = Window.getEnteredAddress();
number = Window.getEnteredNumber();
email = Window.getEnteredEmail();
lastMaintenance = Window.getEnteredLast();
nextMaintenance = Window.getEnteredNext();
note = Window.getEnteredNote();
PreparedStatement posted = connection.prepareStatement("INSERT INTO contracts.persons (Name, Adresse, Telefonnummer, email, Letzte, Kommende, Hinweis) VALUES('"+name+"','"+address+"','"+number+"','"+email+"','"+lastMaintenance+"','"+nextMaintenance+"','"+note+"')");
posted.executeUpdate();
} catch(Exception ex) {
System.out.println(ex);
}
finally {
JOptionPane.showMessageDialog(null, "erolgreich hinzugefügt.");
}
}
public static void getData() {
SwingWorker<String, Void> worker = new SwingWorker<String, Void>() {
@Override
protected String doInBackground() throws Exception {
String query = "select * from contracts.persons";
resultset = statement.executeQuery(query);
while(resultset.next()) {
name = resultset.getString("name");
address = resultset.getString("Adresse");
number = resultset.getString("Telefonnummer");
email = resultset.getString("email");
lastMaintenance = resultset.getString("Letzte");
nextMaintenance = resultset.getString("Kommende");
note = resultset.getString("Hinweis");
return text=("Name: "+name+"| Adresse: "+address+"| Telefonnummer: "+number+"| E-Mail: "+email+"| Letzte Wartung: "+lastMaintenance+"| Nächste Wartung: "+nextMaintenance+"| Notiz: "+note);
}
return text=("Name: "+name+"| Adresse: "+address+"| Telefonnummer: "+number+"| E-Mail: "+email+"| Letzte Wartung: "+lastMaintenance+"| Nächste Wartung: "+nextMaintenance+"| Notiz: "+note);
}
@Override
protected void process(List<String> data){
}
};
worker.execute();
}
I hope that somehow visible is what I try to do.
My Window Class I think is not really relevant at the moment, I have not the slightest to stand in there regarding the TextArea display.
If anyone has tips or can show me the mistakes that would be extremely friendly.
MfG rogue