我是一个初学者,我正在尝试找出一种方法,以便在将新条目插入 MySQL 数据库列时自动创建一个 jbutton。此按钮的文本将从数据库中的条目中提取。我已经想出了如何从表格中提取信息以用作 jbutton 文本,但是,我觉得必须有一种更简单的方法来做到这一点。关于这些问题的任何想法?我在下面包含了我的代码片段。谢谢!
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);
}
}