Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SampleCalendarDatabase.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.demo.util;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.sql.Timestamp;
  11. /**
  12. * Creates temporary database named toolkit with sample table named employee and
  13. * populates it with data. By default we use HSQLDB. Ensure that you have
  14. * hsqldb.jar under WEB-INF/lib directory. Database is will be created into
  15. * memory.
  16. *
  17. * @author IT Mill Ltd.
  18. *
  19. */
  20. public class SampleCalendarDatabase {
  21. public static final int ENTRYCOUNT = 100;
  22. public static final String DB_TABLE_NAME = "calendar";
  23. public static final String PROPERTY_ID_START = "EVENTSTART";
  24. public static final String PROPERTY_ID_END = "EVENTEND";
  25. public static final String PROPERTY_ID_TITLE = "TITLE";
  26. public static final String PROPERTY_ID_NOTIME = "NOTIME";
  27. private Connection connection = null;
  28. private static final String[] titles = new String[] { "Meeting", "Dentist",
  29. "Haircut", "Bank", "Birthday", "Library", "Rent", "Acme test",
  30. "Party" };
  31. /**
  32. * Create temporary database.
  33. *
  34. */
  35. public SampleCalendarDatabase() {
  36. // connect to SQL database
  37. connect();
  38. // initialize SQL database
  39. createTables();
  40. // test by executing sample JDBC query
  41. testDatabase();
  42. }
  43. /**
  44. * Creates sample table named employee and populates it with data.Use the
  45. * specified database connection.
  46. *
  47. * @param connection
  48. */
  49. public SampleCalendarDatabase(Connection connection) {
  50. // initialize SQL database
  51. createTables();
  52. // test by executing sample JDBC query
  53. testDatabase();
  54. }
  55. /**
  56. * Connect to SQL database. In this sample we use HSQLDB and an toolkit
  57. * named database in implicitly created into system memory.
  58. *
  59. */
  60. private void connect() {
  61. // use memory-Only Database
  62. final String url = "jdbc:hsqldb:mem:toolkit";
  63. try {
  64. Class.forName("org.hsqldb.jdbcDriver").newInstance();
  65. connection = DriverManager.getConnection(url, "sa", "");
  66. } catch (final Exception e) {
  67. throw new RuntimeException(e);
  68. }
  69. }
  70. /**
  71. * use for SQL commands CREATE, DROP, INSERT and UPDATE
  72. *
  73. * @param expression
  74. * @throws SQLException
  75. */
  76. public void update(String expression) throws SQLException {
  77. Statement st = null;
  78. st = connection.createStatement();
  79. final int i = st.executeUpdate(expression);
  80. if (i == -1) {
  81. System.out.println("SampleDatabase error : " + expression);
  82. }
  83. st.close();
  84. }
  85. /**
  86. * Create test table and few rows. Issue note: using capitalized column
  87. * names as HSQLDB returns column names in capitalized form with this demo.
  88. *
  89. */
  90. private void createTables() {
  91. try {
  92. String stmt = null;
  93. stmt = "CREATE TABLE "
  94. + DB_TABLE_NAME
  95. + " ( ID INTEGER IDENTITY, TITLE VARCHAR(100), "
  96. + "EVENTSTART DATETIME, EVENTEND DATETIME, NOTIME BOOLEAN )";
  97. update(stmt);
  98. for (int j = 0; j < ENTRYCOUNT; j++) {
  99. final Timestamp start = new Timestamp(new java.util.Date()
  100. .getTime());
  101. start.setDate((int) ((Math.random() - 0.4) * 200));
  102. start.setMinutes(0);
  103. start.setHours(8 + (int) Math.random() * 12);
  104. final Timestamp end = new Timestamp(start.getTime());
  105. if (Math.random() < 0.7) {
  106. long t = end.getTime();
  107. final long hour = 60 * 60 * 1000;
  108. t = t + hour + (Math.round(Math.random() * 3 * hour));
  109. end.setTime(t);
  110. }
  111. stmt = "INSERT INTO "
  112. + DB_TABLE_NAME
  113. + "(TITLE, EVENTSTART, EVENTEND, NOTIME) VALUES ("
  114. + "'"
  115. + titles[(int) (Math.round(Math.random()
  116. * (titles.length - 1)))] + "','" + start
  117. + "','" + end + "'," + (Math.random() > 0.7) + ")";
  118. update(stmt);
  119. }
  120. } catch (final SQLException e) {
  121. if (e.toString().indexOf("Table already exists") == -1) {
  122. throw new RuntimeException(e);
  123. }
  124. }
  125. }
  126. /**
  127. * Test database connection with simple SELECT command.
  128. *
  129. */
  130. private String testDatabase() {
  131. String result = null;
  132. try {
  133. final Statement stmt = connection.createStatement(
  134. ResultSet.TYPE_SCROLL_INSENSITIVE,
  135. ResultSet.CONCUR_UPDATABLE);
  136. final ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM "
  137. + DB_TABLE_NAME);
  138. rs.next();
  139. result = "rowcount for table test is " + rs.getObject(1).toString();
  140. stmt.close();
  141. } catch (final SQLException e) {
  142. throw new RuntimeException(e);
  143. }
  144. return result;
  145. }
  146. public Connection getConnection() {
  147. return connection;
  148. }
  149. }