OSDN Git Service

AutoCloseable に対応
authorhayashi <hayashi.yuu@gmail.com>
Sat, 7 Jul 2018 09:46:10 +0000 (18:46 +0900)
committerhayashi <hayashi.yuu@gmail.com>
Sat, 7 Jul 2018 09:46:10 +0000 (18:46 +0900)
src/jp/co/areaweb/tools/database/DoSQL.java
src/jp/co/areaweb/tools/database/Select.java

index 868b64b..8fdab2b 100644 (file)
@@ -34,34 +34,24 @@ public class DoSQL
      * @param sqlfile SQL文が記述されたファイル。\r
      */\r
     public static void doSQL(String propertieFile, String sqlfile) {\r
-        Connection conn = null;\r
-        try {\r
-            conn = DatabaseTool.openDb(propertieFile);\r
-            \r
-            BufferedReader ds = new BufferedReader(new InputStreamReader(new FileInputStream(sqlfile)));\r
-            String sqlStr = "";\r
-            while ((sqlStr = ds.readLine()) != null) {\r
-               sqlStr = JapaneseString.trim(sqlStr);\r
-               if (sqlStr.endsWith(";")) {\r
-                       sqlStr = sqlStr.substring(0, sqlStr.length() - 1);\r
-               }\r
-                System.out.println(sqlStr);\r
-                sqlExecute(conn, sqlStr);\r
+        try (Connection conn = DatabaseTool.openDb(propertieFile)) {\r
+            try (BufferedReader ds = new BufferedReader(new InputStreamReader(new FileInputStream(sqlfile)))) {\r
+                String sqlStr = "";\r
+                while ((sqlStr = ds.readLine()) != null) {\r
+                    sqlStr = JapaneseString.trim(sqlStr);\r
+                    if (sqlStr.endsWith(";")) {\r
+                        sqlStr = sqlStr.substring(0, sqlStr.length() - 1);\r
+                    }\r
+                    System.out.println(sqlStr);\r
+                    sqlExecute(conn, sqlStr);\r
+                }\r
             }\r
-\r
-            ds.close();\r
             System.out.println("SQL Finished");\r
         }\r
-        catch(Exception e) {\r
-            // Print out the error message\r
+        catch(IOException | ClassNotFoundException | SQLException e) {\r
             System.out.println(e);\r
             e.printStackTrace();\r
         }\r
-        finally {\r
-            if (conn != null) {\r
-                DatabaseTool.closeDb(conn);\r
-            }\r
-        }\r
     }\r
 \r
     /**\r
@@ -70,7 +60,7 @@ public class DoSQL
      * @param sqlStr 実行するSQL文\r
      * @throws SQLException SQL実行エラー\r
      */\r
-    static void sqlExecute(Connection conn, String sqlStr) throws SQLException {\r
+    public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {\r
         // Create a statement object\r
         Statement stat = conn.createStatement();\r
 \r
index 6a5b84e..0efc78f 100644 (file)
@@ -2,10 +2,8 @@ package jp.co.areaweb.tools.database;
 \r
 import java.io.*;\r
 import java.sql.*;\r
-\r
 import jp.co.areaweb.tools.core.JapaneseString;\r
 \r
-\r
 /**\r
  * SQL文が記述されたファイルを読み込んで実行する。\r
  *\r
@@ -35,10 +33,7 @@ public class Select
      * @param sqlfile SQL文が記述されたファイル。\r
      */\r
     public static void doSQL(String propertieFile, String sqlfile) {\r
-        Connection conn = null;\r
-        try {\r
-            conn = DatabaseTool.openDb(propertieFile);\r
-            \r
+        try (Connection conn = DatabaseTool.openDb(propertieFile)) {\r
             BufferedReader ds = new BufferedReader(new InputStreamReader(new FileInputStream(sqlfile)));\r
             String sqlStr = "";\r
             while ((sqlStr = ds.readLine()) != null) {\r
@@ -49,20 +44,12 @@ public class Select
                 System.out.println(sqlStr);\r
                 sqlExecute(conn, sqlStr);\r
             }\r
-\r
-            ds.close();\r
             System.out.println("SQL Finished");\r
         }\r
         catch(Exception e) {\r
-            // Print out the error message\r
             System.out.println(e);\r
             e.printStackTrace();\r
         }\r
-        finally {\r
-            if (conn != null) {\r
-                DatabaseTool.closeDb(conn);\r
-            }\r
-        }\r
     }\r
 \r
     /**\r
@@ -72,21 +59,20 @@ public class Select
      * @throws SQLException SQL実行エラー\r
      */\r
     public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {\r
-               // テーブルの列情報を取り出す\r
-               Statement statement = conn.createStatement();\r
-               ResultSet rs = statement.executeQuery(sqlStr);\r
-               ResultSetMetaData rsmd = rs.getMetaData();\r
-               int count = rsmd.getColumnCount();\r
-               while (rs.next()) {\r
-                       for (int column = 1; column <= count; column++) {\r
-                               System.out.print("'"+ rs.getString(column) +"'");\r
-                               if (column < count) {\r
-                                       System.out.print(",");\r
-                               }\r
-                       }\r
-                       System.out.print("\n");\r
-               }\r
-               rs.close();\r
-               statement.close();\r
+        try (Statement statement = conn.createStatement();\r
+            ResultSet rs = statement.executeQuery(sqlStr))\r
+        {\r
+            ResultSetMetaData rsmd = rs.getMetaData();\r
+            int count = rsmd.getColumnCount();\r
+            while (rs.next()) {\r
+                for (int column = 1; column <= count; column++) {\r
+                    System.out.print("'"+ rs.getString(column) +"'");\r
+                    if (column < count) {\r
+                        System.out.print(",");\r
+                    }\r
+                }\r
+                System.out.print("\n");\r
+            }\r
+        }\r
     }\r
 }\r