你JNDI数据源配好了吗?再仔细检查下自己的关于连接数据库信息的属性
解决方案 »
- java字符串处理小工具(新手使用) 1/2
- spring问题
- 搞怪,搞笑,又搞我的hibernate hql语句查询问题
- 此题非孔明转世能解也!
- netbean开发j2ee需要安装什么,如何配置?
- 请问为什么我的javax.ejb.*是有的,javax.ejb.Remote确没有呢
- 很奇怪的问题,java的程序,在台式机上按TAB键是按顺序走,但是在笔记本上却不是,什么原因,谢谢!
- 如何把Elemnet的对象转换成String,输出到文本框?(新手莫笑)
- struts标签的问题
- [求助]怎样设置状态会话Bean的Bean收集池大小
- JSP+JavaBean开发WAP程序(与开发WEB类似),求助
- 研究各大公司(SUN,微软,IBM,HP)身份验证和单点登录(SSO)系统得群,希望有兴趣得人都来研究一下!qq群号码:9993425
(url与driverClassName相对应)
...
<GlobalNamingResources>
<Environment
name="simpleValue"
type="java.lang.Integer"
value="30"/>
<Resource
auth="Container"
description="User database that can be updated and saved"
name="UserDatabase"
type="org.apache.catalina.UserDatabase"
pathname="conf/tomcat-users.xml"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"/>
<Resource
name="jdbc/sqlserver"
type="javax.sql.DataSource"
password="111111"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
maxIdle="2"
maxWait="5000"
username="sa"
url="jdbc:jtds:sqlserver://localhost:1433/demo"
maxActive="4"/>
</GlobalNamingResources>
...(2)conf/context.xml含以下标记<Context>
...
<ResourceLink name="jdbc/sqlserver" global="jdbc/sqlserver" type="javax.sql.DataSource"/>
</Context>(3)在webapps下的项目所在的目录的 WEB-INF/web.xml 里含以下标记<web-app version="2.4">
...
<resource-ref>
<description>MSSQLSERVER Datasource</description>
<res-ref-name>jdbc/sqlserver</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>(4)编写测试 test.jsp
...
<%@ page import="javax.naming.*" %>
<%@ page import="javax.sql.*" %>
<%
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/sqlserver");
Connection conn = ds.getConnection();
if(conn!=null){
out.print("Connection pool success<br>");
try{
PreparedStatement ps = conn.prepareStatement("select * from table1");
ResultSet rs = ps.executeQuery();
while(rs.next()){
out.print(rs.getString(2)+"<br>");
}
}
catch(Exception e){
out.print("-------- Error: "+e+" -------------<br>");
}
finally{
conn.close(); //释放连接
}
}
%>(5)重启动tomcat,MSSQLSERVER ,测试通过。