title: Data submission not working while using Swing application otherwise it works normally
body: <p>When i write PreparedStatement in main function, it works perfectly and submits the data but when i write in in actionPerformed function it doesn't work, why??
  I don't think I'm doing anything wrong.</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.event.*;
  import java.sql.*;
  
  public class EnterData {
  
      static void insertData(int rn, String fn, String ln) throws ClassNotFoundException, SQLException {    
          Class.forName("com.mysql.jdbc.Driver");
          try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo","root","12345")) {
              String query = "insert into student (rollno, fname, lname)" + " values (?, ?, ?)";
              PreparedStatement preparedStmt = conn.prepareStatement(query);
              preparedStmt.setInt (1, rn);
              preparedStmt.setString (2, fn);
              preparedStmt.setString (3, ln);
              preparedStmt.execute();
          }
      }
  
      public static void main(String[] args) {
          JFrame mainFrame = new JFrame("Submit Information");
          mainFrame.setSize(400, 300);
          mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          mainFrame.setLocationRelativeTo(null);
          mainFrame.setLayout(null);
  
          JLabel heading = new JLabel("Enter Data");
          heading.setBounds(150, 10, 100, 20);
          mainFrame.add(heading);
  
          JLabel rollnoLabel = new JLabel("Roll Number");
          rollnoLabel.setBounds(70, 60, 100, 20);
          mainFrame.add(rollnoLabel);
          JTextField rollno = new JTextField();
          rollno.setBounds(220, 60, 100, 20);
          mainFrame.add(rollno);
  
          // Taking First Name
          JLabel name1 = new JLabel("First Name");
          name1.setBounds(70, 110, 100, 20);
          mainFrame.add(name1);
          JTextField fname = new JTextField();
          fname.setBounds(220, 110, 100, 20);
          mainFrame.add(fname);
  
          // Taking Last Name
          JLabel name2 = new JLabel("Last Name");
          name2.setBounds(70, 160, 100, 20);
          mainFrame.add(name2);
          JTextField lname = new JTextField();
          lname.setBounds(220, 160, 100, 20);
          mainFrame.add(lname);
  
          // Submit and Reset Button
          JButton submit = new JButton("Submit");
          submit.setBounds(70, 220, 100, 20);
          mainFrame.add(submit);
          JButton reset = new JButton("Reset");
          reset.setBounds(220, 220, 100, 20);
          mainFrame.add(reset);
  
          // Frame Visibility
          mainFrame.setVisible(true);
  
          submit.addActionListener(new ActionListener(){
              @Override
              public void actionPerformed(ActionEvent e) {
                  try {
                      int rn = Integer.parseInt(rollno.getText());
                      String fn = fname.getText();
                      String ln = lname.getText();
  
                      insertData(rn, fn, ln);
  
                      JOptionPane.showMessageDialog(null, "Submitted Successfully...!!!", "Submission Prompt", JOptionPane.PLAIN_MESSAGE);
                  } catch (Exception ex) {
                      System.err.println("Got an exception!");
                      System.err.println(ex.getMessage());
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>I've tried this and it works perfectly but when i put those statements in actionPerformed function they don't work.</p>
  
  <pre><code>import java.sql.*;
  
  public class DatabaseTest {
  
      public static void main(String[] args) {
          try {
              Class.forName("com.mysql.cj.jdbc.Driver");
              Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentinfo","root","12345");
  
              String query = "insert into student (rollno, fname, lname)" + " values (?, ?, ?)";
  
              PreparedStatement preparedStmt = conn.prepareStatement(query);
              preparedStmt.setInt (1, 6505);
              preparedStmt.setString (2, "Prince");
              preparedStmt.setString (3, "Saini");
  
              preparedStmt.execute();
              conn.close();
          } catch (Exception e) {
              System.err.println("Got an exception!");
              System.err.println(e.getMessage());
          }
      }
  }
  </code></pre>