我是新来的java和我使用itextpdf作为我的输出。
现在我很害怕这个问题。
从数据库到itextpdf的结果集
我的问题是,我想显示PDF格式的数据库结果集与扭曲。
例如,我将设置一个表3列,
,这些都是从数据库中的结果,
名称
约翰
简
玛丽
桑尼
基尔
所以现在itextpdf中的输出应该像这样被查看
| __ columnname _ | __ columnname_ | _columnname__ |
| _____ John ______ | _____ Jane _____ | _____ Mary ____ |
| _____ Sonny _____ | _____ Kiel _____ | _____________ |
我想结果被插入每一列,我不知道如何做到这一点。
有人吗?如果有人能指导我,这将是很好的。
Document document = new Document(PageSize.A4, 25, 25, 25, 25);
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\PURCHASEORDER\\"+see+".pdf"));
document.open();
try {
Class.forName(driver);
conn = DriverManager.getConnection(url+db, user, pass);
Statement st = conn.createStatement();
String zero = dates.getSelectedItem().toString();
String sql = "select name as hehe from names where servedate = '"+zero+"'";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
Rectangle react = writer.getPageSize();
PdfPTable table2 = new PdfPTable(new float[] { 3,3,3});
table2.setTotalWidth(527);
table2.getDefaultCell().setBorder(Rectangle.NO_BORDER);
PdfPCell cell = new PdfPCell(new Paragraph(""));
cell.setColspan(8);
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setBackgroundColor(BaseColor.GRAY);
table2.addCell(cell);
table2.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
while(rs.next()){
String v1 = rs.getString("hehe");
FontFactory.getFont(FontFactory.TIMES_BOLD,14,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
table2.addCell(new Paragraph("TOTAL Number: "+v1+"", FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK)));
}
table2.setWidthPercentage(100);
document.add(table2);
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
document.close();
String pdfFile="D:\\PURCHASEORDER\\"+see+".pdf";
File f = new File(pdfFile);
if (pdfFile.toString().endsWith(".pdf")) {
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + pdfFile);
} else {
//For cross platform use
Desktop desktop = Desktop.getDesktop();
desktop.open(f);
}
你看过使用java提供的PreparedStatement和ResultSet类吗? – Aaron
是的,我已经知道他们,我不知道的唯一部分是,我如何显示他们,每个结果集将被插入每列。 –