你能做组合框方法吗

时间:2019-03-13 09:37:11

标签: java mysql

这是我想在各种组合框上执行的代码块

重要信息:

AssignRollno()是我创建的一种方法,该方法返回一个int值,并如其所说,它将rollno分配给新用户。字符串“ a”是用户的名称。方法execute()执行mySQL中的字符串。

问题:

现在,有名为d1 d2 d3 ..... d8的组合框。我不想将整个代码编写8次,而是要编写一次并使它在所有组合框中执行。如果可能的话。因此,我想像for循环一样执行它(显然for循环不会让我这样做)。

在下面的代码中,我只希望'x'保持从1到8的增量,并执行8次代码块  有什么办法做到吗?

 String a=t1.getText();
 int i=AssignRollno();
 int j=dx.getSelectedIndex();
 if(j==0)
 {
 String str1="insert into dx values("+i+",'"+a+"')";
 execute(str1);
 String str2="insert into t"+i+"1"+" values(0)";
 execute(str2);
 }
 else
{
String str3="insert into t"+i+"1 values("+j+")";
execute(str3);
}

2 个答案:

答案 0 :(得分:1)

如何使用逗号?

示例

插入dx值

("+i+",'"+a+"'),

("+i+",'"+a+"'),

("+i+",'"+a+"'),

("+i+",'"+a+"');

问题重复Inserting multiple rows in mysql

答案 1 :(得分:1)

我不太确定您想要什么。但是您可以为组合框使用数组:

    ComboBox[] comoboboxes = new ComboBox[8];
    comoboboxes[0] = d1;
    comoboboxes[1] = d2;
    comoboboxes[2] = d3;
    comoboboxes[3] = d4;
    comoboboxes[4] = d5;
    comoboboxes[5] = d6;
    comoboboxes[6] = d7;
    comoboboxes[7] = d8;

    for (int x = 0; x < 8; x++) {
        String a = t1.getText();
        int i = AssignRollno();
        int j = comboboxes[x].getSelectedIndex();
        if (j == 0) {
            String str1 = "insert into dx values(" + i + ",'" + a + "')";
            execute(str1);
            String str2 = "insert into t" + i + "1" + " values(0)";
            execute(str2);
        } else {
            String str3 = "insert into t" + i + "1 values(" + j + ")";
            execute(str3);
        }
    }