东方通tongweb使用jndi访问数据源以及达梦数据测试

项目需要,研究国产中间件东方通tongWeb和国产数据库达梦数据库dmdbs7

测试tongweb6.0的jndi使用方法

环境如下:

jdk1.7,TongWeb6.0,达梦数据库dm7,eclipse

目标:简单测试数据库连通并输出到网页

前期工作,部署tongweb6.0服务到eclipse,这个自己摸索,或者看文档

访问http://localhost:9060/console打开tongweb控制台》jdbc配置》创建 jdbc  

jdbc名称叫dm15,数据源选择达梦,测试连通成功


tongweb会自己在jndi里面生成一个dm15的jndi连接



1在eclipse中新建一个maven项目,新建一个servlet,

package com.nanjing.dameng;

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

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

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	static String sqlQuery = "select CITY_ID,CITY_NAME,REGION_ID from CITY;";


	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method success</br>");

		Context ctx;
		try {
			ctx = new InitialContext();
			String strLookup = "java:comp/env/dm15";
			DataSource ds = (DataSource) ctx.lookup(strLookup);
			Connection con = ds.getConnection();
			PreparedStatement pstmt = con.prepareStatement(sqlQuery);
			ResultSet rs = pstmt.executeQuery();
			out.print("jndi connection success");
			while (rs.next()) {
				out.println(rs.getString(1) + "/" + rs.getString(2)+ "/</br>" );
			
			}
			System.out.println("查询结束!");
			pstmt.close();
			if (con != null) {
				out.print("jndi connection close");
				con.close();
			} 
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			
		}

		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}



2pom中引入达梦数据库jdbc的jar包,servlet包


3 web.xml配置如下

    

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
 	 <resource-ref>
  	<description>DB Connection</description>
	<res-ref-name>dm15</res-ref-name>
	<res-type>javax.sql.DataSource</res-type>
	<res-auth>Container</res-auth>
         </resource-ref>
	
  <servlet>
     <!-- 类名 -->
    <servlet-name>do1</servlet-name>
    <!-- 所在的包 -->
    <servlet-class>com.nanjing.dameng.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>do1</servlet-name>
    <!-- 访问的网址 -->
    <url-pattern>/servlet/do1</url-pattern>
    </servlet-mapping>
    
    
    
   
</web-app>


4访问本地地址http://localhost:28080/demo01/servlet/do1

显示成功

This is class com.wisdom.dameng.TestServlet, using the GET method success
BJ /北京/
SJZ/石家庄/
SH /上海/
NJ /南京/


备注:

1不需要  contet.xml,如果按照tomcat的jndi方式部署,我没有成功,各种object和name找不到,我猜测tongweb找不到context文件或者有自己的文件方式

2 tongweb适用jndi其实就是3个步骤,

 a在控制台新增一个数据库连接池dm15,tongweb自己就自动生成一个jndi 名字也叫dm15

 b在web.xml中引入 <res-ref-name>dm15</res-ref-name>

 c在调用的ctx.lookup("java:comp/env/dm15")





你可能感兴趣的:(JNDI,达梦数据库,东方通,tongweb)