![](/img/trans.png)
[英]how to create a table in vaadin 10 and get data from mysql database
[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.