< Previous | Next >

Lesson 1: Setting up your Java project

In this lesson, you set up Optim™ Development Studio to develop a Java™ application that uses pureQuery client optimization.

Before you begin

Ensure that you can connect to the DB2 SAMPLE database from your computer.

In this lesson, you create a Java project and database connection with the workbench, create a simple Java application, and add pureQuery support to the project.

Creating a database connection and a Java project

Enabling pureQuery support in the workbench requires both a Java project and a database connection. You can create the project and connection in any order.
This tutorial assumes that workbench starts with the IBM® SQL and Routine Development perspective and the default set of views.

You create the create a connection to the SAMPLE database in the workbench with the SQL and Routine Development perspective. Then you switch to the Java perspective and create a Java project. The connection to the database is needed when you enable pureQuery support for the project

To create a database connection and Java project:

  1. Start Optim Development Studio.

    The workbench opens the IBM SQL and Routine Development perspective.

  2. Create a connection to the SAMPLE database.
    1. In the Data Source Explorer window, right-click the Database Connections folder and select New.
    2. Follow the steps in the wizard to create a connection to your instance of the SAMPLE database.
      In Connection Parameters, select the following to create a DB2® connection:
      • In the Select a database manager field field, select DB2 for Linux, UNIX, and Windows.
      • In the JDBC driver field, select IBM Data Server Driver for JDBC and SQLJ (JDBC 4.0) Default.
  3. Switch to the Java perspective.

    Click Task Navigator > Java Database Application Development.

    The Java perspective opens. This tutorial assumes that you are working with the default Java perspective.

    Notice the two tabs in the upper left part of the Java perspective: the Package Explorer and the Hierarchy tabs:

    Figure 1. The Package Explorer and the Hierarchy tabs
    The Package Explorer and the Hierarchy tabs
  4. If the Task Launcher view is open, close it.

    Click the X on the Task Launcher tab.

  5. Create a Java project.
    1. Right-click in the Package Explorer and select New > Java Project.

      The New Java Project wizard opens.

    2. Type pq-tutorial in the Project name field at the top of the wizard. Leave the rest of the options at their default values.
    3. Click Finish.

    Optim Development Studio creates your Java project. You see the pq-tutorial project in the Package Explorer.

    Figure 2. The pureQuery_test Java project in the Package Explorer
    Package Explorer view with pureQuery_test project

Adding a simple Java application to your project

You add code for a simple Java application to your project. The application runs different SQL statements against the SAMPLE database.

To add a simple Java application to your project:

  1. Create a package in your project.
    1. In the Package Explorer, right-click the pq_tutorial/src folder, and click New > Class. The New Java Class wizard opens.
    2. Complete the following fields:
      • In the Package field, enter myTestApp.
      • In the Name field, enter MySample.
    3. Change the following settings:

      In the Which method stubs would you like to create section:

      • Select public static void (main[] string args).
      • Clear Inherited abstract methods.
    4. Click Finish.
    The workbench creates a class and opens the MySample.java file in the Java editor.
  2. Add the source code for the application.

    Replace text in the MySample.java file with the text in the Tutorial source code.

  3. Save the file.

You created a class and added Java code to a Java file.

In the Java editor, some errors will appear. The errors are resolved when you enable pureQuery support for the project.

Adding support for pureQuery to your Java project

You enable pureQuery support for your Java project.

Before you begin

You must have created a database connection and a Java project.

To add support for pureQuery to your Java project:

  1. In the Package Explorer, right-click the pq-tutorial project folder and click pureQuery > Add pureQuery Support.

    The pureQuery menu item is near the bottom of the menu.

  2. In the Add pureQuery Support wizard, select the SAMPLE connection from the Select Connection page and click Next.
  3. On the Add pureQuery Support page, accept the defaults and click Finish.
Your Java project is enabled with pureQuery support. The following actions occur:
  • The DB2 libraries that the Java application uses to connect to the SAMPLE database are added to the Java project's build path.
  • The pureQuery Runtime files pdq.jar and pdqmgmt.jar are added to the Java project.
  • The pureQuery configuration files that are used with the pureQuery Configure and StaticBinder utilities are added to the pureQueryFolder folder.
  • A connection to the SAMPLE database and the pureQuery project properties are added to the properties for your project.
When a Java application is enabled with pureQuery support, you can use the following pureQuery features including:
  • Enhanced SQL assistance with the Java editor, which includes SQL formatting, content assistance for SQL statements, and SQL templates.
  • The SQL outline view, which you can use to view the relationship between SQL statements, database objects, and Java source code.
  • Editors for the pureQueryXML file, the pureQuery StaticBinder options file, and the pureQuery configuration options file.
  • The pureQuery API.

Running your Java application

You update the connection information specified in the source code of your Java application and run the application.
  1. Update the database connection information in the Java code for your installation.
    1. Go to the method public static Connection getConnection().
    2. In the parameters for the getConnection method, update the host, port number, database, user name, and password to match the connection to the SAMPLE database on your computer.
      In the following sample connection, the host, port number, and database are localhost:50000/SAMPLE and the user name and password are testuser1 and mypwd:
      jdbcCon=DriverManager.getConnection(
      "jdbc:db2://localhost:50000/SAMPLE:" +
      "retrieveMessagesFromServerOnGetMessage=true;",
      "testuser1", "mypwd");
      Note: In this sample application, the connection information including the user name and password are included in the application to be easily updated. In a production application, the user name and password would not be included with the application.
  2. Save the file.
  3. Run the application.

    In the Package Explorer, right-click the MySample.java file, and click Run As > Java application.

    Output displays in the Console window. The output is similar to the following example:
     =======
    IBM Optim pureQuery Runtime 2.2.1.0 build 2.25.63
     =======
    case 0
    case 0 - employee count: 36
    Finished case 0

Lesson checkpoint

In this lesson, you used the workbench to create a Java application that connects to the SAMPLE DB2 database and tested the application.

You learned how to perform the following tasks:

  • Create a Java project.
  • Enable a Java project with pureQuery support.
  • Run a Java application.
< Previous | Next >

Feedback