Problem in printing report using java

Can get only one page of report but the report should show the custome

Page 1 of 1

0 Replies - 1093 Views - Last Post: 11 November 2006 - 06:59 PM Rate Topic: -----

#1 pingkorn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-November 06

Problem in printing report using java

Post icon  Posted 11 November 2006 - 06:59 PM

My program can get only one page of report but the report should show the customer inf about 5-6 person per page and it should be printed for more than one page. So please help me because i try to fix it for long time ago with many technics. I still not find the solution to solve this problem. Please look at my code if i make something wrong so Could you inform me? Thank you.


public class OtheCustomerReport extends JPanel implements Printable {
	Book	bk;
	PageFormat   pf;
	Paper	pp;
	String	BusinessName, TitleReport, FontFormat;
 	
	Connection	conn;
	Statement	stat;
	ResultSet	rs;
	String		url="jdbc:odbc:ConnectDB";

	int	   width=612,height=792;
	
-public OtheCustomerReport(){
	BusinessName="ELEKTRON CORP., LTD.";
	TitleReport="CUSTOMER REPORT";
	FontFormat="Courier New";
	
	pf=new PageFormat();
	pp=new Paper();
	pp.setSize(width,height);
				pp.setImageableArea(10,20,pp.getWidth()-10,pp.getHeight()-20);
	pf.setPaper(pp);
	}
	
-public void showPrinter(){
	PrinterJob pr=PrinterJob.getPrinterJob();
	bk=new Book();
	bk.append(this,pf,3);
	pr.setPageable(bk);
		
	if(pr.printDialog()){
	try{
		pr.print();
	}catch(Exception e){
				System.out.println(e); }
				   }
						 }
	
-public void connectData() {
	try{
	  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	  conn=DriverManager.getConnection(url);
	  stat=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
	}catch(SQLException e) {
	  JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
	}catch(ClassNotFoundException e) {
	  JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
	}
}

-public int print(Graphics g,PageFormat pf,int pageIndex) throws PrinterException{
	if(pageIndex >= 1)
		 {
	return Printable.NO_SUCH_PAGE;
		 }else
	try{
		   Graphics2D g2 = (Graphics2D)g;
		   g2.translate(pf.getImageableX(),pf.getImageableY());
		   connectData();
		   String sql="SELECT * FROM Customer ORDER BY customer_id";
			
		   rs=stat.executeQuery(sql);
		   rs.last();
		   if(rs.getRow()>0)
		   {
	int r=100;
	rs.first();
					
	g2.setFont(new Font(FontFormat,1,22));
	g2.drawString(BusinessName,20,20);
	g2.setFont(new Font(FontFormat,0,16));
	g2.drawLine(20,25,width-20,25);
	g2.drawLine(20,27,width-20,27);
			
	g2.setFont(new Font(FontFormat,0,25));
	g2.drawString(TitleReport,40,55);
	g2.drawLine(20,70,width-20,70);
	g2.drawLine(20,72,width-20,72);
		
	do
	{
	g2.setFont(new Font(FontFormat,1,16));
	g2.drawString("CUSTOMER ID",40,r);
	g2.setFont(new Font(FontFormat,0,16));
	g2.drawString(rs.getString("customer_id"),200,r);
	r+=20;
	g2.setFont(new Font(FontFormat,1,16));
	g2.drawString("NAME",40,r);
	g2.setFont(new Font(FontFormat,0,16));
		   g2.drawString(rs.getString("customer_name"),200,r);
	r+=20;
	g2.setFont(new Font(FontFormat,1,16));
	g2.drawString("PHONE",40,r);
	g2.setFont(new Font(FontFormat,0,16));
		  g2.drawString(rs.getString("customer_phone"),200,r);
	r+=20;
	g2.setFont(new Font(FontFormat,1,16));
	g2.drawString("ADDRESS",40,r);
	g2.setFont(new Font(FontFormat,0,16));
		g2.drawString(rs.getString("customer_address"),200,r);
	r+=20;
	g2.drawLine(20,180,width-20,180);
	r+=40;
	}while(rs.next());
	}
	rs.close();
	stat.close();
	conn.close();
				}catch(Exception e)
	{
	JOptionPane.showMessageDialog(null, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
	}
	return Printable.PAGE_EXISTS;
	}
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1