WAS v8.5 > Deploy applications > Deploy applications to the Liberty profile > Deploy data access applications to the Liberty profile
Deploying an existing JDBC application to the Liberty profile
We can take an existing application that uses Java™ Database Connectivity (JDBC) and a data source, and deploy the application to a server. We 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 server.xml. You must also add code that tells the server the JDBC driver location and specifies properties 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.
- Create a server.
- Add the jdbc-4.0 and the servlet-3.0 Liberty features to server.xml.
- Add code to server.xml to specify the database type and the data source location.
For example:
For information about other options for coding data source definitions, see Using Ref tags in configuration files.<jdbcDriver 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>
- Optional: Enable JDBC tracing.
- 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(); }
- Add the application to the server.
- If it is not already running, start the server.
- Optional: Test 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
Parent topic: Deploy data access applications to the Liberty profile
Related
Configuring connection pooling for database connections
|