title: How to put database connection information to a separate file?
body: <p>In PHP, we simply create a file, e.g. <code>connect.php</code>
  and then we include this file to each and every page we like: </p>
  
  <p>In Java I created  a separate file in the same project folder called <code>connect.java</code> and tried  to call it in other files so that those files get the database connection but no luck so far  . </p>
  
  <p>I tried last time but what amazed me is that some variables from the connect page I was not able to use them in my main program . can someone please help me . bellow is the code I use to connect to mysql and also I prinde some stuff</p>
  
  <p>but I wanna make it a separate file which don’t print nothing just to connect and I just print all what I want in a different file</p>
  
  <p>so if I would talk in a php term I wanna include it into other files where connectivity might be needed.</p>
  
  <pre><code>try {
                  Class.forName("com.mysql.jdbc.Driver");
                  System.out.println("Driver loading success!");
                  String url = "jdbc:mysql://localhost:3306/saloon";
                  String name = "root";
                  String password = "";
                  try {
  
                      java.sql.Connection con = DriverManager.getConnection(url, name, password);
                      System.out.println("Connected.");
               // pull data from the database 
      java.sql.Statement stmts = null;
      String query = "select  userid, username, name , address, hairstyle from saloonuser ";
      stmts = con.createStatement();
      ResultSet rs = stmts.executeQuery(query);
      int li_row = 0;
      while(rs.next()){
          mysTable.setValueAt(rs.getString("username"),li_row,0);
          mysTable.setValueAt(rs.getString("name"),li_row,1);
          mysTable.setValueAt(rs.getString("address"),li_row,2);
          mysTable.setValueAt(rs.getString("hairstyle"),li_row,3);
  
          int userid = rs.getInt("userid");
          String username = rs.getString("username");
          String name1     = rs.getString("name");
          System.out.println(name1);
          li_row++;
          } } catch (SQLException e) {
                      e.printStackTrace();
                  }
  
              } catch (ClassNotFoundException e) {
                  e.printStackTrace();
              }
  
          }
  
      }
  </code></pre>