将现有 JDBC 应用程序部署到 Liberty 概要文件
您可以采用某一使用 Java™ 数据库连接 (JDBC) 和数据源的现有应用程序,然后将该应用程序部署到服务器。
关于此任务
可以获取现有 JDBC 应用程序并将其部署到 Liberty 概要文件。要完成此部署,请将 jdbc-4.0 Liberty 功能部件添加到 server.xml 文件。还必须添加代码,用以将 JDBC 驱动程序位置告知给服务器,以及指定 JDBC 驱动程序用于连接至数据库的属性。
在此示例中,您可以扩展您的 servlet 应用程序,或使用此处提供的 servlet 应用程序来测试通过 JDBC 驱动程序使用的互操作性是否按预期工作。
过程
- 创建服务器。
- 启动服务器。
- 将 jdbc-4.0 和 servlet-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.
- 在 server.xml 文件中指定数据库类型和数据源位置。
其中,path_to_derby 是 derby 在操作系统上的安装位置,lib 是 derby.jar 所在的文件夹,data/exampleDB 是创建的目录(如果不存在此目录)。
例如:
有关用于编写数据源定义代码的其他选项的信息,请参阅在配置文件中使用 Ref 标记。<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>
- 将一些 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 Profile</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'"); would 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(); } } } } }
- 编译应用程序。
其中,path_to_liberty 是 Liberty 在操作系统上的安装位置,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 上的示例:如果无法识别 javac 命令,请确保在操作系统的 PATH 环境变量中具有 Java bin 目录。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
- 将应用程序添加到服务器。 在此示例中,JDBC 应用程序放置于服务器的 dropins 目录中:
...\dropins\HelloWorldApp.war\WEB-INF\classes\wasdev\HelloWorld
wasdev 目录使用 HelloWorld.java 中所使用的相同软件包名称。
- 检查 JDBC 应用程序是否正在运行。 针对此示例,转至以下 URL:
http://localhost:9080/HelloWorldApp/HelloWorld
端口 9080 是 Liberty 服务器使用的缺省 HTTP 端口。 您可通过查看 server.xml 文件检查服务器设置为使用哪个 HTTP 端口。
在此示例中,浏览器上的输出类似于以下内容:
Hello World Liberty Profile The county for myHomeCity is myHomeCounty
相关任务:

信息中心的条款和条件 | 反馈

http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_dep_jdbc
文件名:twlp_dep_jdbc.html