Created
October 18, 2016 18:33
-
-
Save trplll/581fe841db6bfba1139d4fe25bb79f18 to your computer and use it in GitHub Desktop.
Try vs try with resources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.trplcd.jdbc; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
/** | |
* Created by ace01359 on 10/10/2016. | |
*/ | |
public class TestSqlCon { | |
public static void main(String[] args) throws SQLException { | |
Connection conn1 = null; | |
try { | |
conn1 = DBUtil.getConnection(DBType.AS400); | |
System.out.println("Connection Established to AS400"); | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
} finally { | |
conn1.close(); | |
} | |
//Try with resources | |
//TRY{} | |
//Try (resource that needs to be closed when finished){} | |
//https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html | |
try (conn1 = DBUtil.getConnection(DBType.AS400)){ | |
System.out.println("Connection Established to AS400"); | |
} catch (SQLException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment