Java Generate PDF file from database records

Java Generate PDF using Hibernate to read records from database

import java.util.*;
public class Student
{
	private int studentId;
	private String studentName;
	private String studentSex;
	private String studentCourseName;
	private String studentPhno;
	private int studentMarks;
	private String studentAddress;
	public Student( int studentId,String studentName, String studentSex, String studentCourseName, String studentPhno,int studentMarks, String studentAddress) {
		this.studentId=studentId;
		this.studentName=studentName;
		this.studentSex= studentSex;
		this.studentCourseName=studentCourseName;
		this.studentPhno=studentPhno;
		this.studentMarks=studentMarks;
		this.studentAddress=studentAddress;
	}
	
	
	
	@Override
	public String toString() {
		return "" + studentId + "\t\t" + studentName + "\t\t" + studentSex
				+ "\t\t" + studentCourseName + "\t\t" + studentPhno + "\t\t"
				+ studentMarks + "\t\t\t" + studentAddress ;
	}



	public int getStudentId() {
		return studentId;
	}


	public void setStudentId(int studentId) {
		this.studentId = studentId;
	}


	public String getStudentName() {
		return studentName;
	}


	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}


	public String getStudentSex() {
		return studentSex;
	}


	public void setStudentSex(String studentSex) {
		this.studentSex = studentSex;
	}


	public String getStudentCourseName() {
		return studentCourseName;
	}


	public void setStudentCourseName(String studentCourseName) {
		this.studentCourseName = studentCourseName;
	}


	public String getStudentPhno() {
		return studentPhno;
	}


	public void setStudentPhno(String studentPhno) {
		this.studentPhno = studentPhno;
	}


	public int getStudentMarks() {
		return studentMarks;
	}


	public void setStudentMarks(int studentMarks) {
		this.studentMarks = studentMarks;
	}


	public String getStudentAddress() {
		return studentAddress;
	}


	public void setStudentAddress(String studentAddress) {
		this.studentAddress = studentAddress;
	}


	public void display(){
		System.out.print(studentId+"\t\t");
		System.out.print(studentName+"\t\t");
		System.out.print(studentSex+"\t");
		System.out.print(studentCourseName+"\t\t");
		System.out.print(studentPhno+"\t\t");
		System.out.print(studentMarks+"\t\t\t");
		System.out.print(studentAddress+"\t");
		System.out.println();
		
	}
}

Hibernate.cfg.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd”>
<hibernate-configuration>
<session-factory>
<property name=”show_sql”>false</property>
<property name=”hbm2ddl.auto”>create</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/student1?zeroDateTimeBehavior=convertToNull</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.connection.password”></property>

<mapping class=”com.santanu.arrays.Student”/>

</session-factory>
</hibernate-configuration>

public class HibernateTest {

	public static void main(String[] args) {
		Student [] student= new Student[10];
		
		student[0]= new Student(1, "Vipin", "M", "Java", "9000000001", 85, "Lucknow");
		student[1]= new Student(2, "Sohel", "M", "C++", "9000000002", 81, "Kolkata");
		student[2]= new Student(3, "Meheraj", "M", "C/C++", "9000000003", 86, "Bengalore");
		student[3]= new Student(4, "Shruti", "F", "J2ee", "9000000004", 55, "Kolkata");
		student[4]= new Student(5, "Shabbu", "M", ".Net", "9000000005", 86, "Bengaluru");
		student[5]= new Student(6, "Sharukh", "M", "Python", "9000000006", 74, "Delhi");
		student[6]= new Student(7, "Deepika", "F", "PHP", "9000000007", 67, "Chandigarh");
		student[7]= new Student(8, "Anuskha", "F", "HTML", "9000000008", 67, "Patna");
		student[8]= new Student(9, "Kejri","M", "Ruby", "9000000009", 71, "Vizag");
		student[9]= new Student(10, "Rahul", "M", "ASP", "9000000010", 51, "Bairely");
		
	    SessionFactory sf = new Configuration().configure().buildSessionFactory();
	    Session sc = sf.openSession();
	    sc.beginTransaction();
	    	  
	    System.out.println("hi");
	    for (Student x:student) {
			sc.save(x);
			}
	    System.out.println("hi");
	    sc.getTransaction().commit();
	    sc.close();
	    
		}

}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.*;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PDFCreator {

	public static void main(String[] args) throws ClassNotFoundException, SQLException, FileNotFoundException, DocumentException,  IllegalAccessException {
	int rowno=0;
	
	Class.forName("com.mysql.jdbc.Driver");//.newInstance();
	Connection  con= DriverManager.getConnection("jdbc:mysql://localhost:3306/student1?zeroDateTimeBehavior=convertToNull", "root","") ;
	Statement st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	ResultSet rs = st.executeQuery("SELECT * FROM student");
	ResultSetMetaData rsmd = rs.getMetaData();
	int colno = rsmd.getColumnCount();
	while (rs.next()) {
		 rowno= rowno+1;
		
	}
	rs.first();
	System.out.println(""+rs.getString(1));
	
	Document d = new Document();
	PdfWriter.getInstance(d, new FileOutputStream("report.pdf"));
	PdfPTable pt = new PdfPTable(colno);
	d.open();
	d.add(new Paragraph("Student Record"));
	for (int i = 0; i < rowno; i++) {
		pt.addCell(""+rs.getString(1));
		pt.addCell(""+rs.getString(2));
		pt.addCell(""+rs.getString(3));
		pt.addCell(""+rs.getString(4));
		pt.addCell(""+rs.getString(5));
		pt.addCell(""+rs.getString(6));
		pt.addCell(""+rs.getString(7));
		rs.next();
	}
	
	d.add(pt);
	
	d.close();
	}

}