sample for connecting database and presenting data in a table

2,611 views
Skip to first unread message

lifemichael

unread,
Jun 9, 2013, 1:20:01 AM6/9/13
to shenkar-industrial-engineering-java-2013-02
package com.example.addressbookdb;

import java.sql.SQLException;

import com.vaadin.data.util.sqlcontainer.SQLContainer;
import
com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
import com.vaadin.data.util.sqlcontainer.query.TableQuery;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Table;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

public class AddressbookdbUI extends UI
{

private static final long serialVersionUID = 1L;

// creating a table for showing all data on screen
private Table contactList = new Table();

// creating a simple SQLContainer that represents
// a table on our database
SQLContainer contactContainer = createMySQLContainer();

// creating a simple layout
VerticalLayout layout = new VerticalLayout();

private static final String[] fieldNames = new String[]
{ "first", "last", "company", "mobile", "work", "email", "street",
"city",
"state", "country", "zip", "id", "facebook", "linkedin" };

protected void init(VaadinRequest request)

{
initLayout();
initContactList();
}

private void initLayout()
{

// setting the main layout
setContent(layout);

// adding a table on out layout
layout.addComponent(contactList);

}

private void initContactList()
{
// setting the table data source
contactList.setContainerDataSource(contactContainer);

// setting the visible coloumns to be just three
contactList.setVisibleColumns(new String[]
{ "first", "last", "company" });

// set the table to respond immediately when it changes
contactList.setImmediate(true);
}

private static SQLContainer createMySQLContainer()
{
TableQuery query = null;
SQLContainer temp = null;
try
{

SimpleJDBCConnectionPool connectionPool = new
SimpleJDBCConnectionPool(
"com.mysql.jdbc.Driver",
"jdbc:mysql://server.zindell.com:3306/vaadin", "_____",
"_____", 2, 5);
query = new TableQuery("customers", connectionPool);
query.setVersionColumn("id");

temp = new SQLContainer(query);
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return temp;

}

}
Reply all
Reply to author
Forward
0 new messages