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.

SampleCalendarDatabase.java 4.3KB

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