將現有的 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</H1>\n"); try { con = ds1.getConnection(); Statement stmt = null; stmt = con.createStatement(); // 建立表格 stmt.executeUpdate("create table cities (name varchar(50) not null primary key, population int, county varchar(30))"); // 插入測試記錄 stmt.executeUpdate("insert into cities values ('myHomeCity', 106769, 'myHomeCounty')"); // 選取記錄 ResultSet result = stmt.executeQuery("select county from cities where name='myHomeCity'"); result.next(); // 顯示城市的國家資訊 out.println("The county for myHomeCity is " + result.getString(1)); // 捨棄表格以進行清除,以及重新執行測試。 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 The county for myHomeCity is myHomeCounty


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