gpt4 book ai didi

mysql - 使用 JSF 中的预准备语句更新数据库

转载 作者:行者123 更新时间:2023-11-29 18:50:54 25 4
gpt4 key购买 nike

我有mainpage.xhtml,它显示数据库中的整个记录​​并允许我编辑它们。如果单击 Edit 按钮,它会调用 mainPageController.edit() 函数,该函数填充 MainPageConroller 类中的 _bilgi 变量,然后重定向到 edit.xhtml 页面。

edit.xhtml页面中,它基本上显示之前在MainPageController类中填充的_bilgi值。到这里为止没有问题(可能有但我不知道。)

当我想要更新其中一个变量(例如 baslik)时,我在 MainPageController 中创建了一个方法,该方法在 updateBilgi(_bilgi) 中调用 < strong>bilgiDAO 类。

UpdateBilgi 中,它的作用非常清楚,但它并没有更新表。对我来说有趣的是,当我调试并跟踪变量时,一切都很顺利,但最终它没有更新。

另一件事是,当我在 BilgilerDAO 中只放入一个字符串时,例如 ps.setString(1, "aqa1") 它可以工作,但是 ps.setString(2, bilgi.getBaslik() ) 甚至 bilgi.getBaslik() 也不会返回更改后的值。

可能出了什么问题?感谢您提前的答复。

我希望我说得足够清楚。

编辑:我发现了故障。在 BilgilerDAO 中,函数 updateBilgi(Bilgi bilgi)变量 bilgi 不会从 _bilgi 获取 id,而是获取其他变量。为什么取不到id?

ma​​inpage.xhtml

<h:form id="bilgiForm">
<h:dataTable id="bilgiTable" value="#{mainPageController.bilgiListesi}"
var="bilgiler" border="1">
<h:column>
<f:facet name="header">
<h:outputText value="Başlık" />
</f:facet>
<h:outputText value="#{bilgiler.baslik}" />
<f:facet name="footer">
<h:outputText value="Başlık" />
</f:facet>
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>


<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:outputText value="#{bilgiler.bilgi}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>

<h:column>
<f:facet name="header">
<h:outputText value="Bilgi Metni" />
</f:facet>
<h:graphicImage
value="https://canakkale.000webhostapp.com/showImage.php/?name=#{bilgiler.img.name}" />
<f:facet name="footer">
<h:outputText value="Bilgi Metni" />
</f:facet>
</h:column>

<h:column>
<fcore:facet name="header">Edit</fcore:facet>
<h:commandButton action="edit" value="Edit"
actionListener="#{mainPageController.edit()}">
<fcore:param name="id" value="#{bilgiler.id}" />
</h:commandButton>
</h:column>
</h:dataTable>
</h:form>




<fcore:view>
<h:form>
<fcore:view>
<h:form>
<h1>Update</h1>
<h:panelGrid border="" cellpadding="20" cellspacing="20"
columns="3">
<h:outputLabel value="Bilgi ID" />
<h:inputText value="#{mainPageController._bilgi.id}"
readonly="true" required="true" requiredMessage="Id is Required"
id="bilgiID">
</h:inputText>
<h:message for="bilgiID" />

<h:outputLabel value="Tag" />
<h:inputText value="#{mainPageController._bilgi.tag}"
readonly="true" required="true"
requiredMessage="Tag Girmeniz Gereklidir." id="bilgiTag">
</h:inputText>
<h:message for="bilgiTag" />
<h:outputLabel value="Başlık" />
<h:inputText value="#{mainPageController._bilgi.baslik}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBaslik">
</h:inputText>
<h:message for="bilgiBaslik" />


<h:outputLabel value="Bilgi Metni" />
<h:inputTextarea value="#{mainPageController._bilgi.bilgi}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
id="bilgiBilgi">
</h:inputTextarea>
<h:message for="bilgiBilgi" />


<h:outputLabel value="Image URL" />
<h:inputText value="#{mainPageController._bilgi.img.name}"
required="true" requiredMessage="Başlık Girmeniz Gereklidir."
readonly="true" id="bilgiImgName">
</h:inputText>
<h:message for="bilgiImgName" />

<h:outputLabel value="Başlık" />
<input type="file" value="#{mainPageController._bilgi.img.image}" />
<h:commandButton action="#{mainPageController.updateRecord()}"
value="Update">
</h:commandButton>
</h:panelGrid>
</h:form>
</fcore:view>


</h:form>
</fcore:view>
</h:body>
</html>

MainPageController类

 @ManagedBean
public class MainPageController implements Serializable {

List<Bilgi> bilgiListesi = new ArrayList<Bilgi>();
private Bilgi _bilgi = new Bilgi();
private static final long serialVersionUID = 1L;

BilgilerDAO bilgiDao = new BilgilerDAO();
ImageDAO imgDao = new ImageDAO();

public MainPageController() {// Default Constructor
try {
bilgiListesi = showAllRecords();
} catch (SQLException e) {
e.printStackTrace();
}
}

public List<Bilgi> showAllRecords() throws SQLException {
return bilgiDao.getAllBilgiler();
}

public List<Image> showAllImages() throws Exception {
return imgDao.getAllImages();

}

public void updateRecord() {
try {
bilgiDao.updateBilgi(_bilgi);
//imgDao.updateImage(_bilgi.getImg());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public void edit() {
FacesContext fc = FacesContext.getCurrentInstance();
int id;
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
id = Integer.parseInt(request.getParameter("id"));
try {
bilgiListesi = bilgiDao.getAllBilgiler();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (Bilgi bilgi : bilgiListesi) {
if (bilgi.getId() == id) {
_bilgi.setBaslik(bilgi.getBaslik());
_bilgi.setBilgi(bilgi.getBilgi());
_bilgi.setImg(bilgi.getImg());
_bilgi.setTag(bilgi.getTag());
_bilgi.setId(bilgi.getId());
}

}
}

public void edit(Bilgi bilgi) {
for (Bilgi existing : getBilgiListesi()) {
existing.setEditable(false);
}
bilgi.setEditable(true);
}


}

BilgilerDAO

@Override
public String updateBilgi(Bilgi bilgi) throws Exception {
String sql = "UPDATE bilgiler SET bilgi=?, baslik =? WHERE id=? ";
ps = DatabaseConnection.getConnection().prepareStatement(sql);

ps.setString(1, "aqa1");
ps.setString(2, bilgi.getBaslik());
ps.setInt(3, bilgi.getId());

int ex = ps.executeUpdate();
if (ex > 0) {
System.err.println("coodld");
}
ps.close();
return "mainpage";

}

最佳答案

我已经解决了这个问题。

<h:inputText value="#{mainPageController._bilgi.id}" readonly="true" required="true" requiredMessage="Id is Required" id="bilgiID">

在此 xhtml 行中 readonly="true" 导致它返回 null。我现在将其删除,它按我想要的方式工作。不过,我仍然希望它们显示但不可编辑。

关于mysql - 使用 JSF 中的预准备语句更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358351/

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