1 package jp.co.areaweb.tools.database;
\r
6 import jp.co.areaweb.tools.core.JapaneseString;
\r
10 * SQL文が記述されたファイルを読み込んで実行する。
\r
12 * ・SELECTコマンドは実行できない。
\r
16 /** コマンドラインラインからdoSQLを実行する。
\r
17 * java DoSQL [SQLファイル名]
\r
18 * exp) java dbtool.DoSQL sqlfile.sql
\r
21 static public void main(String[] args) {
\r
22 if (args.length < 2) {
\r
23 System.out.println("exp: java jp.co.areaweb.tools.database.Select [propertieFile] [sqlfile]");
\r
26 Select.doSQL(args[0], args[1]);
\r
31 * sqlfileの中のSQL文を1行づつ実行する。
\r
33 * ・一つのSQL文は1行で記述すること。(途中改行はだめ)
\r
34 * @param propertieFile プロパティファイル
\r
35 * @param sqlfile SQL文が記述されたファイル。
\r
37 public static void doSQL(String propertieFile, String sqlfile) {
\r
38 Connection conn = null;
\r
40 conn = DatabaseTool.openDb(propertieFile);
\r
42 BufferedReader ds = new BufferedReader(new InputStreamReader(new FileInputStream(sqlfile)));
\r
44 while ((sqlStr = ds.readLine()) != null) {
\r
45 sqlStr = JapaneseString.trim(sqlStr);
\r
46 if (sqlStr.endsWith(";")) {
\r
47 sqlStr = sqlStr.substring(0, sqlStr.length() - 1);
\r
49 System.out.println(sqlStr);
\r
50 sqlExecute(conn, sqlStr);
\r
54 System.out.println("SQL Finished");
\r
56 catch(Exception e) {
\r
57 // Print out the error message
\r
58 System.out.println(e);
\r
59 e.printStackTrace();
\r
63 DatabaseTool.closeDb(conn);
\r
70 * @param conn データベースコネクション
\r
71 * @param sqlStr 実行するSQL文
\r
72 * @throws SQLException SQL実行エラー
\r
74 public static void sqlExecute(Connection conn, String sqlStr) throws SQLException {
\r
76 Statement statement = conn.createStatement();
\r
77 ResultSet rs = statement.executeQuery(sqlStr);
\r
78 ResultSetMetaData rsmd = rs.getMetaData();
\r
79 int count = rsmd.getColumnCount();
\r
81 for (int column = 1; column <= count; column++) {
\r
82 System.out.print("'"+ rs.getString(column) +"'");
\r
83 if (column < count) {
\r
84 System.out.print(",");
\r
87 System.out.print("\n");
\r