birdog9999
birdog9999

Reputation: 1

I'm trying to find a more efficient way than using a bunch of switch statments

I am having trouble figuring out how to make my method more efficient I’m using a switch to fill in some textfields which isn’t the most efficient way to do it but I have tried and tried to come up with a better way but I am really stuck. I have 130 sets of 3 textfields. I get the information to fill the fields from a database and then fill in each set with a switch statement. This slows down the result but not to a point that I couldn’t live with it, but I am looking for a more efficient way to handle this. I would really appreciate any help I can get.

Here is my code

@FXML
       private void GetAssingActionPerformed(ActionEvent event) {
           // Fill in the run fields with the information stored in the runassingn db table.
        String user = "root";
        String password = "";

        try {
            // Get connection.
            Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kennelmanagment1", user, password);

            // Create statement.
            Statement myStmt = myConn.createStatement();

              // Execute query.
            ResultSet myRs = myStmt.executeQuery("SELECT * FROM runasign");

           int count = 1;        
            // Process the result.
            while (myRs.next()) {
               switch (count)  {
                   case 1:
                        Names1.setText(myRs.getString("name").toUpperCase());
                        Sex1.setText(myRs.getString("sex").toUpperCase());
                        Owners1.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 2:
                        Names2.setText(myRs.getString("name").toUpperCase());
                        Sex2.setText(myRs.getString("sex").toUpperCase());
                        Owners2.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 3:
                        Names3.setText(myRs.getString("name").toUpperCase());
                        Sex3.setText(myRs.getString("sex").toUpperCase());
                        Owners3.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 4:
                        Names4.setText(myRs.getString("name").toUpperCase());
                        Sex4.setText(myRs.getString("sex").toUpperCase());
                        Owners4.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 5:
                        Names5.setText(myRs.getString("name").toUpperCase());
                        Sex5.setText(myRs.getString("sex").toUpperCase());
                        Owners5.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 6:
                        Names6.setText(myRs.getString("name").toUpperCase());
                        Sex6.setText(myRs.getString("sex").toUpperCase());
                        Owners6.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 7:
                        Names7.setText(myRs.getString("name").toUpperCase());
                        Sex7.setText(myRs.getString("sex").toUpperCase());
                        Owners7.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 8:
                        Names8.setText(myRs.getString("name").toUpperCase());
                        Sex8.setText(myRs.getString("sex").toUpperCase());
                        Owners8.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 9:
                        Names9.setText(myRs.getString("name").toUpperCase());
                        Sex9.setText(myRs.getString("sex").toUpperCase());
                        Owners9.setText(myRs.getString("owner").toUpperCase());
                        break;
                    case 10:
                        Names10.setText(myRs.getString("name").toUpperCase());
                        Sex10.setText(myRs.getString("sex").toUpperCase());
                        Owners10.setText(myRs.getString("owner").toUpperCase());
                        break;

                    //As you can see I have removed the redundant cases 11 thur 125




                         case 126:
                        Names126.setText(myRs.getString("name").toUpperCase());
                        Sex126.setText(myRs.getString("sex").toUpperCase());
                        Owners126.setText(myRs.getString("owner").toUpperCase());
                        break;      
                      case 127:
                        Names127.setText(myRs.getString("name").toUpperCase());
                        Sex127.setText(myRs.getString("sex").toUpperCase());
                        Owners127.setText(myRs.getString("owner").toUpperCase());
                        break;        
                      case 128:
                        Names128.setText(myRs.getString("name").toUpperCase());
                        Sex128.setText(myRs.getString("sex").toUpperCase());
                        Owners128.setText(myRs.getString("owner").toUpperCase());
                        break;        
                      case 129:
                        Names129.setText(myRs.getString("name").toUpperCase());
                        Sex129.setText(myRs.getString("sex").toUpperCase());
                        Owners129.setText(myRs.getString("owner").toUpperCase());
                        break;        
                      case 130:
                        Names130.setText(myRs.getString("name").toUpperCase());
                        Sex130.setText(myRs.getString("sex").toUpperCase());
                        Owners130.setText(myRs.getString("owner").toUpperCase());
                        break;         
                }

                count ++;
               }

            } catch (Exception exc) {
            exc.printStackTrace();
}
    }    

Upvotes: 0

Views: 65

Answers (4)

Carcigenicate
Carcigenicate

Reputation: 45760

Since you haven't shown how those Names#, Owner# and Sex# variables are creates, I can only give you a partial answer, but it should help.

Rarely should you ever start ending variable names with numbers, and then create a ton of them with that pattern (and certainly not 100+!).

Put the variables into an array, then loop over the array. This is the point of arrays, for this very problem you're having.

List<Name> names = new ArrayList<>();

List<Owner> owners = new ArrayList<>();

List<Sex> sexes = new ArrayList<>();

// Fill the arrays with all the data

for (int i = 0; i < names.size(); i++) {
    names.get(i).setText(myRs.getString("name").toUpperCase());
    sexes.get(i).setText(myRs.getString("sex").toUpperCase());
    owners.get(i).setText(myRs.getString("owner").toUpperCase());

}

Note, I have no idea what type those variables are exactly, so I used the types Name, Sex, and Owner as placeholders. You'll need to change those to the actual types of the objects when you create the ArrayLists.

Upvotes: 0

Abrar Hossain
Abrar Hossain

Reputation: 2702

I think trying to encapsulate Name, Sex and Owner under one entity ( I.e., a class would be a better option). The sex type can be an enum class; .

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726699

All these cases are redundant: all you need is a single array of 130 items, each one consisting of three fields to represent Name, Gender, and Owners. Your loop should look like this:

while (myRs.next()) {
    DisplayBlock obj = blocks[count++];
    obj.getNames().setText(myRs.getString("name").toUpperCase());
    obj.getGender().setText(myRs.getString("sex").toUpperCase());
    obj.getOwners().setText(myRs.getString("owner").toUpperCase());
}

Class DisplayBlock (this may not be the best name, so feel free to rename it) should have getters for the three parts into which you would like to write the data that you read from the database.

Upvotes: 2

Mibac
Mibac

Reputation: 9458

You should create a list. Names, Sex and Owner lists to be exact. You can create them like so:

List<your type> namesList = new ArrayList<>();

Then do the same with Sex and Owner. Now that that's done we can replace that giant switch with:

namesList.get(count).setText(myRs.getString("name").toUpperCase());
sexList.get(count).....
ownerList.get(count)...

Upvotes: 0

Related Questions