0

I am a beginner and I am trying to figure out a way to have a jbutton automatically be created when a new entry is inserted into the MySQL database column. The text of this button will be pulled in from an entry in the database. I have figured out how to pull in the information from the table to be used as the jbutton text, however, I feel that there has to be an easier way to do it. Any ideas on either of these issues? I have included a snippet of my code below. Thanks!

public class JuniorSkills extends javax.swing.JFrame {

/**
 * Creates new form JuniorSkills
 */
public JuniorSkills() throws ClassNotFoundException {
initComponents();
    Connection conn = null;
    Statement statement = null;
    ResultSet rs = null;


    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/simlab","root","password");
        System.out.println("Connected to database");

        statement = conn.createStatement();
        rs = statement.executeQuery("SELECT * FROM skill_table WHERE class='junior'");

        int count = 0;
        while(rs.next())
        {
            count++;
            String gotit = rs.getString(1);
            //System.out.println(gotit);
            if(count==1)
            {
                Skill1.setText(rs.getString(1));
            }
            else if(count==2)
            {
                skill2.setText(rs.getString(1));
            }
            else if(count==3)
            {
                skill3.setText(rs.getString(1));
            }
            else if(count==4)
            {
                skill4.setText(rs.getString(1));
            }
            else if(count==5)
            {
                Skill5.setText(rs.getString(1));
            }
            else if(count==6)
            {
                skill6.setText(rs.getString(1));
            }
            else if(count==7)
            {
                Skill7.setText(rs.getString(1));
            }
            else if(count==8)
            {
                Skill8.setText(rs.getString(1));
            }
            else if(count==9)
            {
                Skill9.setText(rs.getString(1));
            }
            else if(count==10)
            {
                Skill10.setText(rs.getString(1));
            }
        }
    } catch (SQLException ex) {
        Logger.getLogger(JuniorSkills.class.getName()).log(Level.SEVERE, null, ex); 
    }



}
2
  • 1
    If you have a fixed number of buttons, create an array of buttons Commented Mar 20, 2013 at 0:01
  • Do not sabotage your own question! Edits rolled back. Commented Mar 20, 2013 at 1:15

1 Answer 1

1

If you only ever have a fixed number of rows, you could create an array of JButtons

private JButton[] myFixedListOfButtons;

//...//

myFixedListOfButtons = new JButton[10];

//...//

while(rs.next())
{
    String gotit = rs.getString(1);
    if (count < myFixedListOfButtons.length) 
    {
        myFixedListOfButtons[count].setText(gotit);
    }
    count++;
}

Alternatively, if you have a variable number of rows, you should simply create the buttons as you need

removeAll(); // remove all the old buttons...
while(rs.next())
{
    String gotit = rs.getString(1);
    JButton btn = new JButton(gotit);
    add(btn);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you both. I will give it a shot and see what I can come up with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.