기존의 JDBC 애플리케이션을 Liberty 프로파일에 배치

JDBC(Java™ Database Connectivity) 및 데이터 소스를 사용하는 기존 애플리케이션을 가져와 서버에 배치할 수 있습니다.

이 태스크 정보

기존의 JDBC 애플리케이션을 가져와서 이를 Liberty 프로파일에 배치할 수 있습니다. 이 배치를 수행하려면 jdbc-4.0 Liberty 기능을 server.xml 파일에 추가하십시오. 또한 JDBC 드라이버 위치를 서버에 알리고 JDBC 드라이버가 데이터베이스에 연결할 때 사용하는 특성을 지정하는 코드도 추가해야 합니다.

이 예제에서는 사용자의 서블릿 애플리케이션을 확장하거나 여기에서 제공된 것을 사용하여 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. JDBC 애플리케이션에 몇 가지 SQL 작성, 읽기, 업데이트 및 삭제 명령을 추가하여 데이터베이스에 대한 대화식 작업을 테스트하십시오.
    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();
                 } 
               }
           }
        }
    }
  6. 애플리케이션을 컴파일하십시오.

    여기서 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에서의 예제:
    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 애플리케이션이 작동하는지 확인하십시오.
    이 예제의 경우 다음 URL로 이동하십시오.
    http://localhost:9080/HelloWorldApp/HelloWorld

    포트 9080은 Liberty 서버에서 사용되는 기본 HTTP 포트입니다. server.xml 파일을 조사하여 사용자의 서버가 어느 HTTP 포트에 설정되는지 확인할 수 있습니다.

    이 예제의 브라우저 출력은 다음과 같습니다.

    Hello World Liberty Profile
    The county for myHomeCity is myHomeCounty

주제의 유형을 표시하는 아이콘 태스크 주제

Information Center 이용 약관 | 피드백


시간소인 아이콘 마지막 업데이트 날짜: Wednesday, 2 September 2015
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=phil&product=was-libcore-mp&topic=twlp_dep_jdbc
파일 이름: twlp_dep_jdbc.html