Skip to content

Instantly share code, notes, and snippets.

@trplll
Created October 18, 2016 18:33
Show Gist options
  • Save trplll/581fe841db6bfba1139d4fe25bb79f18 to your computer and use it in GitHub Desktop.
Save trplll/581fe841db6bfba1139d4fe25bb79f18 to your computer and use it in GitHub Desktop.
Try vs try with resources
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