hi there!
i’m a c# developer so i’m kind of spoilt by entity framework, ado.net, linq and super simple ui binding techniques.
now this is my first approach to get data from an ms sql server database into a vaadin table.
since i didn’t found a beginner-friendly entity framework + linq alternative out there i decided to use “standard jdbc stuff” in combination with my own objects (beans, the way i created it?).
the goal was to get some data from a ms sql server database into a vaadin table and be able to get the selected table items back if i click on them.
am i on the right way? is it “ok” to do it like this? or am i breaking some fundamental rules? i shall use a connection pool, huh?
thanks!!!
package com.example.addressbook;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.*;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.*;
@SuppressWarnings("serial")
@Theme("addressbook")
public class AddressbookUI extends UI
{
private Table tableContactList = new Table();
@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = AddressbookUI.class)
public static class Servlet extends VaadinServlet
{
}
@Override
protected void init(VaadinRequest request)
{
initLayout();
initTable();
}
private void initLayout()
{
VerticalLayout mainLayout = new VerticalLayout();
setContent(mainLayout);
mainLayout.addComponent(tableContactList);
mainLayout.setSizeFull();
tableContactList.setSizeFull();
}
private void initTable()
{
BeanItemContainer<Person> personContainer = new BeanItemContainer<Person>(Person.class);
for (Person p : getPersons())
{
personContainer.addBean(p);
}
tableContactList.setContainerDataSource(personContainer);
tableContactList.setVisibleColumns(new Object[] { "id","firstname", "lastname" });
tableContactList.setSelectable(true);
tableContactList.setImmediate(true);
tableContactList.addValueChangeListener(new Property.ValueChangeListener()
{
public void valueChange(ValueChangeEvent event)
{
BeanItem<Person> selectedBeanItem = (BeanItem)tableContactList.getItem(tableContactList.getValue());
Person selectedPerson = (Person)selectedBeanItem.getBean();
Notification.show(selectedPerson.firstname);
}
});
}
@SuppressWarnings("unchecked")
private List<Person> getPersons()
{
List<Person> persons = new ArrayList<Person>();
Connection conn = null;
Statement stmt = null;
try
{
conn = getDbConnection();
stmt = conn.createStatement();
String query = "SELECT id, firstname, lastname FROM person";
ResultSet rs = stmt.executeQuery(query);
while (rs.next())
{
Person p = new Person();
p.setId(rs.getInt("id"));
p.setFirstname(rs.getString("firstname"));
p.setLastname(rs.getString("lastname"));
persons.add(p);
}
rs.close();
stmt.close();
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (stmt != null)
stmt.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return persons;
}
private Connection getDbConnection()
{
Connection conn = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:sqlserver://localhost\\MSSQLDEV;databaseName=vaadin_test", "sa", "");
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
public class Person implements Serializable
{
private int id;
private String firstname;
private String lastname;
public Person()
{
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstname()
{
return firstname;
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getLastname()
{
return lastname;
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
}
}