title: JDBC Connection using different files
body: <p>First of all sorry for the name of the title, but i dont know how to put another one since english is not my native language.</p>
  
  <p>I have the following method to connect to a database:</p>
  
  <pre><code>import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.SQLException;
  
  public class PgConnect {
      public  void connect() {
          Connection connection = null;
          try {
              connection = DriverManager.getConnection("jdbc:postgresql://pgserver:5432/db", "test","test123");
          } catch (SQLException e) {
              System.out.println("Connection Failed! Check output console");
              e.printStackTrace();
              return;
          }
          if (connection != null) {
              System.out.println("Connection working");
          } else {
              System.out.println("Failed to make connection!");
          }
      }
  }
  </code></pre>
  
  <p>And what I need to do is include the method from the <code>PgConnect</code> in the code below. Basically I need this because I have many types of SQL's calls to database, and changing this to that way would be easy to maintain, since the credentials/host would be on one file only. </p>
  
  <p>I believe the change should be where I have the comment </p>
  
  <pre><code>// i want to change this, for using the method on the first file. 
  </code></pre>
  
  <p>Please correct me if I'm wrong.</p>
  
  <pre><code>import java.sql.DriverManager;
  import java.sql.Connection;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  public class ReturnResults {
  
      public static void main(String[] args) {
          Connection connection = null;
          try {
                  // i want to change this, for using the method on the first file.
              connection = DriverManager.getConnection("jdbc:postgresql://pgserver:5432/db", "test","test123");
  
          } catch (SQLException e) {
              System.out.println("Connection Failed! Check output console");
              e.printStackTrace();
              return;
          }
          if (connection != null) {
  
              String result = null;
              String selectString = "select * from mwp.servers where env='TEST' order by server";
              //result ="iServer\tLabel\n";
  
              try {
  
                  Statement stmt = connection.createStatement();
                  ResultSet rs = stmt.executeQuery(selectString);
  
                  while (rs.next()) {
  
                      String iEnv     = rs.getString("env");
                      String iServer  = rs.getString("iserver");
                      String iLabel   = rs.getString("label");
                      String iTitle   = rs.getString("title");
                      String iLogin   = rs.getString("login");
  
                      result=iEnv+"\t"+ iServer+"\t"+iLabel+"\t"+iTitle+"\t"+iLogin;
  
                      System.out.println(result);
                  }
                  stmt.close();
                  connection.close();
  
              } catch(SQLException ex) {
                  System.err.println("SQLException: " + ex.getMessage());
              }
  
          } else {
              System.out.println("Failed to make connection!");
          }
      }
  }
  </code></pre>
  
  <p>I know how to do this on Perl, but I don't have any practice in Java.</p>