title: Java Scope Try/Catch
body: <p>Fairly new to Java and am trying to better understand a few things.  I understand that try/catch statement variables only have scope within that statement.  I am trying to learn java db sql and am running into an issue.</p>
  
  <pre><code>final String DB_URL = "jdbc:derby://localhost:1527/Customers";    
  Connection conn = DriverManager.getConnection(DB_URL);
  </code></pre>
  
  <p>This creates the connection with my database.  I need to have this in a try/catch statement.  I want to access the conn variable again at the end of the program to close the connection.  I tried to declare this variable at the top of my program but it won't let me as it needs a try/catch.  What is the best way to tackle this?</p>
  
  <p>As suggested below I created it as a variable.  I am trying to close the connection with my exit button but the code is not getting executed.  With the if statement the program doesn't close nor does the System.out.print message fires.  The connection is starting OK but not closing.</p>
  
  <pre><code>if (conn != null){
     conn.close();
     System.out.println("Connection Closed");
     System.exit(0);}
  </code></pre>