'Display SQL query data in grid vaadin

I would like to display the result of my SQL query in a grid vaadin

I can get the data from my toto table

But I can't display them in a grid

How to do it?

public class MainView extends VerticalLayout {

    private CustomerService service = CustomerService.getInstance();
    private Grid<Customer> grid = new Grid<>(Customer.class);

    public MainView() {
        /*Button button = new Button("Click me",
                event -> Notification.show("Clicked!"));
        add(button);*/

        //BDD
        try{
            //step1 load the driver class
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //step2 create  the connection object
            Connection con=DriverManager.getConnection(
                    "jdbc:oracle:thin:@xxxx:1521:chan","xxx","xxx");

            //step3 create the statement object
            Statement stmt=con.createStatement();

            //step4 execute query
            ResultSet rs=stmt.executeQuery("select * from toto");
            while(rs.next())
                System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));


            //step5 close the connection object
            con.close();

        }catch(Exception e){ System.out.println(e);}



        //GRID
        grid.setColumns("firstName", "lastName", "status");

        add(grid);

        setSizeFull();

        updateList();
    }

    public void updateList()
    {
        grid.setItems(service.findAll());
    }
}

I modified my code by adding a grid, now I would like to understand how I can display the results of my query in my grid



Solution 1:[1]

You have defined a Grid of the type Customer with Grid<Customer>. This means the Grid can be set up by providing a List of Customers. Using your code example:

        List<Customer> customerList = new ArrayList<>();
        try{
            //step1 load the driver class
            Class.forName("oracle.jdbc.driver.OracleDriver");

            //step2 create  the connection object
            Connection con=DriverManager.getConnection(
                    "jdbc:oracle:thin:@xxxx:1521:chan","xxx","xxx");

            //step3 create the statement object
            Statement stmt=con.createStatement();

            //step4 execute query
            ResultSet rs=stmt.executeQuery("select * from toto");
            while(rs.next()) {
                Customer customer = new Customer();
                customer.setFirstName(rs.getString(2));
                customer.setLastName(rs.getString(3));
                customer.setStatus(rs.getInt(1));
                customerList.add(customer);
            }


            //step5 close the connection object
            con.close();

        }catch(Exception e){ System.out.println(e);}



        //GRID
        grid.setColumns("firstName", "lastName", "status");
        grid.setItems(customerList);
        add(grid);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ollitietavainen