title: Swing form database connection
body: <p>I am new to Java and <a href="http://en.wikipedia.org/wiki/NetBeans" rel="nofollow">NetBeans</a>, and I am attempting to create a form that</p>
  
  <ol>
  <li>connects to a database using <a href="http://en.wikipedia.org/wiki/Java_Database_Connectivity" rel="nofollow">JDBC</a> connection</li>
  <li>reads information from seven columns and displays them on a jTable component already on the form</li>
  </ol>
  
  <p>I already have this working. I am now trying to optimize my code and use a better architecture to separate the database connection and the user interface (UI forms) code so that I can have a separate class to perform the connection and then simply call the method from this class in the code behind the button. The problem with using NetBeans and forms is that I don't know where to instantiate this class and such. Below is a cope of the class that I have created to perform the JDBC connection</p>
  
  <pre><code>public class ConnectionManager {
  private static String url = "jdbc:mysql://localhost:3306/prototypeeop";
  private static String driverName = "com.mysql.jdbc.Driver";
  private static String username = "root";
  private static String password = "triala";
  private static Connection con;
  private static String url;
  
  public static Connection getConnection() {
      try {
          Class.forName(driverName);
          try {
              con = DriverManager.getConnection(url, username, password);
          } catch (SQLException ex) {
              // log an exception. fro example:
              System.out.println("Failed to create the database connection.");
          }
      } catch (ClassNotFoundException ex) {
          // log an exception. for example:
          System.out.println("Driver not found.");
      }
      return con;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>This is already a <code>.java</code> file. I have a JForm, and I need to call this method behind the button. Here is how I do it in the form currently without using a connection class:</p>
  
  <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      DefaultTableModel model=(DefaultTableModel)jTable1.getModel();
      model.setRowCount(0);
      String sql="Select * from eopdata";
      try
      {
          Class.forName("com.mysql.jdbc.Driver");
          Connection con=(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/prototypeeop","root","jakamuga");
          Statement stmt=con.createStatement();
          ResultSet rs=stmt.executeQuery(sql);
          while(rs.next())
          {
              String Year=rs.getString("Year");
              String Month=rs.getString("Month");
              String Day=rs.getString("Day");
              String MJD=rs.getString("MJD");
              Double xarcsec=rs.getDouble("xarcsec");
              Double yarcsec=rs.getDouble("yarcsec");
              Double UT1UTCsec=rs.getDouble("UT1UTCsec");
              model.addRow(new Object[] { Year, Month, Day, MJD,xarcsec,yarcsec,UT1UTCsec});
          }
      }
      catch(Exception e) {
          JOptionPane.showMessageDialog(this, e.getMessage());
      }
  </code></pre>
  
  <p>How can I use the class instead of hard coding in the connection? I have already created the class but where do I instantiate it. Do I do it in the main of the form or do I do it in the actionevent code with the following code?</p>
  
  <pre><code>private Connection con = null;
  private Statement stmt = null;
  private ResultSet rs = null;
  
  con = ConnectionManager.getConnection();
  stmt = con.createStatement();
  rs = stmt.executeQuery(sql);
  </code></pre>