2

I have GUI application with JDBC. One thread is for swing gui.

public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                try {
                    runApp();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }

After starting the program displays my data from the database in a JTable.

public List<Category> getCategory() throws SQLException
    {
        List<Category> cat = new ArrayList<Category>();

        Connection conn = Database.getInstance().getConnection();

        System.out.println(conn);

        String sql = "select id, name from kategorie";
        Statement selectStatement =  conn.createStatement();

        ResultSet results = selectStatement.executeQuery(sql);

        while(results.next())
        {
            int id = results.getInt("id");
            String name = results.getString("name");

            Category category = new Category(id, name);
            cat.add(category);
        }

        results.close();
        selectStatement.close();

        return cat;

    }

I wonder how can I add a new thread so that all database operations run in a separate thread, not this thread swing.

28
  • 2
    Yupp, have a look at SwingWorker. I might find an example too for your usecase. Though I need some itme for that :-) Commented Aug 16, 2014 at 10:15
  • 2
    This tutorial is good. Commented Aug 16, 2014 at 10:52
  • 1
    @lukassz: I have updated the code in the previous link, to actually adhere to Swing's single thread rule. Do check it out again, this time it will work without any errors of any sort, just run the JAR file./database.rar Commented Aug 16, 2014 at 12:25
  • 1
    @lukassz: Seems like I am not been able to understand you, on this one. But I did had a looked at the code, but I couldn't find, where exactly you added that to the table. You simply just called getCategory() inside MySQLCategoryDAO class, though you never did added anything to the JTable. Commented Aug 16, 2014 at 17:22
  • 1
    @lukassz: Seems like a bit confusing to me, not been able to get a hold on the code. Just try to make a small thingy, which simply includes, what you wanted to achieve, do not incorporate that straight away in your project, just watch the flow. Simply lend the part of retreiving values from the database to the Database class instead of writing code everywhere. Never worked on MySQL before, so couldn't run it. A small example to understand the flow, will be highly appreciated. Commented Aug 17, 2014 at 4:41

1 Answer 1

2

Have a look at ExecutorService class for example.

ExecutorService exec = Executors.newFixedThreadPool(2);
exec.execute(new Runnable() { 
// Run your database thread

});
exec.shutdown();
Sign up to request clarification or add additional context in comments.

3 Comments

Ok. What do you think about this solution stackoverflow.com/questions/3489543/…
@lukassz: At the end of retrieving values, the JTable must be updated on the Event Dispatcher Thread - EDT. So if you go for SwingWorker that thingy is something that won't give headaches to you, since the methods process()/done() put everythingy on the EDT by default.
@lukassz it better plus take nIcE cOw notes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.