系列3:WAS Liberty Profile hello mysql jdbc

新建 <wlp_home>\usr\shared\resources\mysql,把mysql的jar包扔进去
在server.xml中可以通过${shared.resource.dir}指代<wlp_home>\usr\shared\resources\
见http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_setup.html

server.xml中的各种ref(各种egg hurt的配置)
http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_setup_reftags.html

最终我的看起来很晦涩的配置如下:

<server description="new server">

	<!-- Enable features -->
	<featureManager>
		<feature>servlet-3.0</feature>
		<feature>jdbc-4.0</feature>
	</featureManager>

	<dataSource id="blogDS" jndiName="jdbc/blogDS" connectionManagerRef="mysqlPool" jdbcDriverRef="mysqlDriver">
		<properties databaseName="test" serverName="localhost" portNumber="3306" />
	</dataSource>

	<connectionManager id="mysqlPool" maxPoolSize="10" />

	<jdbcDriver id="mysqlDriver" libraryRef="mysqlLib" />
	<library id="mysqlLib" filesetRef="mysqlFileset" />
	<fileset id="mysqlFileset" dir="${shared.resource.dir}/mysql" includes="*.jar" />

	<webApplication name="helloworld" location="helloworld.war" />

	<httpEndpoint id="defaultHttpEndpoint" host="localhost" httpPort="9080" httpsPort="9443" />
</server>

这里有各种数据连接的参考配置:
http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_dep_configuring_ds.html


修改HelloServlet.java的代码如下
package test;

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

import javax.annotation.Resource;
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;

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {

	@Resource(name = "jdbc/blogDS")
	private DataSource ds;

	private Connection con;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<H1>Hello World Liberty Profile</H1>\n");
		try {
			con = ds.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();
				}
			}
		}

	}
}



这段java代码来自于http://www-01.ibm.com/support/knowledgecenter/api/content/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_dep_jdbc.html, 看起来没有关闭statement和resultset(有内存泄漏?)

同样使用如下命令编译即可
javac -cp C:\IBM\was855nalp\dev\api\spec\com.ibm.ws.javaee.servlet.3.0_1.0.1.jar HelloServlet.java


你可能感兴趣的:(系列3:WAS Liberty Profile hello mysql jdbc)