diff options
author | Jani Laakso <jani.laakso@itmill.com> | 2007-04-02 14:27:33 +0000 |
---|---|---|
committer | Jani Laakso <jani.laakso@itmill.com> | 2007-04-02 14:27:33 +0000 |
commit | 7397520086aec1cff86d6da93577c9c8d68aeb3f (patch) | |
tree | c4b26437a7584d7800c391c09a627b1326951bb9 /src/com/itmill/toolkit/demo/util/SampleDatabase.java | |
parent | dc6043cba8ec3e51a75d7c0f339ad94f39e8f2c3 (diff) | |
download | vaadin-framework-7397520086aec1cff86d6da93577c9c8d68aeb3f.tar.gz vaadin-framework-7397520086aec1cff86d6da93577c9c8d68aeb3f.zip |
Added new demos.
svn changeset:1097/svn branch:trunk
Diffstat (limited to 'src/com/itmill/toolkit/demo/util/SampleDatabase.java')
-rw-r--r-- | src/com/itmill/toolkit/demo/util/SampleDatabase.java | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/com/itmill/toolkit/demo/util/SampleDatabase.java b/src/com/itmill/toolkit/demo/util/SampleDatabase.java new file mode 100644 index 0000000000..36c8b26510 --- /dev/null +++ b/src/com/itmill/toolkit/demo/util/SampleDatabase.java @@ -0,0 +1,148 @@ +package com.itmill.toolkit.demo.util; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +public class SampleDatabase { + + public static final int ROWCOUNT = 1000; + + private Connection connection = null; + + private static final String[] firstnames = new String[] { "John", "Mary", + "Joe", "Sarah", "Jeff", "Jane", "Peter", "Marc", "Josie", "Linus" }; + + private static final String[] lastnames = new String[] { "Torvalds", + "Smith", "Jones", "Beck", "Sheridan", "Picard", "Hill", "Fielding", + "Einstein" }; + + private static final String[] titles = new String[] { "Project Manager", + "Marketing Manager", "Sales Manager", "Trainer", "IT Support", + "Account Manager", "Customer Support", "Testing Engineer", + "Software Designer", "Programmer", "Consultant" }; + + private static final String[] units = new String[] { "Tokyo", + "Mexico City", "Seoul", "New York", "Sao Paulo", "Bombay", "Delhi", + "Shanghai", "Los Angeles", "London", "Bangalore", "Hong Kong", + "Madrid", "Milano", "Beijing", "Paris", "Moscow", "Helsinki" }; + + /** + * Creates temporary database named toolkit with sample table named employee + * and populates it with data. By default we use HSQLDB. Ensure that you + * have hsqldb.jar under WEB-INF/lib directory. Database is will be created + * into memory. + * + */ + public SampleDatabase() { + // connect to SQL database + connect(); + + // initialize SQL database + createTables(); + + // test by executing sample JDBC query + testDatabase(); + } + + /** + * Creates sample table named employee and populates it with data.Use the + * specified database connection. + * + * @param connection + */ + public SampleDatabase(Connection connection) { + // initialize SQL database + createTables(); + + // test by executing sample JDBC query + testDatabase(); + } + + /** + * Connect to SQL database. In this sample we use HSQLDB and an toolkit + * named database in implicitly created into system memory. + * + */ + private void connect() { + // use memory-Only Database + String url = "jdbc:hsqldb:mem:toolkit"; + try { + Class.forName("org.hsqldb.jdbcDriver").newInstance(); + connection = DriverManager.getConnection(url, "sa", ""); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + // use for SQL commands CREATE, DROP, INSERT and UPDATE + public void update(String expression) throws SQLException { + Statement st = null; + st = connection.createStatement(); + int i = st.executeUpdate(expression); + if (i == -1) { + System.out.println("SampleDatabase error : " + expression); + } + st.close(); + } + + /** + * Create test table and few rows. Issue note: using capitalized column + * names as HSQLDB returns column names in capitalized form with this demo. + * + */ + private void createTables() { + try { + String stmt = null; + stmt = "CREATE TABLE employee ( ID INTEGER IDENTITY, FIRSTNAME VARCHAR(100), " + + "LASTNAME VARCHAR(100), TITLE VARCHAR(100), UNIT VARCHAR(100) )"; + update(stmt); + for (int j = 0; j < ROWCOUNT; j++) { + stmt = "INSERT INTO employee(FIRSTNAME, LASTNAME, TITLE, UNIT) VALUES (" + + "'" + + firstnames[(int) (Math.random() * (firstnames.length - 1))] + + "'," + + "'" + + lastnames[(int) (Math.random() * (lastnames.length - 1))] + + "'," + + "'" + + titles[(int) (Math.random() * (titles.length - 1))] + + "'," + + "'" + + units[(int) (Math.random() * (units.length - 1))] + + "'" + ")"; + update(stmt); + } + } catch (SQLException e) { + if (!e.toString().contains("Table already exists")) + throw new RuntimeException(e); + } + } + + /** + * Test database connection with simple SELECT command. + * + */ + private String testDatabase() { + String result = null; + try { + Statement stmt = connection.createStatement( + ResultSet.TYPE_SCROLL_INSENSITIVE, + ResultSet.CONCUR_UPDATABLE); + ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM employee"); + rs.next(); + result = "rowcount for table test is " + rs.getObject(1).toString(); + stmt.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return result; + } + + public Connection getConnection() { + return connection; + } + +} |