我想将中的MySQL数据库表连接到Itext报表。我已编码,但报表中只显示数据库表的第一条记录。我想在报告中显示完整的数据库表。
我的代码和生成报告的屏幕截图。。
private void backupOKActionPerformed(java.awt.event.ActionEvent evt) {
int result;
Date nowdate = new Date(System.currentTimeMillis());
Date backday = backup_date.getDate();
if(backday==null){//checking the date is null or not
JOptionPane.showMessageDialog(null, "Please Enter the Date ...");
}else if(!backday.before(nowdate)){//checking given date before todays date or not
JOptionPane.showMessageDialog(null, "Please Enter Date before Todays date...");
}else{
try {
// backup function goes here
//supplier info backup file fromthe mysql database
String sql = "Select * from supplierinfo";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
if(rs.next()){
String v1 = rs.getString("SupplierID");
String v2 = rs.getString("SupplierName");
String v3 = rs.getString("Address");
String v4 = rs.getString("ContactInfo");
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Save Backup");
chooser.setApproveButtonText("Save");
//disables the all filesoptioning here
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
// System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
// System.out.print("getSelectedFile() : "+chooser.getSelectedFile());
// creating the pdf for supplier details using itext
try {
Document pdfsup = new Document();
PdfWriter.getInstance(pdfsup, new FileOutputStream(new File(chooser.getSelectedFile(),"Supplier Details Report.pdf")));
pdfsup.open();
Image imgsup = Image.getInstance("hedder.png");
// pdfsup.add(new Paragraph("Suppliers"));
pdfsup.add(imgsup);
pdfsup.add(new Paragraph("Supplier Details Report",FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE)));
pdfsup.add(new Paragraph(new Date().toString()));
pdfsup.add(new Paragraph("----------------------------------------------------------------------------------------------------------------"));
PdfPTable tablesup= new PdfPTable(4);
PdfPCell cell = new PdfPCell(new Paragraph("Supplier Information"));
cell.setColspan(8);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setBackgroundColor(BaseColor.CYAN);
tablesup.addCell(cell);
tablesup.addCell("Supplier ID");
tablesup.addCell(v1);
tablesup.addCell("Supplier Name");
tablesup.addCell(v2);
tablesup.addCell("Address");
tablesup.addCell(v3);
tablesup.addCell("Contact Info");
tablesup.addCell(v4);
pdfsup.add(tablesup);
pdfsup.close();
JOptionPane.showMessageDialog(null, "Report Saved...");
} catch (DocumentException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (FileNotFoundException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}else{
System.out.println("No Selection");
}
}
} catch (SQLException ex) {
Logger.getLogger(Backup.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
已编辑:注意:在我的数据库表中,第一个供应商的地址和联系人信息为空
最佳答案
必须使用循环而不是if语句:
while(rs.next()){
String v1 = rs.getString("SupplierID");
String v2 = rs.getString("SupplierName");
String v3 = rs.getString("Address");
String v4 = rs.getString("ContactInfo");
....
把所有应该执行一次的代码放到循环之外。