Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.app11;
- import java.sql.Connection;
- import java.sql.SQLException;
- /**
- *
- * @author Admin
- */
- public class Main {
- // test1
- public static void test1() {
- // try-catch-finally
- Connection c = null;
- try {
- c = DataBaseUtil.getConnection();
- DataBaseUtil.printMeta(c);
- } catch (SQLException ex) {
- ex.printStackTrace();
- } finally {
- // закрываем соединение - используем close()
- if (c != null) try {
- c.close();
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- }
- // try-with-resource
- public static void test2() {
- // close - вызывается для объекта типа Connection
- try (Connection c = DataBaseUtil.getConnection()) {
- DataBaseUtil.printMeta(c);
- } catch (SQLException ex) {
- ex.printStackTrace();
- }
- }
- //
- public static void main(String[] args) {
- test2();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement