Step 10.2: Add a new class, HelloHSQL

Step 10.2: Add a new class, HelloHSQL


At this point, you can either add a new class and type in the code, or import the class directly from the included CD-ROM. If you would like more practice with the editor, then continue with Steps 10.2 and 10.3. If you would rather just import the code and move on to testing, then skip ahead to Step 10.4.

click to expand

q 10.2(a) Make sure the SWT project is selected.

q 10.2(b) Right click in the Package Explorer, select New/Class.

click to expand
Figure 10.9: Creating a new class using the New/Class menu option.

q 10.2(c) Make sure the Source Folder is 'SWT'.

q 10.2(d) Leave Package blank.

q 10.2(e) Leave Enclosing type unchecked.

q 10.2(l) Enter 'HelloHSQL' in the Name field.

q 10.2(g) Make sure public is checked, abstract and final are unchecked.

q 10.2(h) Leave Superclass as java.lang.Object, Interfaces blank.

q 10.2(i) Make sure public static void main(String[] args) is checked.

q 10.2(j) Leave Constructors from superclass and Inherited abstract methods unchecked.

q 10.2(k) Click Finish.

click to expand
Figure 10.10: Setting the fields properly for a new class.

This will create a public class HelloHSQL in the default package (because you left Package blank) in project SWT. In Steps 10.2(i) and 10.2(j) you instructed the IDE to create a standard "main" method but no other methods. That being the case, you will see the result as in Figure 10.11.

click to expand
Figure 10.11: The result of the addition of the new class HelloSWT.

Step 10.3: Write the code for the HelloHSQL class

click to expand

q 10.3(a) Enter the source code below after the comments.

 import java.sql.*;

public class HelloHSQL {

   private static final String SQLCLASS = "org.hsqldb.jdbcDriver";
   private static final String SQLURL   = "jdbc:hsqldb:hsqldb";
   private static final String SQLUSER  = "sa";
   private static final String SQLPWD   = "";

   public static void main(String[] args) {

          try {
                 // Create a connection
                 Class.forName (SQLCLASS);
                 Connection c = DriverManager.getConnection(SQLURL, SQLUSER, SQLPWD);
                 Statement s = c.createStatement();

                 // Delete and recreate the table
                 try {
s.execute("DROP TABLE ITEMS");
} catch (Exception e) {}
                 s.execute("CREATE TABLE ITEMS (ItemNumber CHAR, Description CHAR)");

                 // Insert data
                 s.execute("INSERT INTO ITEMS VALUES ('DOG', 'Cuddly Duddly')");
                 s.execute("INSERT INTO ITEMS VALUES ('CAT', 'Felix the Cat')");
                 s.execute("INSERT INTO ITEMS VALUES ('OCTOPUS', 'Squidworth')");

                 // Check the results
                 ResultSet rs = s.executeQuery("SELECT * FROM ITEMS");
                 while (
                 "Item: " + rs.getString("ItemNumber") +
                 ", Description: " + rs.getString("Description"));

                 // Shut down

          } catch (Exception e) {
                 System.out.println("Error: " + e);

          // When done, clean up resource and exit

At this point, your screen will look like the one in Figure 10.12.

click to expand
Figure 10.12: Source code entered into the editor pane.

Use the scroll bar to reposition the source window to the top of the code. You can also use standard editing keystrokes, such as Ctrl-Home.

q 10.3(b) Position window to top of source.

click to expand
Figure 10.13: Using the scroll bar to position the window to the top of the source.

click to expand
Figure 10.14: It's a larger source, so you'll have to scroll two or three times.

Now save the source. You can use the Save tool on the main Eclipse tool bar, Ctrl-S, or the editor popup menu. I'll use the Save tool:

q 10.3(c) Click on the Save icon on the tool bar.

click to expand
Figure 10.15: Using the Save tool to save the source.

You'll see the screen shown in Figure 10.16. You may skip ahead now to Step 10.5.

click to expand
Figure 10.16: The workbench after saving the new HelloHSQL source.