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.

sqlcontainer-getting-started.asciidoc 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. ---
  2. title: Getting Started with SQLContainer
  3. order: 2
  4. layout: page
  5. ---
  6. [[sqlcontainer.getting-started]]
  7. = Getting Started with SQLContainer
  8. Getting development going with the SQLContainer is easy and quite
  9. straight-forward. The purpose of this section is to describe how to create the
  10. required resources and how to fetch data from and write data to a database table
  11. attached to the container.
  12. [[sqlcontainer.getting-started.connection-pool]]
  13. == Creating a connection pool
  14. First, we need to create a connection pool to allow the SQLContainer to connect
  15. to a database. Here we will use the [classname]#SimpleJDBCConnectionPool#, which
  16. is a basic implementation of connection pooling with JDBC data sources. In the
  17. following code, we create a connection pool that uses the HSQLDB driver together
  18. with an in-memory database. The initial amount of connections is 2 and the
  19. maximum amount is set at 5. Note that the database driver, connection url,
  20. username, and password parameters will vary depending on the database you are
  21. using.
  22. ----
  23. JDBCConnectionPool pool = new SimpleJDBCConnectionPool(
  24. "org.hsqldb.jdbc.JDBCDriver",
  25. "jdbc:hsqldb:mem:sqlcontainer", "SA", "", 2, 5);
  26. ----
  27. [[sqlcontainer.getting-started.query-delegate]]
  28. == Creating the [classname]#TableQuery# Query Delegate
  29. After the connection pool is created, we'll need a query delegate for the
  30. SQLContainer. The simplest way to create one is by using the built-in
  31. [classname]#TableQuery# class. The [classname]#TableQuery# delegate provides
  32. access to a defined database table and supports reading and writing data
  33. out-of-the-box. The primary key(s) of the table may be anything that the
  34. database engine supports, and are found automatically by querying the database
  35. when a new [classname]#TableQuery# is instantiated. We create the
  36. [classname]#TableQuery# with the following statement:
  37. ----
  38. TableQuery tq = new TableQuery("tablename", connectionPool);
  39. ----
  40. In order to allow writes from several user sessions concurrently, we must set a
  41. version column to the [classname]#TableQuery# as well. The version column is an
  42. integer- or timestamp-typed column which will either be incremented or set to
  43. the current time on each modification of the row. [classname]#TableQuery#
  44. assumes that the database will take care of updating the version column; it just
  45. makes sure the column value is correct before updating a row. If another user
  46. has changed the row and the version number in the database does not match the
  47. version number in memory, an [classname]#OptimisticLockException# is thrown and
  48. you can recover by refreshing the container and allow the user to merge the
  49. data. The following code will set the version column:
  50. ----
  51. tq.setVersionColumn("OPTLOCK");
  52. ----
  53. [[sqlcontainer.getting-started.container-creation]]
  54. == Creating the Container
  55. Finally, we may create the container itself. This is as simple as stating:
  56. ----
  57. SQLContainer container = new SQLContainer(tq);
  58. ----
  59. After this statement, the [classname]#SQLContainer# is connected to the table
  60. tablename and is ready to use for example as a data source for a Vaadin
  61. [classname]#Table# or a Vaadin [classname]#Form#.