0

I'm new to java and I need help with displaying a joined table/query in jtable.

First, I have done displaying data from 1 table which is:

  • Select data from 1 table
  • insert the result to its entity and insert each one of it to a List
  • return the list to view and insert row to jtable

I am using a DAO pattern, which has a factory, interface, implement, entity and view.

So what if I select data from other table?

Here is my get method in implement for getting book

 public List get(String find) {
    try {
        ps = db.connect().prepareStatement("SELECT * FROM books WHERE title like ? ");
        ps.setString(1, "%" + find + "%");

        status = db.execute(ps);
        if (status) {
            books = db.get_result();
            listBooks = new ArrayList<>();

            while (books.next()) {
                entity_books b = new entity_books();
                b.setId(books.getInt(1));
                b.setId_category(books.getInt(2)); 
                b.setTitle(books.getString(3));
                listBooks.add(b);
            }
            books.close();
            return listBooks;
        }
    } catch (SQLException e) {
        System.out.println(e.getMessage());
    }
    return null;
}

and then in my view:

listBooks = booksDAO.get(find.getText());
    model = (DefaultTableModel) book_table.getModel();
    model.setRowCount(0);

    listBooks.forEach((data) -> {
        model.addRow(new Object[]{
            data.getId(),
            data.getId_category(),
            data.getTitle(),

        });
    });

This works fine, but I want the query to join table so I can see the category name instead of just ID category. I can do the query, but how do I apply that to my code?

Here is the query for joined table

select title,category from book b
join category c on c.id = b.id_category    

Normally if I select only 1 table, I would insert it to its entity ( book table -> book entity ), so how do I handle this with multiple tables?

7
  • any help / suggestion / clue on how to do this would be really helpful , thanks :) Commented Jun 2, 2018 at 17:17
  • can you clarify your question as a summery? Commented Jun 2, 2018 at 17:20
  • how do i handle the result from joined query in jave ? because if it only one table , i insert the result to its own entity class like in my code above. Commented Jun 2, 2018 at 17:22
  • This is really a broad question. There is a whole class of libraries (ORM) that does things like this (eg Hibernate), and they aren't simple. Commented Jun 2, 2018 at 17:26
  • So without a complicated code / using library , there is no simple way to use joined table ? but there is simple way to just use 1 table like above ? or do i misunderstood ? Commented Jun 2, 2018 at 17:28

1 Answer 1

1

I didn't use prepared statement, but this code works on my end.

String sql = "SELECT * FROM customer c JOIN company cmp ON c.company_idcompany = cmp.idcompany";

        ResultSet rs = stmt.executeQuery(sql);
        //STEP 5: Extract data from result set
        while (rs.next()) {
            //Retrieve this from customer table
            int id = rs.getInt("idcustomer");
            //Retrieve this from customer table
            String username = rs.getString("company_username");

            //Display values
            System.out.println("ID: " + id);
            System.out.println("Username: " + username);
        }
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help! , what if i have multiple line of result ? how do i insert the id & username to a list and output it ? thank you :) Because i need to output it on other class so i need to return the list to that class. so i guess it need a array inside array right ?
Thanks i worked it out with your help above :) thanks !

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.