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.

SampleDatabase.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /**
  11. * Creates temporary database named toolkit with sample table named employee and
  12. * populates it with data. By default we use HSQLDB. Ensure that you have
  13. * hsqldb.jar under WEB-INF/lib directory. Database is will be created into
  14. * memory.
  15. *
  16. * @author IT Mill Ltd.
  17. *
  18. */
  19. public class SampleDatabase {
  20. public static final int ROWCOUNT = 1000;
  21. private Connection connection = null;
  22. private static final String[] firstnames = new String[] { "Amanda",
  23. "Andrew", "Bill", "Frank", "Matt", "Xavier", "John", "Mary", "Joe",
  24. "Gloria", "Marcus", "Belinda", "David", "Anthony", "Julian",
  25. "Paul", "Carrie", "Susan", "Gregg", "Michael", "William", "Ethan",
  26. "Thomas", "Oscar", "Norman", "Roy", "Sarah", "Jeff", "Jane",
  27. "Peter", "Marc", "Josie", "Linus" };
  28. private static final String[] lastnames = new String[] { "Torvalds",
  29. "Smith", "Jones", "Beck", "Burton", "Bell", "Davis", "Burke",
  30. "Bernard", "Hood", "Scott", "Smith", "Carter", "Roller", "Conrad",
  31. "Martin", "Fisher", "Martell", "Freeman", "Hackman", "Jones",
  32. "Harper", "Russek", "Johnson", "Sheridan", "Hill", "Parker",
  33. "Foster", "Moss", "Fielding" };
  34. private static final String[] titles = new String[] { "Project Manager",
  35. "Marketing Manager", "Sales Manager", "Sales", "Trainer",
  36. "Technical Support", "Account Manager", "Customer Support",
  37. "Testing Engineer", "Software Designer", "Programmer", "Consultant" };
  38. private static final String[] units = new String[] { "Tokyo",
  39. "Mexico City", "Seoul", "New York", "Sao Paulo", "Bombay", "Delhi",
  40. "Shanghai", "Los Angeles", "London", "Shanghai", "Sydney",
  41. "Bangalore", "Hong Kong", "Madrid", "Milano", "Beijing", "Paris",
  42. "Moscow", "Berlin", "Helsinki" };
  43. /**
  44. * Create temporary database.
  45. *
  46. */
  47. public SampleDatabase() {
  48. // connect to SQL database
  49. connect();
  50. // initialize SQL database
  51. createTables();
  52. // test by executing sample JDBC query
  53. testDatabase();
  54. }
  55. /**
  56. * Creates sample table named employee and populates it with data.Use the
  57. * specified database connection.
  58. *
  59. * @param connection
  60. */
  61. public SampleDatabase(Connection connection) {
  62. // initialize SQL database
  63. createTables();
  64. // test by executing sample JDBC query
  65. testDatabase();
  66. }
  67. /**
  68. * Connect to SQL database. In this sample we use HSQLDB and an toolkit
  69. * named database in implicitly created into system memory.
  70. *
  71. */
  72. private void connect() {
  73. // use memory-Only Database
  74. final String url = "jdbc:hsqldb:mem:toolkit";
  75. try {
  76. Class.forName("org.hsqldb.jdbcDriver").newInstance();
  77. connection = DriverManager.getConnection(url, "sa", "");
  78. } catch (final Exception e) {
  79. throw new RuntimeException(e);
  80. }
  81. }
  82. /**
  83. * use for SQL commands CREATE, DROP, INSERT and UPDATE
  84. *
  85. * @param expression
  86. * @throws SQLException
  87. */
  88. public void update(String expression) throws SQLException {
  89. Statement st = null;
  90. st = connection.createStatement();
  91. final int i = st.executeUpdate(expression);
  92. if (i == -1) {
  93. throw new SQLException("Database error : " + expression);
  94. }
  95. st.close();
  96. }
  97. /**
  98. * Create test table and few rows. Issue note: using capitalized column
  99. * names as HSQLDB returns column names in capitalized form with this demo.
  100. *
  101. */
  102. private void createTables() {
  103. try {
  104. String stmt = null;
  105. stmt = "CREATE TABLE employee ( ID INTEGER IDENTITY, FIRSTNAME VARCHAR(100), "
  106. + "LASTNAME VARCHAR(100), TITLE VARCHAR(100), UNIT VARCHAR(100) )";
  107. update(stmt);
  108. for (int j = 0; j < ROWCOUNT; j++) {
  109. stmt = "INSERT INTO employee(FIRSTNAME, LASTNAME, TITLE, UNIT) VALUES ("
  110. + "'"
  111. + firstnames[(int) (Math.random() * (firstnames.length - 1))]
  112. + "',"
  113. + "'"
  114. + lastnames[(int) (Math.random() * (lastnames.length - 1))]
  115. + "',"
  116. + "'"
  117. + titles[(int) (Math.random() * (titles.length - 1))]
  118. + "',"
  119. + "'"
  120. + units[(int) (Math.random() * (units.length - 1))]
  121. + "'" + ")";
  122. update(stmt);
  123. }
  124. } catch (final SQLException e) {
  125. if (e.toString().indexOf("Table already exists") == -1) {
  126. throw new RuntimeException(e);
  127. }
  128. }
  129. }
  130. /**
  131. * Test database connection with simple SELECT command.
  132. *
  133. */
  134. private String testDatabase() {
  135. String result = null;
  136. try {
  137. final Statement stmt = connection.createStatement(
  138. ResultSet.TYPE_SCROLL_INSENSITIVE,
  139. ResultSet.CONCUR_UPDATABLE);
  140. final ResultSet rs = stmt
  141. .executeQuery("SELECT COUNT(*) FROM employee");
  142. rs.next();
  143. result = "rowcount for table test is " + rs.getObject(1).toString();
  144. stmt.close();
  145. } catch (final SQLException e) {
  146. throw new RuntimeException(e);
  147. }
  148. return result;
  149. }
  150. public Connection getConnection() {
  151. return connection;
  152. }
  153. }