/**
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
 */
package com.codeup.db;

import java.io.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {
    private final Connection connection;

    public Database(Connection connection) {
        this.connection = connection;
    }

    public void create(String database) throws SQLException {
        Statement statement = connection.createStatement();
        statement.executeUpdate(
            String.format("CREATE DATABASE IF NOT EXISTS %s", database)
        );
        statement.close();
    }

    public void drop(String database) throws SQLException {
        Statement statement = connection.createStatement();
        statement.executeUpdate(
            String.format("DROP DATABASE IF EXISTS %s", database)
        );
        statement.close();
    }

    public void use(String database) throws SQLException {
        Statement statement = connection.createStatement();
        statement.executeUpdate(String.format("USE %s", database));
        statement.close();
    }

    public void importFile(
        String sqlFile
    ) throws IOException, SQLException {
        String line;
        StringBuilder queries = new StringBuilder();

        BufferedReader reader = new BufferedReader(new FileReader(
            new File(sqlFile)
        ));
        while ((line = reader.readLine()) != null) {
            queries.append(line).append(" ");
        }
        reader.close();

        Statement statement = connection.createStatement();

        String[] ddlStatements = queries.toString().split(";");
        for (String query : ddlStatements) {
            if (!query.trim().isEmpty()) {
                statement.executeUpdate(query);
            }
        }
        statement.close();
    }
}

@@ -449,32 +449,37 @@
 ption %7B%0A        
+try (
 Statement statem
@@ -504,34 +504,40 @@
 reateStatement()
-;%0A
+) %7B%0A    
         statemen
@@ -557,32 +557,36 @@
 te(%0A            
+   
+ 
 String.format(%22C
@@ -629,32 +629,36 @@
 tabase)%0A        
+    
 );%0A        state
@@ -652,34 +652,17 @@
         
-statement.close();
+%7D
 %0A    %7D%0A%0A
@@ -721,32 +721,37 @@
 ption %7B%0A        
+try (
 Statement statem
@@ -776,34 +776,40 @@
 reateStatement()
-;%0A
+) %7B%0A    
         statemen
@@ -817,32 +817,36 @@
 .executeUpdate(%0A
+    
             Stri
@@ -895,32 +895,36 @@
 tabase)%0A        
+    
 );%0A        state
@@ -918,34 +918,17 @@
         
-statement.close();
+%7D
 %0A    %7D%0A%0A
@@ -986,32 +986,37 @@
 ption %7B%0A        
+try (
 Statement statem
@@ -1049,18 +1049,24 @@
 tement()
-;%0A
+) %7B%0A    
         
@@ -1133,34 +1133,17 @@
         
-statement.close();
+%7D
 %0A    %7D%0A%0A
@@ -1164,16 +1164,19 @@
 d import
+SQL
 File(%0A  
@@ -1188,23 +1188,20 @@
  String 
-sqlFile
+path
 %0A    ) t
@@ -1238,29 +1238,8 @@
 n %7B%0A
-        String line;%0A
     
@@ -1284,24 +1284,42 @@
 Builder();%0A%0A
+        try (%0A    
         Buff
@@ -1376,51 +1376,66 @@
 der(
-%0A            new File(sqlFile)%0A        ));%0A
+new File(path)))%0A        ) %7B%0A            String line;%0A    
     
@@ -1495,16 +1495,20 @@
         
+   
+ 
 queries.
@@ -1533,32 +1533,36 @@
 d(%22 %22);%0A        
+    
 %7D%0A        reader
@@ -1559,23 +1559,9 @@
     
-reader.close();
+%7D
 %0A%0A  
@@ -1566,16 +1566,21 @@
         
+try (
 Statemen
@@ -1621,19 +1621,24 @@
 tement()
-;%0A%0A
+) %7B%0A    
         
@@ -1701,16 +1701,20 @@
         
+  
+  
 for (Str
@@ -1754,16 +1754,20 @@
         
+    
 if (!que
@@ -1785,24 +1785,28 @@
 sEmpty()) %7B%0A
+    
             
@@ -1845,34 +1845,42 @@
 y);%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1885,26 +1885,9 @@
     
-statement.close();
+%7D
 %0A   

Use try-with-resource statements in Database class.