title: JAVA JDBC reusing connections
body: <p>I have a Java program in which I am doing some JDBC for select queries. Will it be advisable to call testDataBase() each time which inturns calls DBConnection() each time or I should reuse one connection for all the queries. Thanks in advance.</p>
  
  <pre><code>private  void testDataBase(String query){
      Connection con = DBConnection();
      Statement st = null;
      ResultSet rs = null;
  
      try {
          st = con.createStatement();
          rs = st.executeQuery(query);
          boolean flag = true;
          while (rs.next()) {
              String resultString = "";
              for(int i = 1; i &lt;=rs.getMetaData().getColumnCount();i++){
                  resultString=resultString+" "+  rs.getString(i);
              }
              System.out.println(resultString);
          }
      } catch (SQLException e) {
          e.printStackTrace();
  
      } finally {
          if (st != null) {
              try {
                  st.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
          if (rs != null) {
              try {
                  rs.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
          if (con != null) {
              try {
                  con.close();
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
      }
  }               
  
  
  
  private  Connection DBConnection() {
      final String method_name =  "DBConnection";
      Connection conn = null;
      try{
        Class.forName(driver).newInstance();
        conn = java.sql.DriverManager.getConnection(url,userName,password);
  
      }catch (ClassNotFoundException e) {
          System.out.println(e.getMessage());
      } catch (SQLException e) {
          System.out.println(e.getMessage());
      } catch (InstantiationException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      }
  
      return conn;
  }
  </code></pre>