Auto Refresh JComboBox on button click.
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/rms","root","root");
PreparedStatement pst=(PreparedStatement) con.prepareStatement("insert into categories(name) values(?)");
pst.setString(1,jTextField8.getText());
pst.executeUpdate();
con.close();
pst.close();
jTextField8.setText("");
}
catch(Exception e)
{
System.out.println(e);
}
fill();
}
public void fill()
{
DefaultComboBoxModel model = new DefaultComboBoxModel();
try
{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3305/rms","root","root");
String sql="select name from categories";
java.sql.PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs = pst.executeQuery(sql);
while(rs.next())
{
String name=rs.getString(1);
model.addElement(name);
}
jComboBox1.setModel(model);
con.close();
pst.close();
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, e);
}
}