geeting org.apache.tomcat.dbcp.dbcp.PoolingDataSource $ PoolGuardConnectionWrapper无法使用tomcat服务器和mysql数据库强制转换为com.mysql.jdbc.Connection,请在下面找到代码。请帮我解决这个问题。
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>
Java代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import javax.sql.DataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
public class Insert extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("do post calling");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String Username = request.getParameter("roll");
Context ctx = null;
Connection connection;
try{
ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/MySQL_ds");
connection = (Connection) ds.getConnection();
System.out.println("Connection established");
PreparedStatement ps=(PreparedStatement) connection.prepareStatement("insert into employee values(?)");
ps.setString(1,Username);
int i=ps.executeUpdate();
if(i>0)
out.print("You are successfully registered...");
}catch (Exception e2) {System.out.println(e2);}
out.close();
}
}
答案 0 :(得分:2)
应删除这些导入:
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;
如果您使用DataSource,您的连接将被池包装,请使用接口。
答案 1 :(得分:1)
在context.xml中使用com.mysql.jdbc.jdbc2.optional.MysqlDataSource
代替com.mysql.jdbc.Driver
。您正在创建数据源,而不是直接连接。
有关详细信息,请参阅the Connector-J docs。