実装初心者です。至らない点ございましたら申し訳ございません。
よろしくお願いよろしくお願いします。
※諸事情により、私の実装環境でまだテストができていない状況での質問となります。。。
実現したいこと
言語 :java
DB :Postgresql
バッチの自動処理の中で、javaからPostgreSQLに接続して、テーブル情報をTSV出力したいと考えています。
TSV出力はPostgreSQLコマンドである"COPY"コマンドを利用して出力したいと思っています。
PostgreSQLに接続後、ステートメントとしてPostgreSQLコマンドを含んだ文字列を作成して実行した場合、コマンドは処理可能でしょうか?
現在、下記のようなコードを作成しています。
SQLではないため、コマンドは実行してくれないような気がしています。。。
java
1public void exportTsv() throws SQLException, ClassNotFoundException, IOException { 2 3 Connection conn = null; 4 Statement stmt = null; 5 6 // プロパティファイル読み込み 7 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties"); 8 InputStreamReader reader = new InputStreamReader(inputStream, "utf8"); 9 Properties properties = new Properties(); 10 properties.load(reader); 11 12 // 接続情報取得 13 String jdbcUrl = properties.getProperty("jdbc_url"); 14 String DbUser = properties.getProperty("db_user"); 15 String DbPassword = properties.getProperty("db_password"); 16 17 // PostgreSQLへ接続 18 Class.forName("org.postgresql.Driver"); 19 conn = DriverManager.getConnection(jdbcUrl, DbUser, DbPassword); 20 21 // PostgreSQLコマンド呼び出し実行 22 stmt = conn.createStatement(); 23 String command = this.getCommand(); 24 25 stmt.executeQuery(command); 26 27} 28 29/** 30 * Postgresqlコマンドの作成 31 * 32 * @return 33 * @throws IOException 34 */ 35public String getCommand() throws IOException { 36 37 // プロパティファイル読み込み 38 InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("application.properties"); 39 InputStreamReader reader = new InputStreamReader(inputStream, "utf8"); 40 Properties properties = new Properties(); 41 properties.load(reader); 42 43 // PostgreSQLコマンド要素取得 44 String exportTable = properties.getProperty("export_tsv_table"); 45 String exportPath = properties.getProperty("export_tsv_path"); 46 47 // PostgreSQLコマンド作成 48 return MessageFormat.format(exportTable, exportPath); 49} 50
properties
1command=COPY テーブル名 TO {0}/ファイル名.tsv WITH CSV NULL AS 'null' DELIMITER E'\t' ; 2export_path=パス
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/17 08:33