user6250770
user6250770

Reputation: 690

Show data in table from mysql db table in vaadin

Am very new to vaadin. I have a task, where i should show the data be fetched from mysql db and show the data in a table. Later all CRUD operations will be done.

I am using pure JDBC,java,vaadin. i tried and jdbc connection is quite ease. But i am stuck in showing the data in table in browser.

the below code is which i tried.

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

Now i stuck, how to show the data from Products table in browser. please suggest me as i hope lot of GURU's done this one with ease.

Upvotes: 0

Views: 2128

Answers (2)

André Schild
André Schild

Reputation: 4754

You might learn more about data binding in vaading in these two links:

Datamodel overview and SQL container

Once you have the access to the SQL data, you can then use a Table or Grid to display the data. There are other solutions available too, like JPAContainer or BeanItemContainers, depending on how you wish to structure your application logic.

Upvotes: 0

Haseeb Anser
Haseeb Anser

Reputation: 494

Try this simple example:

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

For Reference view this link: https://vaadin.com/forum#!/thread/272110

Upvotes: 1

Related Questions