2017-10-12 46 views
1

我正在試圖將我創建的數據庫的表中的所有字段寫入PDF。這是我的代碼:將數據庫中的列表寫入PDF

Document snapshot = new Document(); 
ArrayList<History> historyList = new ArrayList<History>(); 
Connection con = dbConnection(); 
String query = "SELECT * FROM apm_system.history"; 
Statement st; 
ResultSet rs; 
try { 
    st = con.createStatement(); 
    rs = st.executeQuery(query); 
    History history; 

    while (rs.next()) { 
     history = new History(rs.getString("TransactionID"), 
     rs.getString("On"), rs.getString("By"), rs.getString("Date"), 
     rs.getString("Time")); 
     historyList.add(history); 
     } 

    PdfWriter.getInstance(snapshot, new FileOutputStream("snapshot.pdf")); 
    snapshot.open(); 

    for (int i = 0; i < historyList.size(); i++) { 
     String temp = historyList.get(i).toString(); 
     Paragraph p = new Paragraph(temp + "\n"); 
     snapshot.add(p); 
     } 
} } catch (Exception e) { 
System.out.println(e); 
     } 
     snapshot.close(); 

我的PDF文件看起來是這樣的:

[email protected] 
[email protected] 
[email protected] 
[email protected] 

我的代碼是不是寫從數據庫中實際的領域,相反,它只是引用它們。我不確定是否使用了正確的術語,而不是輸出:ID32321 User Admin 2017/10/12 14:24。它輸出:[email protected]到PDF。 我該如何解決這個問題?提前致謝。

+0

我的代碼不是從數據庫中寫入實際的字段,而是僅僅引用它們。我不確定是否使用了正確的術語,而不是輸出:ID32321 User Admin 2017/10/12 14:24。它輸出:[email protected]到PDF – HelloWorld

+0

我不同意近距離投票(不清楚你在問什麼)。問題非常清楚,包括代碼示例,包括預期和實際結果。答案如此微不足道,即使不是像我這樣的開發人員也能夠回答這個問題。 –

回答

2

toString方法添加到您的History類中以輸出除對象引用之外的內容。

+0

這工作。非常感謝:) – HelloWorld

+0

如果你能接受答案,會很好。 –