Advertisement
sergAccount

Untitled

Feb 13th, 2021
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.30 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.app11;
  7.  
  8. import java.sql.Connection;
  9. import java.sql.SQLException;
  10.  
  11. /**
  12.  *
  13.  * @author Admin
  14.  */
  15. public class Main {
  16.  
  17.     // test1
  18.     public static void test1() {
  19.         // try-catch-finally
  20.         Connection c = null;
  21.         try {
  22.             c = DataBaseUtil.getConnection();
  23.             DataBaseUtil.printMeta(c);
  24.         } catch (SQLException ex) {
  25.             ex.printStackTrace();
  26.         } finally {
  27.             // закрываем соединение - используем close()
  28.             if (c != null) try {
  29.                 c.close();
  30.             } catch (SQLException ex) {
  31.                 ex.printStackTrace();
  32.             }
  33.         }
  34.     }
  35.     // try-with-resource
  36.     public static void test2() {
  37.         // close - вызывается для объекта типа Connection
  38.         try (Connection c = DataBaseUtil.getConnection()) {
  39.             DataBaseUtil.printMeta(c);
  40.         } catch (SQLException ex) {
  41.             ex.printStackTrace();
  42.         }
  43.     }
  44.  
  45.     //
  46.     public static void main(String[] args) {
  47.         test2();
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement