今天遇到这样的一个问题,mysql连接不上(数据库能正常工作)!特此求助!!!
背景:Tomcat5.5+Jdk 1.5, JSP
Tomcat中的server文件我添加了如下代码:
<Resource
name="jdbc/wlps"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="2"
username="root"
maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
password="root"
url="jdbc:mysql://localhost:3306/wlps"/>主目录的web.xml文件中相应的添加了:<resource-ref>
<description>Test</description>
<res-ref-name>jdbc/wlps</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>我链接数据库的代码是:Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/wlps"); ;
return ds.getConnection();报错如下:
org.apache.jasper.JasperException: Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.jspdev.filter.EncodingFilter.doFilter(EncodingFilter.java:37)java.sql.SQLException: No suitable driver
java.sql.DriverManager.getDriver(Unknown Source)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
com.jspdev.util.DatabaseConn.getConnection(DatabaseConn.java:17)
com.jspdev.login.LoginBean.<init>(LoginBean.java:13)
org.apache.jsp.signin_005fdo_jsp._jspService(signin_005fdo_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.jspdev.filter.EncodingFilter.doFilter(EncodingFilter.java:37)今天已经折腾6-7小时了,没找出什么错误,望大侠们仗义出手!!!
背景:Tomcat5.5+Jdk 1.5, JSP
Tomcat中的server文件我添加了如下代码:
<Resource
name="jdbc/wlps"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="2"
username="root"
maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
password="root"
url="jdbc:mysql://localhost:3306/wlps"/>主目录的web.xml文件中相应的添加了:<resource-ref>
<description>Test</description>
<res-ref-name>jdbc/wlps</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>我链接数据库的代码是:Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/wlps"); ;
return ds.getConnection();报错如下:
org.apache.jasper.JasperException: Cannot create JDBC driver of class '' for connect URL 'null'
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:460)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:355)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.jspdev.filter.EncodingFilter.doFilter(EncodingFilter.java:37)java.sql.SQLException: No suitable driver
java.sql.DriverManager.getDriver(Unknown Source)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1143)
org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
com.jspdev.util.DatabaseConn.getConnection(DatabaseConn.java:17)
com.jspdev.login.LoginBean.<init>(LoginBean.java:13)
org.apache.jsp.signin_005fdo_jsp._jspService(signin_005fdo_jsp.java:120)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:331)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:329)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
com.jspdev.filter.EncodingFilter.doFilter(EncodingFilter.java:37)今天已经折腾6-7小时了,没找出什么错误,望大侠们仗义出手!!!
解决方案 »
- 请问这样的表单,如何在action中得到表单提交的数据
- Java 如何导出 excel为两个sheet
- java 接受参数乱码。。。。
- 在线= 如何将action中的某个值传到页面!
- Struts问题急
- 请问-如果将查讯出来的数据放到数组中???
- 超級鏈接問題
- 求助ext高手 extjs提交按钮怎么不触发url: path+"addFile.action?MOID="+moid,
- 求jsp调用EXCEL函数并计算后将值返回的方法
- web项目可以正常启动,但是打成war包放到tomcat中启动就报错
- zhuchao_ko : 关于Protal、SOA(抛砖引玉)
- 谁知道oracle系统自带哪些用户,例如:SYS,SCOTT等
说明没有成功加载驱动,、第一:要保证mysql驱动(一个jar包)放到正确的路径下,按你所使用的tomcat数据库连接池,那你应该把驱动放到tomcat主目录的 lib目录下第二:在获取连接之前,至少要调用一次Class.forName("="com.mysql.jdbc.Driver");一般是你获取数据库连接的那个类中加一段静态代码
static{
Class.forName("="com.mysql.jdbc.Driver");
}
第二个不太明白,这个放在哪?麻烦大侠详细点拜托
public class ConnDB{ //放在这个位置
static{
try{
Class.forName("="com.mysql.jdbc.Driver");
}catche(Exception e){ }
}
public Connection getConnection(){
//这个可能是你获取连接的方法
Context initCtx = new javax.naming.InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource)envCtx.lookup("jdbc/wlps"); ;
return ds.getConnection(); }}
使用连接池,web容器会自动加载驱动吧??
<description>Test</description>
<res-ref-name>jdbc/wlps</res-ref-name>
<res-ref-type>javax.sql.DataSource</res-ref-type>
<res-auth>Container</res-auth>
</resource-ref>错误竟然是:
Status reportmessage /wlps/description The requested resource (/wlps/) is not available.除掉那段代码,错误是:
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context这是神马原因呢?
这个项目是从Boss那接来的,他那边是可以正常跑的,代码不是我自己编
求助!!!
问题解决了。我这个问题的主要解决方法是:
在tomcat下的conf下的context.xml文件,在<context></context>之间添加如下内容:
<Resource
name="jdbc/wlps"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="2"
username="root"
maxWait="5000"
driverClassName="com.mysql.jdbc.Driver"
password="root"
url="jdbc:mysql://localhost:3306/wlps"/>
和server.xml中<GlobalNamingResources></GlobalNamingResources>中的内容一样。
谢谢各位的帮忙!!!
有关Tomcat不同版本配置问题,我找了些资料。仅供参考:
Tomcat 数据源配置
一、 Tomcat 5.0:
准备:把数据库驱动程序jar包放到tomcat5.0下的common\lib下面:
1、 配置tomcat下的conf下的server.xml文件,添加如下内容:
<Resource name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://127.0.0.1:3306/study </value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>198395</value>
</parameter>
</ResourceParams>
</Context>
2、 配置tomcat下的conf下的context.xml文件,在<context></context>之间添加如下内容:
<Resource name="jdbc/mysql" type="javax.sql.DataSource"/>
<ResourceParams name="jdbc/mysql">
<parameter>
<name>maxWait</name>
<value>5000</value>
</parameter>
<parameter>
<name>maxActive</name>
<value>4</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:mysql://127.0.0.1:3306/study </value>
</parameter>
<parameter>
<name>driverClassName</name>
<value>org.gjt.mm.mysql.Driver</value>
</parameter>
<parameter>
<name>maxIdle</name>
<value>2</value>
</parameter>
<parameter>
<name>username</name>
<value>sa</value>
</parameter>
<parameter>
<name>password</name>
<value>198395</value>
</parameter>
</ResourceParams>
</Context>
3、配置你的应用下的web.xml中的<web-app></web-app>之间加入:
1) <resource-ref>
2) <description>
3) DB Resource
4) </description>
5) <res-ref-name>
6) jdbc/mysql
7) </res-ref-name>
8) <res-type>
9) javax.sql.DataSource
10) </res-type>
11) <res-auth>
12) Container
13) </res-auth>
14) </resource-ref>二、 Tomcat 5.5:
准备:把数据库驱动程序jar包放到tomcat5.5下的common\lib下面:
1、 配置tomcat下的conf下的server.xml文件,添加如下内容:
2) <GlobalNamingResources>
3) <Resource
4) name="jdbc/sqlserver"
5) type="javax.sql.DataSource"
6) password=""
7) driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
8) maxIdle="2"
9) maxWait="5000"
10) username="sa"
11) url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=corejava"
12) maxActive="4"/>
13) </GlobalNamingResources>2、 配置tomcat下的conf下的context.xml文件,在<context></context>之间添加如下内容:
14) <Resource
15) name="jdbc/sqlserver"
16) type="javax.sql.DataSource"
17) password=""
18) driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
19) maxIdle="2"
20) maxWait="5000"
21) username="sa"
22) url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=corejava"
23) maxActive="4"/>3、配置你的应用下的web.xml中的<web-app></web-app>之间加入:
1) <resource-ref>
2) <description>
3) DB Resource
4) </description>
5) <res-ref-name>
6) jdbc/sqlserver
7) </res-ref-name>
8) <res-type>
9) javax.sql.DataSource
10) </res-type>
11) <res-auth>
12) Container
13) </res-auth>
14) </resource-ref>
三、 Tomcat 6.0:
准备:把数据库驱动程序jar包放到tomcat6下的lib下面
1、 配置tomcat下的conf下的context.xml文件,在<context></context>之间添加如下内容:
<Resource name="jdbc/mysql"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/test"
username="root"
password="root"
maxActive="100"
maxIdle="30"
maxWait="10000" /> 2、 配置你的应用下的web.xml中的<web-app></web-app>之间加入:<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、 大功告成,不用在原来的server.xml里面配置了测试代码:
1. <%@ page import="java.sql.*"%>
2. <%@ page import="javax.sql.*"%>
3. <%@ page import="javax.naming.*"%>
4. <%@ page session="false" %>
5.
6. <html>
7. <head>
8. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
9. <title></title>
10. </head>
11. <body>
12. <%
13. out.print("我的测试开始");
14. DataSource ds = null;
15.
16. try{
17. InitialContext ctx=new InitialContext();
18. ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql");
19. Connection conn = ds.getConnection();
20. Statement stmt = conn.createStatement();
21.
22. String strSql = " select * from users"; // users是数据库已有的表
23. ResultSet rs = stmt.executeQuery(strSql);
24. while(rs.next()){
25.
26. out.print(rs.getString(1));
27. }
28.
29. out.print("我的测试结束");
30.
31. }catch(Exception ex){
32. out.print(“出现例外,信息是:”+ex.getMessage());
33. ex.printStackTrace();
34.
35. }
36.
37. %>
38. </body>
39. </html>