Dobry den, potreboval bych pomoc s maturitnim projektem. Už jsem skoro u konce a jedina vec co me jeste nejde je prave vytvoreni PDF kde bude vygenerovanych vice stran, prave v jednom PDF. Dale pak PDF se spravnymi vysledky.
package project;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Header;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.io.*;
import java.awt.Color;
import java.io.FileOutputStream;
import java.lang.String;
import java.sql.*;
import javax.swing.*;
import java.sql.Statement;
import java.sql.DriverManager;
import java.util.ArrayList;
public class OdpovediPDF{
private Connection conn = null;
ResultSet rs1 = null;
ResultSet rs2 = null;
ResultSet rs3 = null;
PreparedStatement pst1 = null;
PreparedStatement pst2 = null;
PreparedStatement pst3 = null;
String sql = null;
private Statement statement = null;
int pocet;
String predmet;
Paragraph preface = new Paragraph();
Paragraph preface1 = new Paragraph();
Document document = new Document(PageSize.A4, 14, 8, 7, 7);
public void nactiOtazky(int lehke,int stredni,int tezke, int topic, int user) {
if(/* Test.vlastniCh.isChecked()*/ true){
conn = PripojeniMySQL.ConnectDb();
String sqlA = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 1) AND (topic_idtopic = "+topic+")) order by Razeni Limit " + lehke;
String sqlB = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 2) AND (topic_idtopic = "+topic+")) order by Razeni Limit " + stredni;
String sqlC = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 3) AND (topic_idtopic = "+topic+")) order by Razeni Limit " + tezke;
try{
pst1 = conn.prepareStatement(sqlA);
pst2 = conn.prepareStatement(sqlB);
pst3 = conn.prepareStatement(sqlC);
rs1 = pst1.executeQuery();
rs2 = pst2.executeQuery();
rs3 = pst3.executeQuery();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
else {
conn = PripojeniMySQL.ConnectDb();
String sqlA = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 1) AND (topic_idtopic = "+topic+")) AND ( userid = " +user+ ") order by Razeni Limit " + lehke;
String sqlB = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 2) AND (topic_idtopic = "+topic+")) AND ( userid = " +user+ ") order by Razeni Limit " + stredni;
String sqlC = "select idquestion, question, aA, aB, aC, aD, correct, level_idlevel, topic_idtopic, userid, RAND() as 'Razeni' from test WHERE ((level_idlevel = 3) AND (topic_idtopic = "+topic+")) AND ( userid = " +user+ ") order by Razeni Limit " + tezke;
try{
pst1 = conn.prepareStatement(sqlA);
pst2 = conn.prepareStatement(sqlB);
pst3 = conn.prepareStatement(sqlC);
rs1 = pst1.executeQuery();
rs2 = pst2.executeQuery();
rs3 = pst3.executeQuery();
}
catch (Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
}
public void generujHlavicku(String predmet) throws Exception{
this.predmet = predmet;
preface.add(new Paragraph("Name:______________________ Date:_____________ Points:_______ Mark:_______ "));
preface.add(new Paragraph(" "));
preface.add(new Paragraph(" "+ predmet +" - Test" ));
preface.add(new Paragraph(" "));
document.add(preface);
}
public void generujOtazky() throws Exception{
while(rs1.next()){
preface.add(new Paragraph(" " +rs1.getString(2)));
preface.add(new Paragraph(" a) "+ rs1.getString(3)));
preface.add(new Paragraph(" b) "+ rs1.getString(4)));
preface.add(new Paragraph(" c) "+ rs1.getString(5)));
preface.add(new Paragraph(" d) "+ rs1.getString(6)));
preface.add(new Paragraph(" ...../"+ rs1.getString(8)));
}
while(rs2.next()){
preface.add(new Paragraph(" " +rs2.getString(2)));
preface.add(new Paragraph(" a) "+ rs2.getString(3)));
preface.add(new Paragraph(" b) "+ rs2.getString(4)));
preface.add(new Paragraph(" c) "+ rs2.getString(5)));
preface.add(new Paragraph(" d) "+ rs2.getString(6)));
preface.add(new Paragraph(" ...../"+ rs2.getString(8)));
}
while(rs3.next()){
preface.add(new Paragraph(" " +rs3.getString(2)));
preface.add(new Paragraph(" a) "+ rs3.getString(3)));
preface.add(new Paragraph(" b) "+ rs3.getString(4)));
preface.add(new Paragraph(" c) "+ rs3.getString(5)));
preface.add(new Paragraph(" d) "+ rs3.getString(6)));
preface.add(new Paragraph(" ...../"+ rs3.getString(8)));
}
document.add(preface);
}
public void generujOdpovedi(String predmet, int vratSoucet) throws Exception{
preface1.add(new Paragraph("Name:______________________ Date:_____________ Points:_______ Mark:_______ "));
preface1.add(new Paragraph(" "));
preface1.add(new Paragraph(" "+ predmet+" - Test"));
preface1.add(new Paragraph(" "));
if(pocet <= vratSoucet){
while(rs1.next()){
preface1.add(new Paragraph(" " + pocet + ", " + rs1.getString(7)));
pocet = pocet + 1 ;
}
while(rs2.next()){
preface1.add(new Paragraph(" " + pocet + ", " + rs2.getString(7)));
pocet = pocet + 1 ;
}
while(rs3.next()){
preface1.add(new Paragraph(" " + pocet + ", " + rs3.getString(7)));
pocet = pocet + 1 ;
}
document.add(preface);
}
}
public int forCyklus(int pocetTest) throws Exception {
int forCyklus = 0;
PdfWriter.getInstance(document,new FileOutputStream("Mathematic.pdf"));
document.open();
for(int i = 1; i <= pocetTest; i++ ){
generujHlavicku(predmet);
generujOtazky();
}
document.add(preface);
return pocetTest;
}
public int forCyklus3(int pocetTest) throws Exception {
int forCyklus = 0;
PdfWriter.getInstance(document,new FileOutputStream("Odpovedi.pdf"));
document.open();
for(int i = 1; i <= pocetTest; i++ ){
generujOdpovedi(predmet, pocet);
}
document.add(preface1);
OtevreniPDF pdfMath = new OtevreniPDF();
pdfMath.generujPdf();
return pocetTest;
}
}
PDF se mi spusti ale nenactou se data. Otviram to pres Google, ale vždy to zůstane zaseknute na nacitani s sedou obrazovkou.
Rad bch použil diakritiku ale mam rozbitou klavesnici.