LibertyでJDBCの設定してみました

## LibertyでJDBCの設定

### server.xmlファイルに以下の文字列を追加

```
<library id="DB2JCCLib">
    <fileset dir="/opt/wlp/usr/servers/server1" includes="db2jcc4.jar db2jcc_license_cisuz.jar"/>
    </library>

    <dataSource id="DefaultDataSource" jndiName="jdbc/db2" connectionSharing="MatchCurrentState"
            isolationLevel="TRANSACTION_READ_COMMITTED" statementCacheSize="20">
    <connectionManager maxPoolSize="20" minPoolSize="5"
                       connectionTimeout="10s" agedTimeout="30m"/>
    <jdbcDriver libraryRef="DB2JCCLib"/>
    <properties.db2.jcc databaseName="XXX" serverName="db2" portNumber="50000"
                        currentLockTimeout="30s" user="db2inst1" password="xxxx"/>
    </dataSource>
```

### サンプルアプリのコード

```
package connect;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Servlet implementation class Connect3
 */
@WebServlet("/Connect4")
public class Connect4 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Connect4() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		// TODO Auto-generated method stub
				response.getWriter().append("Served at: ").append(request.getContextPath());
				
				// ContentTypeを設定
		        response.setContentType("text/html; charset=UTF-8");
		        // 出力用PrintWriterを取得
		        PrintWriter out = response.getWriter();
		        // 出力
		        out.println("<html>");
		        out.println("<head>");
		        out.println("<title>DB2 Connect Sample</title>");
		        out.println("</head>");
		        out.println("<body>");
		        out.println("<table border=\"1\">");
		        out.println("<tr><td>ID</td><td>DOC</td></tr>");
	    
		        try {
		            Context ctx = new InitialContext();
		            //コネクションプールからデータソースを取得
		            DataSource ds = (DataSource)ctx.lookup("jdbc/db2");
		            Connection con = ds.getConnection();
		            Statement stmt = con.createStatement();
		            
		            //rssテーブルを検索し、結果を表示
		            ResultSet rs = stmt.executeQuery("select id,name from COUNTRIES");
		            while(rs.next()) {
		                out.println("<tr><td>");
		                out.println(rs.getString("id"));
		                out.println("</td><td>");
		                out.println(rs.getString("name"));
		                out.println("</td></tr>");
		            }
		            rs.close();
		            stmt.close();
		            con.close();
		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		        
		        out.println("</table>");
		        out.println("</body>");
		        out.println("</html>");

		    }
	    
		
		


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

```