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

happygeek commented: spammer - your footprints are all over Google... -4

Recommended Answers

All 2 Replies

Break this down into easy steps.
Your destination is to have the database query running in a different thread from the Swing thread, because the whole gui will freeze during any processing that you do on its thread. That’s why you can use SwingWorker, for example.
But first, just ignore the threading and get the database query and GUI display working.
Then look at moving the query to separate thread. SwingWorker can be hard to understand, so maybe just use a simple Runnable in a Thread that you create, and hand the results back to a Gui update method via SwingUtilities exécuteLater

Ps please excuse typos, I’m on an iPad

jamescherrill - thank you for answer!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.