gpt4 book ai didi

java - 此查询似乎为 Module_Code 插入 null 而不是下拉选项中的值

转载 作者:行者123 更新时间:2023-12-02 00:47:38 26 4
gpt4 key购买 nike

我是检门新手。我有一个名为 Students2modules 的表,其中包含 2 个字段:Stud_Username、Module_Code,它将学生链接到所修读的模块。我想注册一名学生参加特定模块。当用户选择学生时,只有该学生尚未注册的模块才会出现:

package myapp.project;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
public class AddStud2Mod extends BasePage {

private List students =new ArrayList();
private List modules=new ArrayList();
private ModalWindow message;
private InformationPanel panel;
private DropDownChoice studChoice;
private DropDownChoice moduleChoice;
public AddStud2Mod(){
super();
Form stud2mod = new Form("addstud2mod");
add(stud2mod);

try{
DB db=new DB();
String query="select Stud_Username from students;";
System.out.println(query);
db.connect();
ResultSet rs=db.execSQL(query);
while(rs.next()){
String studentUname=rs.getString("Stud_Username");
students.add(studentUname);
}

db.close();

}
catch(Exception e){
e.printStackTrace();
}
studChoice = new DropDownChoice("studentChoice",new Model(),students);

moduleChoice = new DropDownChoice("modChoice",new Model(),modules);


studChoice.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
@Override
protected void onUpdate(AjaxRequestTarget target)
{
try{
DB db=new DB();
db.connect();
String query="select distinct Module_Code from modules where Module_Code NOT IN(Select Module_Code from students2modules where Stud_Username="+"'"+getUserName()+"');";
System.out.println(query);


ResultSet rs=db.execSQL(query);
while(rs.next()){
String moduleNotRegistered =rs.getString("Module_Code");
modules.add(moduleNotRegistered);
}
moduleChoice.setChoices(modules);
}
catch(Exception e){
e.printStackTrace();
}
target.addComponent(moduleChoice);
}
});

final FeedbackPanel feedback=new FeedbackPanel("msgs");
feedback.setOutputMarkupId(true);
add(feedback);
final InvalidInputIndicator codeLabel = new InvalidInputIndicator("codeLabel",moduleChoice);
codeLabel.setOutputMarkupId(true);
stud2mod.add(codeLabel);
final InvalidInputIndicator studLabel = new InvalidInputIndicator("usernameLabel",studChoice);
studLabel.setOutputMarkupId(true);
stud2mod.add(studLabel);
AjaxButton ok=new AjaxButton("confirm"){
public void onSubmit(AjaxRequestTarget target,Form form){
try{

DB db=new DB();
db.connect();
String query= "Insert into students2modules Values('"+getUserName()+"'"+",'"+moduleChoice.getDefaultModelObjectAsString()+"')";

System.out.println(query);
db.updateSQL(query);
modules.clear();
db.close();

}
catch(Exception e){
e.printStackTrace();
}
InformationPanel.add2ModuleMessage();
message.show(target);
}
protected void onError(AjaxRequestTarget target,Form form){
target.addComponent(feedback);
target.addComponent(studLabel);
target.addComponent(codeLabel);

}
};
stud2mod.add(ok);
Button cancel=new Button("cancel"){
public void onSubmit(){
AddStud2Mod lecturer=new AddStud2Mod();
setResponsePage(lecturer);

}
};
stud2mod.add(cancel);
studChoice.setRequired(false);
studChoice.setOutputMarkupId(true);
moduleChoice.setRequired(false);
moduleChoice.setOutputMarkupId(true);

stud2mod.add(moduleChoice);
stud2mod.add(studChoice);
message=new ModalWindow("InformationDialog");
add(message);
panel=new InformationPanel(message.getContentId(),message);
message.setContent(panel);
message.setCssClassName(ModalWindow.CSS_CLASS_BLUE);
message.setTitle("Important Information");
message.setResizable(false);
message.setInitialHeight(150);
message.setInitialWidth(150);
message.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
public void onClose(AjaxRequestTarget target) {
AddStud2Mod as2mod =new AddStud2Mod();
setResponsePage(as2mod);

}
});



}

private String getUserName(){
return studChoice.getDefaultModelObjectAsString();
}

}

这是问题所在 - onSubmit 中的插入语句 - '' 似乎是为 Module_Code 插入的,无论选择了什么模块代码,我不知道为什么。学生用户名已插入,但模块代码未插入。非常感谢您的帮助,并对大量代码表示歉意(包括在内,以便您可以阅读我的类(class)的所有代码)。

最佳答案

一些一般性评论:您的 UI 逻辑(Ajax,..)和“业务”逻辑(“为学生获取模块”)全部混合在一起。考虑通过引入“服务”层来将它们分开。另外,我不会像您在数据模型中那样使用学生姓名。如果学生改了名字怎么办?或者模块被重命名?请改用生成的 ID。

您使用 jdbc 的方式会使您的应用程序遭受 SQL 注入(inject)。 永远不要构建连接用户提供的输入的查询。为此使用准备好的语句(数据库的查询缓存也会喜欢这样;))

您的实际问题:您使用ajax。使用 Ajax,UI 中的更改不会推送到模型中。您可以正常获取学生姓名,因为您已将 AjaxFormComponentUpdatingBehavior 添加到了 DDC。

希望有帮助

编辑:查看您的个人资料,我发现您在这里提出了一些问题,但从未接受过任何问题的答案。 我强烈建议您这样做,否则人们会忽略您的问题。

关于java - 此查询似乎为 Module_Code 插入 null 而不是下拉选项中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4485309/

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