在网格vaadin中显示SQL查询数据

发布于 2025-01-26 21:11:00 字数 1495 浏览 2 评论 0原文

我想在网格vaadin中显示我的SQL查询的结果,

我可以从toto表中获取数据,

但是我无法在网格中显示它们

如何做?

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 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空心↖ 2025-02-02 21:11:00

您已经用网格&lt; customer&gt;定义了类型客户的网格。这意味着可以通过提供客户列表来设置网格。使用您的代码示例:

        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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文