Deploying an existing JDBC application to the Liberty profile

You can take an existing application that uses Java™ Database Connectivity (JDBC) and a data source, and deploy the application to a server.

About this task

You can take an existing JDBC application and deploy it to the Liberty profile. To do this, you add the jdbc-4.0 Liberty feature to the server.xml file. You must also add code that tells the server the JDBC driver location and specifies properties that the JDBC driver uses to connect to the database.

This example uses the ImpactWeb sample application. This application includes a servlet called WorkingServlet. In this example, you extend the servlet with code that tests that the JDBC application is working as expected.

Procedure

  1. Create a server.
  2. Add the jdbc-4.0 and the servlet-3.0 Liberty features to the server.xml file.
  3. Add code to the server.xml file to specify the database type and the data source location.
    For example:
    <jdbcDriver id="DerbyEmbedded" libraryRef="DerbyLib"/>
    <library id="DerbyLib">
      <fileset dir="C:/myDerbyLocation/lib" includes="derby.jar"/>
    </library>
    <dataSource id="ds1" jndiName="jdbc/exampleDS" jdbcDriverRef="DerbyEmbedded">
      <properties.derby.embedded
        databaseName="C:/myDerbyLocation/data/exampleDB"
        createDatabase="create"
      />
    </dataSource>
    For information about other options for coding data source definitions, see Using Ref tags in configuration files.
  4. Optional: Enable JDBC tracing.
  5. Modify the WorkingServlet.java servlet.
    For example, add the following code:
    @Resource(name = "jdbc/exampleDS")
    DataSource ds1;
      Connection con = ds1.getConnection();
      Statement stmt = null;
      try {
        stmt = con.createStatement();
        // create a table
        stmt.executeUpdate("create table cities 
            (name varchar(50) not null primary key, population int, county varchar(30))");
        // insert a test record
        stmt.executeUpdate("insert into cities values ('myHomeCity', 106769, 'myHomeCounty')");
        // select a record
        ResultSet result = stmt.executeQuery("select county from cities where name='myHomeCity'");
        result.next();
    	    // display the county information for the city.
        System.out.println("The county for myHomeCity is " + result.getString(1));
      } 
      catch (SQLException e) {
        e.printStackTrace();
      } 
      finally {
        try {
          // drop the table to clean up and to be able to rerun the test.
          stmt.executeUpdate("drop table cities");
        } 
        catch (SQLException e) {
          e.printStackTrace();
        }				
        con.close();
      }
  6. Add the application to the server.
  7. If it is not already running, start the server.
  8. Optional: Test that the JDBC application is working as expected.
    For example, run the modified WorkingServlet.java servlet. You should see the following console output:
    [AUDIT   ] CWWKZ0001I: The application ImpactWeb has started successfully.
    [AUDIT   ] CWWKD0000I: The dataSource ds1 is available as jdbc/exampleDB.
    [AUDIT   ] CWWKD0000I: The jdbcDriver DerbyEmbedded is available.
    The county for myHomeCity is myHomeCounty

Icon that indicates the type of topic Task topic

Terms and conditions for information centers | Feedback


Timestamp icon Last updated: Wednesday, 22 May 2013
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-nd-mp&topic=twlp_dep_jdbc
File name: twlp_dep_jdbc.html