1

This is a block of code which I want to execute on various Combo Boxes

Important Information:

AssignRollno() is a method which i created which returns a int value and as it says it assigns a rollno to the new user. String 'a' is the name of the user. The method execute() executes the string in mySQL.

Problem:

Now, there are Combo Boxes named as d1 d2 d3 ..... d8. Instead of writing the whole code 8 times I want to write it once and make it execute for all the Combo Boxes. If possible. So, I want to execute it like a for a loop ( obviously a for loop would not let me do it ).

In the following code i just want 'x' to keep increment from 1 to 8 and execute the block of code 8 times Is there any way to do that ??

 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);
}
JJIqbal
  • 630
  • 1
  • 8
  • 23

2 Answers2

1

how about use Comma?

example

insert into dx values

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

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

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

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

prob duplicated Inserting multiple rows in mysql

Mikev
  • 2,012
  • 1
  • 15
  • 27
mackbex
  • 69
  • 4
  • I think you misunderstood my question I want to get the information from d1 combo box run the code then getSelectedIndex() from d2 run the code and so on... – Yash Yelmame Mar 13 '19 at 10:22
1

I'm not quite sure what exact you want. But you could use an array for your comboboxes:

    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);
        }
    }
Ralf Renz
  • 1,061
  • 5
  • 7