Search code examples
c#mysqlpdfitext

Writing a list from a database to a PDF


I'm trying to write all the fields from a table of a database I created into a PDF. This is my code:

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();

My PDF file looks something like this:

apm.History@6dec77
apm.History@2669323d
apm.History@33eea10d
apm.History@5e850105

My code is not writing the actual fields from the database, instead it's just referencing them. I'm not sure if I'm using the correct terminology but instead of outputting:

ID32321 User Admin 2017/10/12 14:24

it outputs:

apm.History@6dec77

to the PDF. How do I fix this?


Solution

  • Add a toString method to your History class to output something else than the object reference.