postgresデータベースからテーブルのリストを取得してxlsファイルに書き込むために、次のプログラムを作成しました。私はxlsファイルを書くためにApachepoiライブラリを含めました。プログラムはエラーなしで実行されており、も作成されますが、出力はファイルに書き込まれません。ファイルは空です。plzは、結果セットをファイルに書き込むのに役立ちます。
package list;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class List
{
public static void main(String[] args) throws SQLException, FileNotFoundException, IOException
{
Connection con = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/db","user","pass");
DatabaseMetaData md = con.getMetaData();
ResultSet rs = md.getTables(null, "public", "%", null);
try (FileOutputStream fileOut = new FileOutputStream("/home/usr/Desktop/list.xls"))
{
Workbook wb = new HSSFWorkbook();
Sheet sheet1 = wb.createSheet("Table List");
Row row = sheet1.createRow(250);
while (rs.next())
{
row.createCell(0).setCellValue(rs.getString(3));
}
wb.write(fileOut);
fileOut.close();
}
catch(SQLException e)
{
System.out.println( "could not get JDBC connection : " + e );
}
}
}