系列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


转载于:https://my.oschina.net/uniquejava/blog/283266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值