2017-10-12 24 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

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