title: Static Connection method in Java
body: <p>I'm confused regarding the use of static Connection to connect database. From what I've read, static connection shouldn't be used. This is my method:</p>
  <pre><code>private static Connection conn()
      {
          
          Connection connection = null;
              
          try {
              
              Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
              connection = (Connection)DriverManager.getConnection(&quot;jdbc:mysql://localhost:3306/test&quot;, &quot;root&quot;, &quot;&quot;);
              
  
          } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException ex) {
              Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
          }
          
          
          return connection;
          
      }
  </code></pre>
  <p>Then to get results a new method:</p>
  <pre><code>public void getResult(ArrayList test)
      {
          
          try
          {
              
              Connection conn = conn();
              
              String sql = &quot;select id from test&quot;;
              
              PreparedStatement ps = conn.prepareStatement(sql);
              ResultSet rs = ps.executeQuery();
              
              while(rs.next())
              {
                  
                 test.add(rs.getInt(&quot;id&quot;));
                  
              }
              
              conn.close();
              
          } catch (SQLException ex) {
              Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
          }
          
      }
  </code></pre>
  <p>Now, I don't know if this is safe. The connection is being started new every time method is called(?), and I' am closing it. Certainly connection pooling would be good for performance, but is this thread safe?</p>