title: Java: connecting to db to fetch data
body: <p>I have a question regarding Java when fetching data from, lets say MySQL database. As of now I need to write quite a lot of redundant code when fetching data. And I wonder if there is a better way to do that.</p>
  
  <p>E.g. I have an method which fetch data from a table A. The method for that will look something like this then</p>
  
  <pre><code>public void readDataBase() throws Exception {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              connect = DriverManager
                      .getConnection("jdbc:mysql://localhost/feedback?"
                              + "user=sqluser&amp;password=sqluserpw");
  
              statement = connect.createStatement();
              resultSet = statement
                      .executeQuery("select * from FEEDBACK.COMMENTS");
              writeResultSet(resultSet);              
  
          } catch (Exception e) {
              throw e;
          } finally {
              close();
          }
  
      }
  </code></pre>
  
  <p>I wonder if there's a better way to write a method such as this one. Because it gets quite ugly when you have to write code such as this, namely that you have to write those line to <code>getConnection</code> all the time in every method that fetch data from the database.</p>