title: How to connect java database connection to many jFrames?
body: <p>I have created a class called <strong>DBConnection</strong> to connect the sql database. I also have a jFrame called <strong>AdminLogin</strong>. What I want to know is, how to pass the database connection to the <strong>AdminLogin</strong> class without having to type the whole database connection codes.</p>
  
  <p>Here is my DBConnection class</p>
  
  <pre><code>package ltdasystem;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  
  
  public class DBConnection {
  
      private Connection conn = null;
      private String query = null;
      private ResultSet rs;
      private final String url=("jdbc:sqlserver://localhost:1433;databaseName=LeisureToursDatabase");
      private final String usrName = "sa";
      private final String pwd= "p@ssword13";
  
      public DBConnection() {
          try{
              conn = DriverManager.getConnection(url, usrName, pwd);
              query = "SELECT * from UserATable";
              PreparedStatement stm = conn.prepareStatement(query);
              rs = stm.executeQuery();
              while (rs.next()){
                  String userName = rs.getString("Username");
                  String password = rs.getString("Password");
  
                  System.out.println("Username : " + userName + "\n" + "Password : " + password); 
  
              }
              conn.close();
  
          }catch (SQLException e){
  
            System.out.println(e.getMessage());
  
          }
  
      }
  
    }
  </code></pre>
  
  <p>Here is my AdminLogin. The way I have done it now works fine but I want to do it without having to type all the database connection codes.</p>
  
  <pre><code>import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import javax.swing.JOptionPane;
  
  
  /**
   *
   * @author Yasitha
   */
  public class AdminLogin extends javax.swing.JFrame {
  
       Connection conn = null;
       String query = null;
       ResultSet rs;
      private final String url=("jdbc:sqlserver://localhost:1433;databaseName=LeisureToursDatabase");
      private final String usrName = "sa";
      private final String pwd= "p@ssword13";
  
  
      /**
       * Creates new form AdminLogin
       */
      public AdminLogin() {
          initComponents();
      }
  
  
  
      private void LoginButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
          //DBConnection db = new DBConnection();
  
         try{
  
               conn = DriverManager.getConnection(url, usrName, pwd);
               query = "SELECT * FROM UserATable WHERE Username = '" + userName.getText()
                      + "' AND Password = " + password.getText();
  
  
            PreparedStatement stm = conn.prepareStatement(query);
              rs = stm.executeQuery();
              if(rs.next()){
                  dispose();
                  String x = rs.getString("Username");
                 AdminMenu admenu = new AdminMenu();
                 admenu.setVisible(true);
                 admenu.generate(x);
  
         }
  
         }catch (SQLException ex) {
  
             JOptionPane.showMessageDialog(null, ex);
             //return null;
  
          } finally {
  
  
          }   
      }                                           
  
  
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new AdminLogin().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private javax.swing.JButton BackButton;
      private javax.swing.JButton LoginButton;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JLabel jLabel2;
      private javax.swing.JPasswordField password;
      private javax.swing.JTextField userName;
      // End of variables declaration                   
  }
  </code></pre>