idea调用mysql报错:org.apache.tomcat.dbcp.dbcp2.DelegatingCallableStatement cannot be cast to com.mysql.jdbc.CallableStatement……
这个错误是类型转换引起的,即DelegatingCallableStatement 无法转换为CallableStatement。
——————————————————————————————————————————————————
问题解决:
看看自己的import,看看是不是导入了mysql的CallableStatement\Connection\DateSource……
检查自己的conn,即Connection ,看看是不是转换成了mysql的CallableStatement。
call = (CallableStatement) conn.prepareCall("{call select_id_fromCourseName(?)}");
这样肯定是错的,把里面的(CallableStatement) 转换去掉。
虽然连接的是mysql数据库,但是导入的却是sql的CallableStatement………………似乎是tomcat决定的?????
正确的方法:
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
。。。。。。。。。。。。省略
CallableStatement call = null;
try {
Context context = new InitialContext();
Context envContext = (Context) context.lookup("java:comp/env/");
DataSource ds = (DataSource) envContext.lookup("jdbc/testDS");
Connection conn = ds.getConnection();
call = conn.prepareCall("{call select_id_fromCourseName(?)}");
call.setString(1,courseName);
ResultSet rs= call.executeQuery();
while (rs.next()){
result=rs.getInt(1);
}
rs.close();
call.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (NamingException e) {
e.printStackTrace();
}