Deploying an existing JDBC application to Liberty
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 Liberty. To complete this deployment, 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.
In this example, you can extend your servlet application, or use the one provided here to test the interactivity that is used through the JDBC driver is working as expected.
Procedure
- Create a server.
- Start the server.
- Add the jdbc-4.0 and
the servlet-3.0 Liberty features to the server.xml file.
<server> <featureManager> <feature>jdbc-4.0</feature> <feature>servlet-3.0</feature> </featureManager> </server>
To check that the server is working and that the features are enabled successfully, see the console.log file, which is stored in the logs directory of the server. You can view it using any text editor. Here is an example:
[AUDIT ] CWWKF0012I: The server installed the following features: [jdbc-4.0, jndi-1.0]. [AUDIT ] CWWKF0008I: Feature update completed in 0.326 seconds.
- Specify the database type and the data source location
in the server.xml file.
Where path_to_derby is the location where derby is installed on your operating system, lib is the folder where derby.jar is located, and data/exampleDB is the directory that is created if it does not exist.
For example:
For information about other options for coding data source definitions, see Using Ref tags in configuration files.<jdbcDriver id="DerbyEmbedded" libraryRef="DerbyLib"/> <library id="DerbyLib"> <fileset dir="C:/path_to_derby/lib" includes="derby.jar"/> </library> <dataSource id="ds1" jndiName="jdbc/exampleDS" jdbcDriverRef="DerbyEmbedded"> <properties.derby.embedded databaseName="C:/path_to_derby/data/exampleDB" createDatabase="create" /> </dataSource>
- Add some SQL create, read, update, and delete statements
to your JDBC application to test the interactivity with the database.
package wasdev; import java.io.*; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.annotation.Resource; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.WebServlet; import javax.sql.DataSource; @WebServlet("/HelloWorld") public class HelloWorld extends HttpServlet { @Resource(name = "jdbc/exampleDS") private DataSource ds1; private Connection con = null; private static final long serialVersionUID = 1L; public HelloWorld() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<H1>Hello World Liberty</H1>\n"); try { con = ds1.getConnection(); Statement stmt = null; 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. out.println("The county for myHomeCity is " + result.getString(1)); // drop the table to clean up and to be able to rerun the test. stmt.executeUpdate("drop table cities"); } catch (SQLException e) { e.printStackTrace(); } finally { if (con != null){ try{ con.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
- Compile your application.
Where path_to_liberty is the location you installed Liberty on your operating system, and path_to_app is the location of the Java file of the application you want to compile.
Example on Windows:C:\> javac -cp path_to_liberty\wlp\dev\api\spec\com.ibm.ws.javaee.servlet.3.0_1.0.1.jar path_to_App\HelloWorld.java
Example on Linux:If the javac command is not recognized, ensure that you have the Java bin directory in the PATH environment variable of your operating system.mo@machine01:~> javac -cp path_to_liberty/wlp/dev/api/spec/com.ibm.ws.javaee.servlet.3.0_1.0.1.jar path_to_App/HelloWorld.java
- Add the application to the server. In this example, the JDBC application is put in the dropins directory of the server:
...\dropins\HelloWorldApp.war\WEB-INF\classes\wasdev\HelloWorld
The wasdev directory uses the same package name that is used in HelloWorld.java.
- Check that your JDBC application is working. For this example, go to this URL:
http://localhost:9080/HelloWorldApp/HelloWorld
Port 9080 is the default HTTP port that is used by the Liberty server. You can check which HTTP port your server is set on by looking in the server.xml file.
The output on the browser for this example looks like:
Hello World Liberty The county for myHomeCity is myHomeCounty


http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-libcore-mp&topic=twlp_dep_jdbc
File name: twlp_dep_jdbc.html