程序验证
编写如下Serverlet,到处war 包,部署到websphere 上。
package com.gbase.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
GBase 8a 程序员手册JDBC 篇
南大通用数据技术股份有限公司
- 75 -
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
public class WebshpereJNDITest extends HttpServlet {
/**
*
*/
private static final long serialVersionUID =
8570136076952105578L;
/**
* Constructor of the object.
*/
public WebshpereJNDITest() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
GBase 8a 程序员手册JDBC 篇
- 76 -
南大通用数据技术股份有限公司
*
This
method
is
called
when
a
form
has
its
tag
value
method
equals
to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
System.out.println("come");
Context initCtx = new InitialContext();
// 获得连接池
DataSource ds = (DataSource) initCtx
.lookup("ds/8acluster");
Map cacheMap = new HashMap();
// 创建连接
conn = ds.getConnection();
System.out.println(conn.getMetaData().getURL());
if (conn != null) {
out.println("The Gbase connection is ok!!");
System.out.println("ok");
} else {
out.println("The Gbase connection occur error");
System.out.println("error");
}
// 使用连接创建Statement 对象执行SQL 语句
GBase 8a 程序员手册JDBC 篇
南大通用数据技术股份有限公司
- 77 -
Statement st = conn.createStatement();
ResultSet rs1 = st
.executeQuery("select * from information_schema.TABLES");
ResultSetMetaData rsm = rs1.getMetaData();
// 获得执行结果,输出结果
while (rs1.next()) {
for (int i = 0; i < rsm.getColumnCount(); i++) {
out.println(rsm.getColumnName(i + 1)+ "= "+
rs1.getString(i + 1));
out.println("
");
}
out.println("+++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++
");
}
} catch (Exception e) {
e.printStackTrace();
out.println(e);
} finally {
out.flush();
out.close();
if (conn == null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public String getCurrentIp(String url) {
return url.substring(url.indexOf("currentIp"),
url.length());
}
/**
* The doPost method of the servlet.
GBase 8a 程序员手册JDBC 篇
- 78 -
南大通用数据技术股份有限公司
*
*
This
method
is
called
when
a
form
has
its
tag
value
method
equals
to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
web.xml 配置如下:
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
GBase 8a 程序员手册JDBC 篇
南大通用数据技术股份有限公司
- 79 -
在部署测试war 包是 “上下文根”使用 “/JNDITest”
部署成功后使用如下URL 访问:
http://localhost:9082/JDNITest/websphereJNDITest
该测试用例会打印出目标数据库中information_schema.TABLES 的内容。