1 package jp.co.areaweb.tools.database;
\r
5 import jp.co.areaweb.tools.core.JapaneseString;
\r
8 * SQL文が記述されたファイルを読み込んで実行する。
\r
10 * ・SELECTコマンドは実行できない。
\r
14 /** コマンドラインラインからdoSQLを実行する。
\r
15 * java DoSQL [SQLファイル名]
\r
16 * exp) java dbtool.DoSQL sqlfile.sql
\r
19 static public void main(String[] args) {
\r
20 if (args.length < 2) {
\r
21 System.out.println("exp: java jp.co.areaweb.tools.database.Select [propertieFile] [sqlfile]");
\r
24 Select.doSQL(args[0], args[1]);
\r
29 * sqlfileの中のSQL文を1行づつ実行する。
\r
31 * ・一つのSQL文は1行で記述すること。(途中改行はだめ)
\r
32 * @param propertieFile プロパティファイル
\r
33 * @param sqlfile SQL文が記述されたファイル。
\r
35 public static void doSQL(String propertieFile, String sqlfile) {
\r
36 try (Connection conn = DatabaseTool.openDb(propertieFile)) {
\r
37 BufferedReader ds = new BufferedReader(new InputStreamReader(new FileInputStream(sqlfile)));
\r
39 while ((sqlStr = ds.readLine()) != null) {
\r
40 sqlStr = JapaneseString.trim(sqlStr);
\r
41 if (sqlStr.endsWith(";")) {
\r
42 sqlStr = sqlStr.substring(0, sqlStr.length() - 1);
\r
44 System.out.println(sqlStr);
\r
45 sqlExecute(conn, sqlStr);
\r
47 System.out.println("SQL Finished");
\r
49 catch(Exception e) {
\r
50 System.out.println(e);
\r
51 e.printStackTrace();
\r
57 * @param conn データベースコネクション
\r
58 * @param sqlStr 実行するSQL文
\r
59 * @throws SQLException SQL実行エラー
\r
61 public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {
\r
62 try (Statement statement = conn.createStatement();
\r
63 ResultSet rs = statement.executeQuery(sqlStr))
\r
65 ResultSetMetaData rsmd = rs.getMetaData();
\r
66 int count = rsmd.getColumnCount();
\r
68 for (int column = 1; column <= count; column++) {
\r
69 System.out.print("'"+ rs.getString(column) +"'");
\r
70 if (column < count) {
\r
71 System.out.print(",");
\r
74 System.out.print("\n");
\r