簡體   English   中英

在vaadin中的mysql db表中顯示表中的數據

[英]Show data in table from mysql db table in vaadin

對vaadin來說是個新手。 我有一個任務,在這里我應該顯示從mysql db獲取的數據,並在表中顯示數據。 稍后將完成所有CRUD操作。

我正在使用純JDBC,java,vaadin。 我試過了,JDBC連接是很容易的。 但是我被困在瀏覽器表中顯示數據。

下面的代碼是我嘗試過的。

    SimpleJDBCConnectionPool pool = new  
   SimpleJDBCConnectionPool("com.mysql.jdbc.Driver",
   "jdbc:mysql://localhost:3306/mg", "root", "root");
        Notification.show("DB connected");
        SQLContainer container = new SQLContainer(new FreeformQuery(
                "SELECT * FROM PRODUCT", Arrays.asList("ID"), pool));
        Notification.show("hi" +container);
        Table table = new Table("Products", container);

現在我堅持,如何在瀏覽器中顯示“產品”表中的數據。 請建議我,因為我希望GURU能夠輕松完成這一任務。

試試這個簡單的例子:

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import com.vaadin.ui.Table;
import com.vaadin.ui.Window;

public class MyApplication extends Application
{
    Table table;
    Window main = new Window("Sample");
    Connection con;
    PreparedStatement ps;
    Statement cs;
    ResultSet rs;
    String dbUrl = "jdbc:mysql://localhost:3306/yourdatabasename";

    public void init()
    {
        setMainWindow(main);
        table = new Table();
        table.setStyleName("iso3166");
        table.setPageLength(6);
        table.setSizeFull();
        table.setSelectable(true);
        table.setMultiSelect(false);
        table.setImmediate(true);
        table.setColumnReorderingAllowed(true);
        table.setColumnCollapsingAllowed(true);
        /*
         * Define the names and data types of columns. The "default value" parameter is meaningless here.
         */
        table.addContainerProperty("NAME", String.class, null);
        table.addContainerProperty("CODE", Integer.class, null);

        /* Add a few items in the table. */
        try
        {

            con = DriverManager.getConnection(dbUrl, "root", "root");
            cs = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = cs.executeQuery("select Name,Code from tablename");
            while (rs.next())
            {
                table.addItem(new Object[] { rs.getString(1), rs.getInt(2) }, rs.getInt(2));
            }
        }
        catch (Exception e)
        {
            // getWindow(null).showNotification("Error");
        }
        table.setWidth("300px");
        table.setHeight("150px");
        main.addComponent(table);
    }
}

對於參考查看此鏈接: https : //vaadin.com/forum#!/thread/272110

您可能會在以下兩個鏈接中了解有關vaading中數據綁定的更多信息:

數據模型概述SQL容器

一旦可以訪問SQL數據,則可以使用表或網格顯示數據。 根據您希望如何構造應用程序邏輯,還有其他解決方案,例如JPAContainer或BeanItemContainers。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM