2016-04-25 48 views
0

對vaadin來說非常新穎。我有一個任務,我應該顯示從MySQL數據庫中獲取數據並將數據顯示在表中。之後所有的CRUD操作都將完成。在vaadin中顯示來自mysql db表的數據

我正在使用純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); 

現在我卡住了,如何在瀏覽器中顯示Products表中的數據。請給我建議,因爲我希望很多GURU都能輕鬆完成這一項。

回答

1

試試這個簡單的例子:

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

+0

謝謝,我會試試這個:) – user6250770

+0

Insha allah ..我會這樣做的。 :) – user6250770

0

你會更瞭解數據在這兩個環節vaading結合:

Datamodel overviewSQL container

一旦你有權訪問SQL數據,你可以n使用表格或網格來顯示數據。 也有其他解決方案,例如JPAContainer或BeanItemContainers,這取決於您希望如何構建應用程序邏輯。

+0

謝謝,是的,我必須學習很多關於vaadin – user6250770

相關問題