gpt4 book ai didi

java - 一个非常非常奇怪的错误ClassCastException。 PreparedStatement 的 setInt 方法

转载 作者:行者123 更新时间:2023-11-29 10:07:42 28 4
gpt4 key购买 nike

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

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

currentSubscriptions 是:

List<Integer> currentSubscriptions;

即使它是整数列表,我也会收到此错误:-

SEVERE: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

假设连接对象已经存在。而且我非常确定 currentSubscriptions 不为空,否则我不会遇到此错误。如果我不使用 List 而是像这样硬编码:

objPreparedStatement.setInt(2,1);

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

提前致谢:)

编辑:-

至少这应该有效。但即使这样也行不通:-

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

objPreparedStatement.setInt(2, intSubscriptionId );

编辑 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();
}
}
}
}

这是我的 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>

请看JSF的h:selectManyCheckbox。 JSF 在内部将我选中的所有复选框传递到我的列表。我认为 JSF 正在将我的整数列表转换为字符串。

最佳答案

您需要指示h:selectManyCheckbox将值转换为 Integer通过指定 javax.faces.Integer作为转换器。通用类型在 EL 中是未知的,它默认将参数视为 String。 .

<h:selectManyCheckbox converter="javax.faces.Integer">

无需使用List<String>相反,这只会导致 bean 中出现更多的弱类型困惑。

关于java - 一个非常非常奇怪的错误ClassCastException。 PreparedStatement 的 setInt 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3221780/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com