请问怎么样在Application里调用Tomcat5.0.28创建的连接池?

06-03-24 pbabi
我用的TOMCAT+SQL2000连接池,直接通过JSP访问成功。

现在想在APPLICATION里调试BEAN,怎么才能做到让APPLICATION能访问TOMCAT的连接池?

如果能告知其他更好调试BEAN的方式也可以。非常感谢!!

以下是我的代码,但是运行出错。

package day;

import java.sql.*;

import java.util.*;

import javax.naming.Context;

import javax.sql.DataSource;

import javax.naming.InitialContext;

public class Test{

public Test(){}

public static void main(String[] args) throws Exception {

Connection myConn = null;

DataSource ds = null;

Properties jndiEnv = new Properties();

jndiEnv.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");

jndiEnv.setProperty(Context.PROVIDER_URL, "jdbc:microsoft:sqlserver://192.168.0.131:1433;User=sa;Password=2;DatabaseName=pubs");

Context ctx= new InitialContext(jndiEnv);

ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/mssql");

myConn = ds.getConnection();

Statement stmt = myConn.createStatement();

ResultSet rs = stmt.getResultSet();

rs = stmt.executeQuery("select fname from employee");

System.out.print("connect ok");

while (rs.next()) {

System.out.print(rs.getString("fname"));

}

}

}

错误

javax.naming.NameNotFoundException: Name java:comp is not bound in this Context

at org.apache.naming.NamingContext.lookup(NamingContext.java:768)

at org.apache.naming.NamingContext.lookup(NamingContext.java:151)

at javax.naming.InitialContext.lookup(InitialContext.java:347)

at daytoday.Test3.main(Test3.java:28)

Exception in thread "main"

banq
2006-03-28 14:44
这需要服务器提供JNDI Server功能,你通过RMI调用,估计Tomcat无此功能,换用JBoss

weistars
2006-04-01 20:39
TOMCAT是支持此功能的,5.0以后的版本是支持的

猜你喜欢