You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableSqlContainer.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package com.vaadin.tests.components.table;
  2. import java.sql.Connection;
  3. import java.sql.SQLException;
  4. import java.sql.Statement;
  5. import java.util.Locale;
  6. import com.vaadin.data.Property;
  7. import com.vaadin.data.Property.ValueChangeEvent;
  8. import com.vaadin.data.Property.ValueChangeListener;
  9. import com.vaadin.data.util.sqlcontainer.SQLContainer;
  10. import com.vaadin.data.util.sqlcontainer.connection.JDBCConnectionPool;
  11. import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool;
  12. import com.vaadin.data.util.sqlcontainer.query.TableQuery;
  13. import com.vaadin.server.VaadinRequest;
  14. import com.vaadin.tests.components.AbstractTestUI;
  15. import com.vaadin.ui.CheckBox;
  16. import com.vaadin.ui.Label;
  17. import com.vaadin.ui.Table;
  18. import com.vaadin.ui.VerticalLayout;
  19. public class TableSqlContainer extends AbstractTestUI {
  20. protected Table table;
  21. @Override
  22. protected void setup(VaadinRequest request) {
  23. setLocale(Locale.ENGLISH);
  24. VerticalLayout layout = new VerticalLayout();
  25. addComponent(layout);
  26. table = new Table("Table with SQLContainer");
  27. layout.addComponent(table);
  28. final Label selectedLabel = new Label("Selected: null");
  29. layout.addComponent(selectedLabel);
  30. try {
  31. JDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool(
  32. "org.hsqldb.jdbc.JDBCDriver",
  33. "jdbc:hsqldb:mem:sqlcontainer", "SA", "", 2, 20);
  34. createTestTable(connectionPool);
  35. insertTestData(connectionPool);
  36. TableQuery q = new TableQuery("mytable", connectionPool);
  37. q.setVersionColumn("version");
  38. SQLContainer myContainer = new SQLContainer(q);
  39. table.setContainerDataSource(myContainer);
  40. } catch (SQLException e) {
  41. e.printStackTrace();
  42. }
  43. table.setImmediate(true);
  44. table.setSizeFull();
  45. table.setSelectable(true);
  46. table.addValueChangeListener(new Property.ValueChangeListener() {
  47. @Override
  48. public void valueChange(ValueChangeEvent event) {
  49. selectedLabel.setValue("Selected: "
  50. + event.getProperty().getValue());
  51. }
  52. });
  53. final CheckBox editMode = new CheckBox("Edit mode");
  54. editMode.addValueChangeListener(new ValueChangeListener() {
  55. @Override
  56. public void valueChange(ValueChangeEvent event) {
  57. table.setEditable(editMode.getValue());
  58. }
  59. });
  60. addComponent(editMode);
  61. }
  62. /**
  63. * (Re)creates the test table
  64. *
  65. * @param connectionPool
  66. */
  67. private void createTestTable(JDBCConnectionPool connectionPool) {
  68. Connection conn = null;
  69. try {
  70. conn = connectionPool.reserveConnection();
  71. Statement statement = conn.createStatement();
  72. try {
  73. statement.executeUpdate("DROP TABLE mytable");
  74. } catch (SQLException e) {
  75. }
  76. statement.execute("CREATE TABLE mytable "
  77. + "(id INTEGER GENERATED BY DEFAULT AS IDENTITY, D DATE,"
  78. + "MYFIELD VARCHAR(45), " + "PRIMARY KEY(ID))");
  79. statement.close();
  80. conn.commit();
  81. } catch (SQLException e) {
  82. e.printStackTrace();
  83. } finally {
  84. connectionPool.releaseConnection(conn);
  85. }
  86. }
  87. /**
  88. * Adds test data to the test table
  89. *
  90. * @param connectionPool
  91. * @throws SQLException
  92. */
  93. private void insertTestData(JDBCConnectionPool connectionPool)
  94. throws SQLException {
  95. Connection conn = null;
  96. try {
  97. conn = connectionPool.reserveConnection();
  98. Statement statement = conn.createStatement();
  99. statement
  100. .executeUpdate("INSERT INTO mytable VALUES(1, '2013-05-24', 'A0')");
  101. statement
  102. .executeUpdate("INSERT INTO mytable VALUES(2, '2013-04-26', 'A1')");
  103. statement
  104. .executeUpdate("INSERT INTO mytable VALUES(3, '2013-05-27', 'B0')");
  105. statement
  106. .executeUpdate("INSERT INTO mytable VALUES(4, '2013-04-28', 'B1')");
  107. statement.close();
  108. conn.commit();
  109. } catch (SQLException e) {
  110. e.printStackTrace();
  111. } finally {
  112. connectionPool.releaseConnection(conn);
  113. }
  114. }
  115. @Override
  116. protected String getTestDescription() {
  117. return "A test with Table connected to a SQLContainer using TableQuery";
  118. }
  119. @Override
  120. protected Integer getTicketNumber() {
  121. return 11224;
  122. }
  123. }