Print

  1 import java.awt.Color;
  2 import java.awt.Font;
  3 import java.awt.Graphics;
  4 import java.awt.print.PageFormat;
  5 import java.awt.print.Printable;
  6 import java.awt.print.PrinterException;
  7 import java.awt.print.PrinterJob;
  8 import java.sql.Connection;
  9 import java.sql.DriverManager;
 10 import java.sql.ResultSet;
 11 import java.sql.Statement;
 12 import java.text.SimpleDateFormat;
 13 import java.util.Date;
 14
 15 ExpandedBlockStart.gifContractedBlock.gif /**/ /**
 16 * 印刷
 17 */

 18 ExpandedBlockStart.gifContractedBlock.gif public   class  Print implements Printable  {
 19
 20    static ResultSet rs = null;
 21    int positionCount = 0;
 22    int employeeCount = 0;
 23    int lineCount = 0;
 24    String oldPosition = null;
 25    String lastNo = null;
 26    String lastName = null;
 27    String lastPosition = null;
 28    boolean flag = false;
 29
 30ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args) {
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 32            Class.forName("org.postgresql.Driver");
 33            Connection con = DriverManager.getConnection(
 34                    "jdbc:postgresql:hellodb""user""pass");
 35            Statement stmt = con.createStatement();
 36            String sql = "SELECT * FROM TABLE ORDER BY POSITION, EMPLOYEE_NO";
 37            rs = stmt.executeQuery(sql);
 38
 39            PrinterJob job = PrinterJob.getPrinterJob();
 40            job.setPrintable(new Print());
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            if (job.printDialog()) {
 42                job.print();
 43            }

 44            rs.close();
 45            stmt.close();
 46            con.close();
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
 48            e.printStackTrace();
 49        }

 50    }

 51
 52    public int print(Graphics g, PageFormat pf, int pageIndex)
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            throws PrinterException {
 54ExpandedSubBlockStart.gifContractedSubBlock.gif        try {
 55            lineCount = 0;
 56            g.setFont(new Font("Arial", Font.PLAIN, 12));
 57            g.setColor(Color.BLACK);
 58            Date nowDate = new Date();
 59            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
 60            g.drawString("***EMPLOYEE INFO*** " + sdf.format(nowDate), 5050);
 61            g.drawString("EMPLOYEE_NO   EMPLOYEE_NAME   POSITION"50100);
 62
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            if (flag) {
 64                g.drawString(lastNo + " " + lastName + " " + lastPosition, 50,
 65                        200);
 66                lineCount++;
 67                flag = false;
 68            }

 69ExpandedSubBlockStart.gifContractedSubBlock.gif            while (rs.next()) {
 70                String no = rs.getString("EMPLOYEE_NO");
 71ExpandedSubBlockStart.gifContractedSubBlock.gif                if (oldPosition == null{
 72                    oldPosition = no;
 73                }

 74                String name = rs.getString("EMPLOYEE_NAME");
 75                String position = rs.getString("POSITION");
 76
 77ExpandedSubBlockStart.gifContractedSubBlock.gif                if (oldPosition.equals(no)) {
 78                    g.drawString(no + " " + name + " " + position, 50,
 79                            200 + lineCount * 50);
 80                    positionCount++;
 81                    lineCount++;
 82ExpandedSubBlockStart.gifContractedSubBlock.gif                }
 else {
 83                    g.drawString("position_total:" + employeeCount, 50,
 84                            200 + lineCount * 50);
 85                    lastNo = no;
 86                    lastName = name;
 87                    lastPosition = position;
 88                    positionCount = 0;
 89                    flag = true;
 90                    return Printable.PAGE_EXISTS;
 91                }

 92
 93ExpandedSubBlockStart.gifContractedSubBlock.gif                if (30 >= lineCount) {
 94                    lastNo = no;
 95                    lastName = name;
 96                    lastPosition = position;
 97                    flag = true;
 98                    return Printable.PAGE_EXISTS;
 99                }

100                employeeCount++;
101            }

102ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) {
103            e.printStackTrace();
104        }

105        g
106                .drawString("company_total:" + employeeCount, 50,
107                        200 + lineCount * 50);
108
109        return Printable.NO_SUCH_PAGE;
110    }

111}

112

转载于:https://www.cnblogs.com/cserft/archive/2008/12/17/1357036.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值