Java org.apache.tomcat.dbcp.dbcp.PoolgDataSource$PoolGuardConnectionWrapper不能强制转换为com.mysql.jdbc.Connection
geeting org.apache.tomcat.dbcp.dbcp.PoolgDataSource$PoolGuardConnectionWrapper无法使用tomcat服务器和mysql数据库强制转换为com.mysql.jdbc.Connection。请查找以下代码。请帮我解决这个问题Java org.apache.tomcat.dbcp.dbcp.PoolgDataSource$PoolGuardConnectionWrapper不能强制转换为com.mysql.jdbc.Connection,java,servlets,jakarta-ee,datasource,jndi,Java,Servlets,Jakarta Ee,Datasource,Jndi,geeting org.apache.tomcat.dbcp.dbcp.PoolgDataSource$PoolGuardConnectionWrapper无法使用tomcat服务器和mysql数据库强制转换为com.mysql.jdbc.Connection。请查找以下代码。请帮我解决这个问题 context.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <Context> <Re
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource name="jdbc/MySQL_ds" auth="Container" type="javax.sql.DataSource"
maxActive="50" maxIdle="30" maxWait="10000"
username="root" password="root321"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://162.70.211.17:3306/emp"
accessToUnderlyingConnectionAllowed="true"/>
</Context>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>sample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>Insert</display-name>
<servlet-name>Insert</servlet-name>
<servlet-class>Insert</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Insert</servlet-name>
<url-pattern>/insert</url-pattern>
</servlet-mapping>
<resource-ref>
<description>MySQL Datasource example</description>
<res-ref-name>jdbc/MySQL_ds</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
在context.xml中使用
com.mysql.jdbc.jdbc2.optional.MysqlDataSource
而不是com.mysql.jdbc.Driver
。您正在创建数据源,而不是直接连接
有关更多信息,请参阅。应删除这些导入:
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
改用
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
如果您使用的是数据源,您的连接将被池包装,请使用接口。感谢您在context.xml中添加stmt后的回复和建议,获取以下错误javax.naming.NamingException:无法创建资源实例运行应用程序时获取以下错误javax.naming.NamingException:无法创建资源实例任何正文都可以帮助我完成整个堆栈跟踪。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;