如何将多个值从表单控件提交到数据库
我在小组中有 8 个 MC 问题。提交后,我希望所有选定的答案都记录在数据库中。但是,我的代码只记录了 1 个问题。这是代码。 (注意:所有 jRadioButton 名称都不相同,因为它们位于一个面板中。)
以下是代码:
public void submitButtonClicked(){
for(int i=1;i<9;i++){
username = "Smith";
questionID = i;
if(jRadioButton1.isSelected()){answer = jRadioButton1.getText();}
else if(jRadioButton2.isSelected()){answer = jRadioButton2.getText();}
if(jRadioButton3.isSelected()){answer = jRadioButton3.getText();}
else if(jRadioButton4.isSelected()){answer = jRadioButton4.getText();}
// and So on until the question 8.
}
I have 8 MC questions in a panel. When submitted, I want all the selected answer to be recorded in the database. However, my code is only recording 1 question. Here is the code. (Note: All the jRadioButton names are not same since they are in one panel together.)
Here is the code :
public void submitButtonClicked(){
for(int i=1;i<9;i++){
username = "Smith";
questionID = i;
if(jRadioButton1.isSelected()){answer = jRadioButton1.getText();}
else if(jRadioButton2.isSelected()){answer = jRadioButton2.getText();}
if(jRadioButton3.isSelected()){answer = jRadioButton3.getText();}
else if(jRadioButton4.isSelected()){answer = jRadioButton4.getText();}
// and So on until the question 8.
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
考虑为每个 JRadioButton 集群创建 ButtonGroup 的数组或 ArrayList。然后,您可以使用 for 循环从每个 ButtonGroup 中获取选择,这是所选 JRadioButton 的模型,如果不为 null,则获取其 actionCommand 字符串。
例如,请在此处查看我的代码。
Consider creating an array or ArrayList of the ButtonGroups for each JRadioButton cluster. You can then use a for loop to get the selection from each ButtonGroup which is the model of the JRadioButton selected, and if not null, get its actionCommand String.
For example, please look at my code here.
在您的解决方案中,仅记录一个值,因为如果执行一个 if 语句,那么它将绕过所有其他 if 语句。
您可以创建一组 jradiobuttons,然后在 for 循环中使用它们,逐个遍历每个按钮,然后记录其答案。
In your solution only one value is recorded because if one if statement is executed then it will bypass all other else if statements.
You could create a array of jradiobuttons and then use them in for loop,traversing each button one by one and then recording its answer.