請問怎麼樣在Application裡呼叫Tomcat5.0.28建立的連線池?

pbabi發表於2006-03-24
我用的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"

相關文章