问题描述
我正在运行 Tomcat 7.0.22,我编写了一个连接到 SQL Anywhere 12.0 数据库的简单 servlet.当我运行 servlet 时,我得到 java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource.我的 ./META-INF/content.
<资源名称="jdbc/FUDB"身份验证=容器"类型="javax.sql.DataSource"用户名 =数据库管理员"密码=sql"driverClassName="sybase.jdbc.sqlanywhere.IDriver"factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
url="jdbc:sqlanywhere:uid=dba;pwd=sql;eng=BTH476331A_FedUtilization;"accessToUnderlyingConnectionAllowed="true"最大活动=8"maxIdle="4"/>
我的 webapp web.
<web-app <display-name>FedUtilization</display-name><小服务程序><servlet-name>Report1</servlet-name><display-name>Report1</display-name><servlet-class>com.sapgss.ps.servlet.Report1</servlet-class>
报告1/报告1
SQL Anywhere 12.0.1 服务器 jdbc3jdbc/FUDBjavax.sql.DataSource容器
servlet 代码如下:
import java.io.*;导入 java.sql.*;导入 javax.servlet.*;导入 javax.servlet.http.*;导入 javax.naming.*;导入 org.apache.catalina.core.StandardContext.*;导入 org.apache.tomcat.jdbc.pool.*;导入 com.sapgss.ps.dbutil.*;导入 org.apache.tomcat.dbcp.dbcp.BasicDataSource;公共类 Report1 扩展了 HttpServlet {public void doGet(HttpServletRequest request,
HttpServletResponse 响应)抛出 IOException, ServletException{尝试{response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("");out.println("");out.println("你好伊莱恩!");out.println("");out.println("");out.println("
伊莱恩你好!
");//这是如何在 Java Context 中编写对数据库的访问initCtx = 新的 InitialContext();上下文 envCtx = (上下文)initCtx.lookup("java:comp/env");数据源 ds = (数据源)envCtx.lookup("jdbc/FUDB");连接 conn = ds.getConnection();...
}}
当我尝试在此行获取数据源时发生错误:DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB");
提前谢谢我正在拔头发.
就我而言,我只是忘记了:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
在我的 /tomcat7/conf/context. 中.刚刚添加,一切正常.
我的 context.
<资源名称="jdbc/gestrel" auth="容器"类型="javax.sql.DataSource"driverClassName="org.postgresql.Driver"url="jdbc:postgresql://127.0.0.1:5432/g...."用户名 =postgres"密码="....." maxActive="20" maxIdle="10"factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"maxWait="-1"/>上下文>
I'm running Tomcat 7.0.22 and I wrote a simple servlet that connects to a SQL Anywhere 12.0 database. When I run the servlet I get java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to org.apache.tomcat.jdbc.pool.DataSource. My ./META-INF/content.
My webapp web.
The servlet code is as follows:
The error happens when I try to get a DataSource at this line: DataSource ds = (DataSource) envCtx.lookup("jdbc/FUDB");
Thanks in advance I'm pulling my hair out.
In my case I just forgot to put:
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
in my /tomcat7/conf/context.. Just added and all worked fine.
My context.
<Context>
<Resource name="jdbc/gestrel" auth="Container"
type="javax.sql.DataSource"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1:5432/g...."
username="postgres"
password="....." maxActive="20" maxIdle="10"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
maxWait="-1"/>
</Context>
这篇关于java.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource 无法转换为 org.apache.tomcat.jdbc.pool.DataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!