【IT168 技术文章】
IBM WebSphere Application Server (WebSphere应用服务器)是一个Web应用服务器(内含IBM Http Server),它本质上是适合于servlet的Web服务器插件,提供了增强的Servlet API和Servlets管理工具,并集成了JSP技术和数据库连接技术。
由于项目的需要,需要将某一部分的web应用移植到WebSphere Application Server上。这几天我作为项目经理对这部分的工作进行了初步的探索。
我先在www.ibm.com中下载了WebSphere Application Server6.0的试用版。在安装了WebSphere以后,打开Jbuilder X发现这个版本的Jbuilder不支持WebSphere Application Server6.0,因此及其郁闷的安装了Jbuilder2006。
现在可以确认的开发环境如下:
系统平台:Windows2000 Server
数据库:Oracle9I
应用服务器:WebSphere Application Server6.0 for windows
编程环境:Jbuilder2006
因为如果进行Web项目的移植,最先需要确认其对数据库访问的正确性。因此需要在安装的WebSphere Application Server先设置一个数据库连接池,以保证Web应用程序对数据库访问的可行性。需要注意的是在6.0版本中需要在数据源中为该数据源设置一个J2EE 连接器体系结构(J2C)认证。在设置了连接数据源的用户名和密码以后,在数据源的组件管理的认证别名中选择刚才设置的认证。在以上配置工作完成以后就可以对该数据连接池进行连接测试了。
在数据连接池测试成功以后,就需要考虑在Web应用程序如何取调用该数据连接池进行数据库连接了。访问应用服务器的数据连接池肯定是通过JNDI进行访问。
主要程序如下:
Connection conn;
Statement stmt = null;
DataSource ds = null;
ResultSet rs = null;
try {
ht.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
ht.put(Context.PROVIDER_URL, "iiop://192.168.1.224:2809/");
Context ctx = new InitialContext(ht);
Object obj = ctx.lookup("Pubtest");
ds = (DataSource) obj;
conn = ds.getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM EPUB.SYS_LOGIN_USER");
while (rs.next()) {
out.println(rs.getString("USER_CODE"));
out.println(rs.getString("PWD"));
out.println(rs.getDate("UP_DATE"));
out.println(rs.getString("OPR_CODE"));
out.println(rs.getString("NOTE"));
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
if (!conn.isClosed()) {
conn.close();
}
}
}
catch (Exception ex) {
// System.err.println("建立数据库连接错误:" + ex.getMessage());
out.print("建立数据库连接错误:" + ex.getMessage()); //输出到客户端
ex.printStackTrace();
}
在程序编译成功以后,执行出现如下错误:
javax.naming.NamingException: Failed to initialize the ORB [Root exception is java.lang.ClassCastException: com.sun.corba.se.impl.orb.ORBImpl]
at com.ibm.ws.naming.util.Helpers.getOrb(Helpers.java:294)
at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:373)建立数据库连接错误:Failed to initialize the ORB
at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:112)
at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:422)
at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:143)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at testwebsphere.test.main(test.java:88)
……………