将现有 JDBC 应用程序部署到 Liberty

您可以采用某一使用 Java™ 数据库连接 (JDBC) 和数据源的现有应用程序,然后将该应用程序部署到服务器。

关于此任务

可以获取现有 JDBC 应用程序并将其部署到 Liberty。要完成此部署,请将 Liberty JDBC 功能部件添加至 server.xml 文件。另外,还必须添加代码,这些代码用于将 JDBC 驱动程序位置告知给服务器,以及指定 JDBC 驱动程序用于连接至数据库的属性。

以下示例使用 jdbc-4.0 功能部件。在此示例中,您可以扩展 Servlet 应用程序,或者使用此处所提供的 Servlet 应用程序,以测试通过 JDBC 驱动程序使用的交互是否在按预期运行。

过程

  1. 创建服务器
  2. 启动服务器
  3. jdbc-4.0servlet-3.0 Liberty 功能部件添加到 server.xml 文件
    <server>
       <featureManager>
           <feature>jdbc-4.0</feature>
           <feature>servlet-3.0</feature>
       </featureManager></server>

    要检查服务器是否正常运行并且已成功启用功能部件,请参阅 console.log 文件,该文件存储在服务器的 logs 目录中。可以使用任何文本编辑器来查看该文件。以下是一个示例:

    [AUDIT   ] CWWKF0012I: The server installed the following features: [jdbc-4.0, jndi-1.0].
    [AUDIT   ] CWWKF0008I: Feature update completed in 0.326 seconds.
  4. server.xml 文件中指定数据库类型和数据源位置。

    其中 path_to_derby 是 derby 在操作系统上的安装位置,libderby.jar 所在的文件夹,data/exampleDB 是所创建的目录(如果不存在该目录)。

    例如:
    <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>
    有关用于编写数据源定义代码的其他选项的信息,请参阅在配置文件中使用 Ref 标记
  5. 将一些 SQL create、read、update 和 delete 语句添加至 JDBC 应用程序以测试与数据库的交互性。
    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();
                 } 
               }
           }
        }
    }
  6. 编译应用程序。

    其中,path_to_libertyLiberty 在操作系统上的安装位置,path_to_app 是要编译的应用程序的 Java 文件位置。

    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
    Linux 上的示例:
    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
    如果不识别 javac 命令,请确保操作系统的 PATH 环境变量中具有 Java bin 目录。
  7. 将应用程序添加到服务器
    在此示例中,JDBC 应用程序放入服务器的 dropins 目录中:
    ...\dropins\HelloWorldApp.war\WEB-INF\classes\wasdev\HelloWorld

    wasdev 目录使用 HelloWorld.java 中所使用的同一软件包名称。

  8. 请检查 JDBC 应用程序是否正在运行。

    对于此示例,请访问 http://localhost:9080/HelloWorldApp/HelloWorld URL。

    端口 9080 是 Liberty 服务器使用的缺省 HTTP 端口。 您可以通过查看 server.xml 文件来检查服务器设置在哪个 HTTP 端口上。

    在此示例中,浏览器上的输出类似于以下内容:

    Hello World Liberty
    The county for myHomeCity is myHomeCounty

用于指示主题类型的图标 任务主题

文件名:twlp_dep_jdbc.html