我用的是MyEclipse 6.5M1,调试使用的是他自带的tomcat服务器。应该是6.0吧。数据库是mssql2000
我想不改变tomcat配置,使用连接池,所以为项目做了一个局部的数据源。
我在项目的 META-INF 目录下新建了 context.xml ,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mssql"
auth="Container"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
type="javax.sql.DataSource"
maxActive="50"
maxIdle="10"
maxWait="10000"
username="sa"
password="admin"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=bbs"
/>
</Context>在WEB-INF目录下的web.xml配置内容如下:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mssql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
包含于<web-app>标签中调试代码如下:
Connection conn=null;
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/mssql");
System.out.println("获得连接池对象!");
conn = ds.getConnection();
if(conn != null){
System.out.println("获得连接");
}else{
System.out.println("没有获得连接");
}
}catch(Exception ex){
ex.printStackTrace();
}
return conn;安装好sql2000驱动后我把lib下的驱动包考到了项目的WEB-INF/lib下,但是连接数据库是总是报错:
Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver'。
说找不到驱动,但是我用java直接连接mssql就可以链接到数据库。
public Connection getConnection(){
Connection con = null;
if(con==null){
try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动程序注册成功");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bbs","sa","admin");
System.out.println("数据库连接成功");
}catch(Exception e){
System.out.println("DBHelp:getConnection()");
System.out.println("数据库连接失败");
}
}
return con;
}这是为什么呢?
我想不改变tomcat配置,使用连接池,所以为项目做了一个局部的数据源。
我在项目的 META-INF 目录下新建了 context.xml ,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mssql"
auth="Container"
removeAbandoned="true"
removeAbandonedTimeout="60"
logAbandoned="true"
type="javax.sql.DataSource"
maxActive="50"
maxIdle="10"
maxWait="10000"
username="sa"
password="admin"
driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
url="jdbc:sqlserver://localhost:1433;DatabaseName=bbs"
/>
</Context>在WEB-INF目录下的web.xml配置内容如下:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mssql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
包含于<web-app>标签中调试代码如下:
Connection conn=null;
try{
Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:/comp/env");
DataSource ds = (DataSource) ctx.lookup("jdbc/mssql");
System.out.println("获得连接池对象!");
conn = ds.getConnection();
if(conn != null){
System.out.println("获得连接");
}else{
System.out.println("没有获得连接");
}
}catch(Exception ex){
ex.printStackTrace();
}
return conn;安装好sql2000驱动后我把lib下的驱动包考到了项目的WEB-INF/lib下,但是连接数据库是总是报错:
Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver'。
说找不到驱动,但是我用java直接连接mssql就可以链接到数据库。
public Connection getConnection(){
Connection con = null;
if(con==null){
try{ Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("数据库驱动程序注册成功");
con = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bbs","sa","admin");
System.out.println("数据库连接成功");
}catch(Exception e){
System.out.println("DBHelp:getConnection()");
System.out.println("数据库连接失败");
}
}
return con;
}这是为什么呢?
解决方案 »
- 数据联合查询的问题(在线等)
- struts2标签<select> 下拉内容显示不了
- 使用weblogic服务器里遇到的问题javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFa
- 用main函数来测试ejb的sessionBean,运行到context = new InitialContext()时,出现如下错误.......
- java虚拟现实中怎样应用?
- struts的Validator验证能简单的实现将所有表单进行相同的数据检验吗?(在线等待)
- 在struts 的action 中我怎么使用mapping.findforward后new ActionForward()来是我的return的jsp界面在新的一业中显示啊????????急!!!!看
- 请教:TOMCAT怎么调用JBOSS
- gradle和spring-boot构建的项目,启动时提示找不到application.properties文件中的参数
- springMvc 这个map为什么只能new 而不能作为方法参数传递?.
- 精通java ee的朋友,谈谈你的学习经历和使用体会
- 线程通讯
1安装sp3的补丁.sql2000sp3以下不支持jdbc,
2写个简单的jdbc测试驱动是否工作正常
3看看驱动包lib是否拷贝...下载最新的驱动
都弄过了 不行,我才来这发的帖子
我这怎么只有logs conf temp works webapps 五个文件夹?
ms sql2000的JDBC驱动程序是:com.microsoft.jdbc.sqlserver.SQLServerDriver
ms sql2005的JDBC驱动程序是:com.microsoft.sqlserver.jdbc.SQLServerDriver
它们是不同的。你用的驱动是2005的看看对应的.jar是不是一样的。