2016-12-19 2 views
2

저는 java에서 새로 생겼으며 itextpdf를 출력으로 사용합니다.
지금 당장이 문제로 인해 혼란 스럽습니다.

결과 집합을 데이터베이스에서 itextpdf로

내 문제는 데이터베이스에서 결과 집합을 PDF 형식으로 표시하고 싶다는 것입니다.

예를 들어,

이름

제인
메리
소니


전, 3 열이
을 테이블을 설정하고이 데이터베이스에서 결과 집합입니다

이제 itextpdf의 출력은 다음과 같이 표시되어야합니다. 0 | 1234 |
| _____ 존 ______ | _____ 제인 _____ | _____ 메리 ____ |
| _____ 소니 _____ | _____ 키엘 _____ | _____________ |


결과가 모든 열에 삽입되기를 원하며이를 수행하는 방법을 모릅니다.

누구? 누군가가 나를 인도 할 수 있으면 좋을 것입니다.

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); 
} 
+0

Java에서 제공하는 PreparedStatement 및 ResultSet 클래스를 사용해 본 적이 있습니까? – Aaron

+0

네, 이미 알고 있습니다. 내가 알지 못하는 부분은 각 결과 집합이 각 열에 삽입된다는 것을 어떻게 표시 할 수 있는지입니다. –

답변

0

왜, 행의 필드의 수를 얻을 반복하고 탭 또는 파이프가 각 값을 분리하는 ResultSetMetaData에 옵션을 사용하지 마십시오. 행의 마지막에 개행 문자를 추가하십시오. hasnext of resultset는 동일한 작업을 수행하는 모든 레코드를 통해 실행됩니다. 전체 목록을 목록에 저장하십시오. 이 목록을 사용하여 PDF로 작성하십시오.

OpenCsv | SQL | Writes only the header and not the table content

1

이 잘못된 것입니다 : 모든

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

먼저, 재사용 할 수있는 Font 객체 생성하여 더러운 코딩을 정리하자,하자

Font font = FontFactory.getFont(FontFactory.TIMES_ROMAN,12,BaseColor.BLACK); 

둘째 각 행에 동일한 값을 추가하는 문제를 수정하십시오.

마지막으로
String v1; 
while(rs.next()){ 
    v1 = rs.getString("hehe"); 
    table2.addCell(new Paragraph("TOTAL Number: " + v1, font)); 
} 

따라서 우리는 마지막 행을 완료해야합니다, 우리는 iText를 만 테이블에 완벽한 행을 추가하는 것을 알고, 당신의 예에서, 소니와 킬은 세 가지의 연속으로 두 개의 셀을 채우기 :

table2.completeRow(); 

이제 table2을 문서에 추가 할 수 있습니다.

다른 어떤 발언 :

이 과잉이다 :

PdfPTable table2 = new PdfPTable(new float[] { 3,3,3}); 

각이 동일한 폭이 3 열이있는 열을 원하는 경우에, 그것은 동일한 값으로 float[]를 전달하는 데 필요는 없습니다 이 줄은 이해가되지 않습니다

PdfPTable table2 = new PdfPTable(3); 

을 : : 당신이 단지 수

을 당신은 폭, 따라서 527의 폭이 무시됩니다을 고정하지 않는

  1. : 때문에
    table2.setTotalWidth(527); 
    

    이 이해가되지 않습니다. 추가 할 수 있습니다. table.setLockedWidth(true);

  2. table2.setWidthPercentage(100);도 사용 가능한 너비의 100 %로 정의합니다.

절대 또는 너비 중 하나를 선택해야합니다. 둘 다 의미가 없습니다. 이 완전히 잘못된 입니다 How to define the width of a cell?

읽어 보시기 바랍니다 : 당신은 3 열있는 테이블을 정의하는

cell.setColspan(8); 

, 8 열가있는 것처럼 따라서 당신이 열 병합을 설정합니다.

당신은 초보자이고 iText를 7 방금 시작하고 사용하지 때문에 왜이 iText 5를 사용하고 3 열이있는 테이블에서 최대 열 병합 마지막 3

입니까? iText 7은 iText 5를 다시 작성한 것입니다.이 API는 다른 미래 보장형 API를 가지고 있습니다. 테이블은 here으로 설명되어 있습니다.

+0

그래, 정말 고마워. 정말 미안 해요. 지금 itext7 공부하고 있습니다. –

관련 문제