Java I';m获取ClassCastException:WrappedPreparedStatementJDK8无法强制转换为SQLServerPreparedStatement

Java I';m获取ClassCastException:WrappedPreparedStatementJDK8无法强制转换为SQLServerPreparedStatement,java,wildfly,war,Java,Wildfly,War,我的环境:面向企业Java开发人员的Eclipse IDE,版本:2019-09 R(4.13.0) 服务器:WildFly 13.0.0.Final 我正在尝试将数据批量上传到Sql Server,下面的代码在具有相同版本的WildFly服务器中运行良好,但在具有相同版本的WildFly服务器的客户端服务器机器中失败,java.lang.ClassCastException:org.jboss.jca.adapters.jdbc.jdk8.WrappedPreparedStatementJDK

我的环境:面向企业Java开发人员的Eclipse IDE,版本:2019-09 R(4.13.0)

服务器:WildFly 13.0.0.Final

我正在尝试将数据批量上传到Sql Server,下面的代码在具有相同版本的WildFly服务器中运行良好,但在具有相同版本的WildFly服务器的客户端服务器机器中失败,java.lang.ClassCastException:org.jboss.jca.adapters.jdbc.jdk8.WrappedPreparedStatementJDK8无法强制转换到com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement在下面的语句中:

SQLServerPreparedStatement pStmt = (SQLServerPreparedStatement) con.prepareStatement(execStoredProc);
以下是完整的代码:

import com.microsoft.sqlserver.jdbc.SQLServerDataTable;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;

import java.sql.*;
import java.util.ArrayList;

public class DatabaseExtended extends Database {
    public DatabaseExtended(String assetDisposition, String assetDisposition_ds) {
        super(assetDisposition, assetDisposition_ds);
        // TODO Auto-generated constructor stub
    }

    public Object executeEstimateStructured(int iClientID, SQLServerDataTable sourceDataTable) {
        //boolean rs = false;
        ArrayList<String[]> lst = new ArrayList<String[]>();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = DriverManager.getConnection(super.connectionUrl);
            Statement stmt = con.createStatement();
            String execStoredProc = "EXEC UploadEstimate ?,?";
            SQLServerPreparedStatement pStmt = (SQLServerPreparedStatement) con.prepareStatement(execStoredProc);//<--Exception here
            pStmt.setInt(1, iClientID);
            pStmt.setStructured(2, "dbo.UT_Estimate", sourceDataTable);
            ResultSet rs = pStmt.executeQuery();
            int columnCount = rs.getMetaData().getColumnCount();

            while (rs.next()) {

                int i = 1;
                String[] cols = new String[columnCount];
                while (i <= columnCount) {
                    cols[i - 1] = rs.getString(i++);
                }
                lst.add(cols);
            }
            con.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return lst;
    }
}

更新日期:2020年3月16日 这是我使用批处理语句更新的代码:

    public String executeInboundEstimateBatchedStoredProc(int iClientID, SQLServerDataTable sourceDataTable) {
        String message="";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = super.getConnection();
            String execStoredProc = "EXEC UploadEstimate ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?";

            PreparedStatement pStmt = con.prepareStatement(execStoredProc);

            Iterator<Entry<Integer, Object[]>> itr1 = sourceDataTable.getIterator();
            while (itr1.hasNext()) {
                Object[] lst=  itr1.next().getValue();              
                int numColumns = lst.length;
                pStmt.setInt(1, iClientID);
                for (int i = 0; i < numColumns; i++) {
                    pStmt.setString(i+2, lst[i].toString());
                }
                // add single insert query
                pStmt.addBatch();
            }

            // Execute batch
            int[] counts = pStmt.executeBatch();

            if (pStmt != null) {
                pStmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        // Handle any errors that may have occurred.
        catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            message=e.getMessage();
        }
        return message;
    }
public String executeInBoundEstimateBackedStoredProc(int-iClientID,SQLServerDataTable-sourceDataTable){
字符串消息=”;
试一试{
Class.forName(“com.microsoft.sqlserver.jdbc.SQLServerDriver”);
Connection con=super.getConnection();
字符串execStoredProc=“EXEC UploadEstimate;
PreparedStatement pStmt=con.prepareStatement(execStoredProc);
迭代器itr1=sourceDataTable.getIterator();
while(itr1.hasNext()){
对象[]lst=itr1.next().getValue();
int numColumns=lst.length;
pStmt.setInt(1,iClientID);
对于(int i=0;i
这是因为您不应该强制转换PreparedStatement,而应该使用unwrap。
con.prepareStatement(execStoredProc).unwrap(SQLServerPreparedStatement.class)

U不必检查连接对象的准备语句的返回类型。依赖于平台的底层驱动程序可以提供自己的预处理语句接口的实现,而这与用户无关。你应该只使用接口提供的公共方法,而不是进一步挖掘。如果您没有错误,只需将您的语句强制转换为常规类型即可。PreparedStatement pStm=con.preparestatemt(execStoredProc)如果您正在处理一个我从您的execStoredProc中假定的过程,那么使用con.prepareCall()并存储在一个可调用的语句类型中

我做了并删除了jboss-deployment-structure.xml,构建了war文件,现在得到以下异常:2020-03-13 08:57:24950错误[stderr](默认任务4)java.sql.SQLException:IJ031030:不是com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement 2020-03-13 08:57:24950错误[stderr](默认任务4)位于org.jboss.jca.adapters.jdbc.JBossWrapper.unwrap(JBossWrapper.java:96)您好,VISHAL,我想将记录批量插入数据库,而通用CallableStation没有我可以在表中传递的setStructured方法。有吗?有。您可以。首先使用setXXX方法设置批处理的参数,然后使用addBatch()创建一批设置的参数。然后调用executeBatch()在statementCallable语句上,它是prepared语句的子接口,只有额外的registerOutXXX方法,因此方法与Hi R VISHAL相同,感谢您的指导。我使用了可用的默认实现,而不是将其强制转换为SqlServer特定类型,然后使用addBatch方法来完成批量导入t、 PreparedStatement pStmt=con.prepareStatement(execStoredProc);很高兴我能帮助您。请记住,setXXX方法和addBatch()方法保存了要重用的语句对象的值,因此,如果您要放弃以前的数据和参数,请在重用语句对象之前始终使用clearbatch()或clearParameters()方法
ERROR [io.undertow.request] (default task-5) UT005023: Exception handling request to /AD/control/UploadEstimates: java.lang.IllegalAccessError: tried to access class com.microsoft.sqlserver.jdbc.Util from class com.microsoft.sqlserver.jdbc.SQLServerDataTable
    at com.microsoft.sqlserver.jdbc.SQLServerDataTable.addColumnMetadata(SQLServerDataTable.java:83)
    at com.MyClient.AD.util.UploadEstimateUT.start(UploadEstimateUT.java:66)
    at com.MyClient.AD.util.UploadEstimates.doPost(UploadEstimates.java:107)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1514)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1514)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1514)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1514)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:360)
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
    at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
    at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:1985)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1487)
    at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1378)
    at java.lang.Thread.run(Thread.java:748)
    public String executeInboundEstimateBatchedStoredProc(int iClientID, SQLServerDataTable sourceDataTable) {
        String message="";
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con = super.getConnection();
            String execStoredProc = "EXEC UploadEstimate ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?";

            PreparedStatement pStmt = con.prepareStatement(execStoredProc);

            Iterator<Entry<Integer, Object[]>> itr1 = sourceDataTable.getIterator();
            while (itr1.hasNext()) {
                Object[] lst=  itr1.next().getValue();              
                int numColumns = lst.length;
                pStmt.setInt(1, iClientID);
                for (int i = 0; i < numColumns; i++) {
                    pStmt.setString(i+2, lst[i].toString());
                }
                // add single insert query
                pStmt.addBatch();
            }

            // Execute batch
            int[] counts = pStmt.executeBatch();

            if (pStmt != null) {
                pStmt.close();
            }
            if (con != null) {
                con.close();
            }
        }
        // Handle any errors that may have occurred.
        catch (SQLException | ClassNotFoundException e) {
            e.printStackTrace();
            message=e.getMessage();
        }
        return message;
    }