vendredi 26 décembre 2014

How do I get the information from SQLite database and store it in a word docx or pdf

Hi guys I really need some help, this has been giving me a headache for quite some time now. I want to try and get all my data from my table in the database and store it in a word document or pdf document.


I tried running a loop through all the data i order to get the stored data but only the values of the first row are printed and I keep getting an error when trying to add the info to the file.


The thing i would like to do is to get the data, set a specific part of the data as italics (in this case the title) and print the rest normally


Please Guys i really need help...


here is a sample of my code:


PDF:-



import java.io.FileOutputStream;
//import java.io.IOException;

//import com.itextpdf.text.BaseColor;
import java.io.*;
import java.sql.*;
//import javax.swing.JOptionPane;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CodesfrTesting {
final Connection conn=DBConnection.ConnectDB();;
ResultSet rs=null;
PreparedStatement pst=null;
String author;
String last;
String title;
String year;
String journalnm;
String pgs;
String volume;
String issue;


public void produce(){
try {
Document doc= new Document();
try {
PdfWriter.getInstance(doc, new FileOutputStream("dov.pdf"));
try{
String sql="Select * from Saved_Journal";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
doc.open();
while(rs.next()){
author=rs.getString("AuthorName");
title=rs.getString("Title");
year=rs.getString("Year");
journalnm=rs.getString("JournalName");
pgs=rs.getString("Pages");
volume=rs.getString("Volume");
issue=rs.getString("Issue");
last=rs.getString("Author_FName");

System.out.println();

Font f=new Font(Font.FontFamily.TIMES_ROMAN,12,Font.ITALIC);


Chunk c1 = new Chunk(author);
Chunk c3 = new Chunk(title);
Chunk c4 = new Chunk(year);
Chunk c5 = new Chunk(journalnm,f);
Chunk c6 = new Chunk(pgs);
Chunk c7 = new Chunk(volume);
Chunk c8 = new Chunk(issue);
Chunk c2 = new Chunk(last);


Phrase p=new Phrase();
p.add(c1);
p.add(c2);
p.add(c3);
p.add(c5);
p.add(c7);
p.add(c8);
p.add(c6);
p.add(c4);

Paragraph p2 = new Paragraph();
p2.add(p);
doc.add(p2);
doc.add(Chunk.NEWLINE);

}
doc.close();

}catch(Exception e){
Logger.getLogger(CodesfrTesting.class.getName()).log(Level.SEVERE, null, e);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(CodesfrTesting.class.getName()).log(Level.SEVERE, null, ex);
}


} catch (DocumentException ex) {
Logger.getLogger(CodesfrTesting.class.getName()).log(Level.SEVERE, null, ex);
}
}

public static void main(String[] args) {
CodesfrTesting a=new CodesfrTesting();
a.produce();
}
}


Word:-



import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;
import org.apache.poi.xwpf.usermodel.*;


public class cather {
final Connection conn=DBConnection.ConnectDB();;
ResultSet rs=null;
PreparedStatement pst=null;
String author;
String last;
String title;
String year;
String journalnm;
String pgs;
String volume;
String issue;
public void str(){
try{

FileOutputStream file= new FileOutputStream("Sample1.docx");
XWPFDocument doc= new XWPFDocument();
XWPFParagraph paragraph= doc.createParagraph();

paragraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun run =paragraph.createRun();
XWPFRun run2=paragraph.createRun();
run.setFontSize(11);
run2.setFontSize(11);
run.setFontFamily("Times New Roman");
run2.setFontFamily("Times New Roman");
run2.setItalic(true);


String sql="Select * from Saved_Journal";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
while(rs.next()){
author=rs.getString("AuthorName");
last=rs.getString("Author_FName");
title=rs.getString("Title");
year=rs.getString("Year");
journalnm=rs.getString("JournalName");
pgs=rs.getString("Pages");
volume=rs.getString("Volume");
issue=rs.getString("Issue");


run.setText(author);
run.setText(last);
run2.setText(title);
run.setText(year);
run.setText(journalnm);
run.setText(pgs);
run.setText(volume);
run.setText(issue);
run.addBreak();
run.addBreak();
doc.write(file);
}
file.close();
JOptionPane.showMessageDialog(null, "All Set!!");
}
catch(Exception e){
JOptionPane.showMessageDialog(null,e);
}
}
public static void main(String[] args) {
cather c=new cather();
c.str();
}

}


Thanks guys....


Aucun commentaire:

Enregistrer un commentaire