* @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
* @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
\r
import java.io.*;\r
import java.sql.*;\r
-\r
import jp.co.areaweb.tools.core.JapaneseString;\r
\r
-\r
/**\r
* SQL文が記述されたファイルを読み込んで実行する。\r
*\r
* @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
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
* @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