一个非常奇怪的错误ClassCastException.PreparedStatement的setInt方法

TCM*_*TCM 1 java jsf jdbc

谁能告诉我为什么这种方法不起作用?

String strQuery = "Insert Into cust_subs (CustomerId,SubscriptionId) Values (?,?)";
PreparedStatement objPreparedStatement = Utils.getPreparedStatement(objConnection, strQuery);
objPreparedStatement.setInt(2, currentSubscriptions.get(0) );
Run Code Online (Sandbox Code Playgroud)

在哪里currentSubscriptions:

List<Integer> currentSubscriptions;
Run Code Online (Sandbox Code Playgroud)

我得到这个错误,即使它是整数列表: -

SEVERE:java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Integer

假设连接对象已存在.而且我非常确定这currentSubscriptions不是空的,否则我不会有这个错误.如果不是List像这样使用i hardcode:

objPreparedStatement.setInt(2,1);
Run Code Online (Sandbox Code Playgroud)

有用.我甚至打印了List使用System.out.println 的值,它非常好.它们只是整数.不知道为什么把它们视为字符串.我甚至试过Integer.parseInt列表的项目.它仍然给我同样的错误.这是我遇到过的最有趣的错误之一.

提前致谢 :)

编辑: -

至少这应该工作.但即使这样也行不通: -

  int intSubscriptionId = Integer.parseInt( currentSubscriptions.get(0).toString());

            objPreparedStatement.setInt(2, intSubscriptionId );
Run Code Online (Sandbox Code Playgroud)

编辑2:

发布完整代码: -

package beans;

import entities.Customer;
import entities.Subscription;
import java.io.IOException;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import misc.Utils;

@ManagedBean
@ViewScoped
public class AddSubscriptionBean implements Serializable {

    private Customer customer;
    private List<Integer> currentSubscriptions;
    private List<Subscription> subscriptionList;

    public List<Subscription> getSubscriptionList() {
        return subscriptionList;
    }

    public void setSubscriptionList(List<Subscription> subscriptionList) {
        this.subscriptionList = subscriptionList;
    }

    public List<Integer> getCurrentSubscriptions() {
        return currentSubscriptions;
    }

    public void setCurrentSubscriptions(List<Integer> currentSubscriptions) {
        this.currentSubscriptions = currentSubscriptions;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }

    /** Creates a new instance of AddSubscriptionBean */
    public AddSubscriptionBean() throws IOException, SQLException {

        Connection objConnection = null;
        try {
            HttpServletRequest objHttpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            int intCustomerId = Integer.parseInt(objHttpServletRequest.getParameter("cid"));
            String strQuery = "Select * from customer Where CustomerID = " + intCustomerId;

            ResultSet objResultSet = Utils.executeResultSet(objConnection, strQuery);
            if (objResultSet.next()) {
                String strFirstName = objResultSet.getString("FirstName");
                String strLastName = objResultSet.getString("LastName");
                customer = new Customer(intCustomerId, strFirstName, strLastName);
            }

            currentSubscriptions = new ArrayList<Integer>();

            for (Subscription objSubscription : customer.getSubscriptionList()) {
                currentSubscriptions.add(objSubscription.getSubscriptionId());
            }


            subscriptionList = new ArrayList<Subscription>();
            strQuery = "Select * from subscription";
            objResultSet = Utils.executeResultSet(objConnection, strQuery);
            while (objResultSet.next()) {
                int intSubscriptionId = objResultSet.getInt("SubscriptionId");
                String strSubsriptionTitle = objResultSet.getString("Title");
                String strSubsriptionType = objResultSet.getString("Type");
                Subscription objSubscription = new Subscription(intSubscriptionId, strSubsriptionTitle, strSubsriptionType);
                subscriptionList.add(objSubscription);
            }


        } catch (Exception ex) {
            ex.printStackTrace();
            FacesContext.getCurrentInstance().getExternalContext().redirect("index.jsf");
        } finally {
            if (objConnection != null) {
                objConnection.close();
            }
        }
    }

    public void save() throws SQLException {

        Connection objConnection = null;
        Savepoint objSavepoint = null;
        try {
            objConnection = Utils.getConnection();
            objConnection.setAutoCommit(false);
            objSavepoint = objConnection.setSavepoint();
            String strQuery = "Delete From cust_subs Where CustomerId = " + customer.getCustomerId();

            if (!Utils.executeQuery(objConnection, strQuery)) {
                throw new Exception();
            }

            strQuery = "Insert Into cust_subs (CustomerId,SubscriptionId) Values (?,?)";


            int intCustomerId = customer.getCustomerId();
            PreparedStatement objPreparedStatement = Utils.getPreparedStatement(objConnection, strQuery);
            for (int intIndex = 0; intIndex < currentSubscriptions.size(); intIndex++) {
                objPreparedStatement.setInt(1, intCustomerId);
                int intSubscriptionId = Integer.parseInt( currentSubscriptions.get(0).toString());

                objPreparedStatement.setInt(2, intSubscriptionId );
                objPreparedStatement.addBatch();
            }

            objPreparedStatement.executeBatch();

            objConnection.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            if (objConnection != null) {
                objConnection.rollback(objSavepoint);
            }
        } finally {
            if (objConnection != null) {
                objConnection.close();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的JSF页面: -

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:msc="http://mscit/jsf">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <center>
            <h:form>
            <h1>Add Subscription</h1>

            <b> Customer Name :</b> <h:outputText value="#{addSubscriptionBean.customer.firstName} #{addSubscriptionBean.customer.lastName}"/>

            <h:selectManyCheckbox value="#{addSubscriptionBean.currentSubscriptions}">

                <f:selectItems value="#{addSubscriptionBean.subscriptionList}" var="row" itemLabel="#{row.title}" itemValue="#{row.subscriptionId}" />

            </h:selectManyCheckbox>

            <h:commandButton value="Save" actionListener="#{addSubscriptionBean.save}"/>
            </h:form>
        </center>
    </h:body>
</html>
Run Code Online (Sandbox Code Playgroud)

请看一下JSF的h:selectManyCheckbox.JSF在内部将我检查过的所有复选框传递给我的列表.我认为JSF正在将我的整数列表转换为字符串.

Bal*_*usC 6

您需要通过指定转换器来指示h:selectManyCheckbox将值转换为.EL中的通用类型是未知的,它默认将参数视为.Integerjavax.faces.IntegerString

<h:selectManyCheckbox converter="javax.faces.Integer">
Run Code Online (Sandbox Code Playgroud)

不需要使用List<String>,只会导致bean中更多的弱类型混乱.