连接池的问题

losky 2006-08-06 10:19:04
tomcat5.5
web.xml里面的内容
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app 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"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<!-- JSPC servlet mappings start -->

<servlet>
<servlet-name>users.hello</servlet-name>
<servlet-class>users.hello</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>users.hello</servlet-name>
<url-pattern>/hello.jsp</url-pattern>
</servlet-mapping>
<resource-ref>
<description>datasource</description>
<res-ref-name>jdbc/text</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>



<!-- JSPC servlet mappings end -->

</web-app>

---------------------------------------------------------------------------------------
server.xml
----------------------------------------------------------------------------------------
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.core.AprLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.storeconfig.StoreConfigLifecycleListener"/>
<GlobalNamingResources>
<Environment name="simpleValue" type="java.lang.Integer" value="30"/>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
<Resource
name="jdbc/text"
type="javax.sql.DataSource"
password="191032"
driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=text"
maxActive="20"/>
</GlobalNamingResources>
<Service name="Catalina">
<Connector
port="8080"
maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" />

<Connector port="8009"
enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
<Engine name="Catalina" defaultHost="localhost">



<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false"
xmlNamespaceAware="false">
<Context path="/dd" reloadable="true" docBase="C:\Documents and Settings\Administrator\workspace\dd" workDir="C:\Documents and Settings\Administrator\workspace\dd\work" />
<Context path="/ss" reloadable="true" docBase="D:\jsp\ss" workDir="D:\jsp\ss\work"/>

</Host>

</Engine>

</Service>

</Server>

--------------------------------------------------------------------------
错误类型
------------------------------------
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at users.user.getConnection(user.java:56)
at users.user.checkteacherid(user.java:74)
at users.user.main(user.java:17)
...全文
393 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
angelinsky 2006-08-08
  • 打赏
  • 举报
回复
在Context context = (Context)initcontext.lookup("java:comp/env");最后加一个“/”
写成这样lookup("java:comp/env/")
试一下
losky 2006-08-07
  • 打赏
  • 举报
回复
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312" %>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@ page import="javax.naming.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Insert title here</title>
</head>
<body>
<%
try{
Context initcontext=new InitialContext();
Context context=(Context)initcontext.lookup("java:comp/env");
DataSource ds=(DataSource)context.lookup("jdbc/textdb");
Connection conn=ds.getConnection();
out.print("ok");
}
catch(Exception ex)
{
out.print("false");

}
%>
</body>
</html>
angelinsky 2006-08-06
  • 打赏
  • 举报
回复
从错误提示看,是初始化CONTEXT失败,可能是没导javax.naming.* ,你先试一下,最好把你怎么调的写出来。
losky 2006-08-06
  • 打赏
  • 举报
回复
不用连接池可以正常访问数据库

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧