1import java.awt.Color;
2import java.awt.Font;
3import java.awt.Graphics;
4import java.awt.print.PageFormat;
5import java.awt.print.Printable;
6import java.awt.print.PrinterException;
7import java.awt.print.PrinterJob;
8import java.sql.Connection;
9import java.sql.DriverManager;
10import java.sql.ResultSet;
11import java.sql.Statement;
12import java.text.SimpleDateFormat;
13import java.util.Date;
14
15/**
16 * 印刷
17 */
18public 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
30 public static void main(String[] args) {
31 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());
41 if (job.printDialog()) {
42 job.print();
43 }
44 rs.close();
45 stmt.close();
46 con.close();
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 }
51
52 public int print(Graphics g, PageFormat pf, int pageIndex)
53 throws PrinterException {
54 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), 50, 50);
61 g.drawString("EMPLOYEE_NO EMPLOYEE_NAME POSITION", 50, 100);
62
63 if (flag) {
64 g.drawString(lastNo + " " + lastName + " " + lastPosition, 50,
65 200);
66 lineCount++;
67 flag = false;
68 }
69 while (rs.next()) {
70 String no = rs.getString("EMPLOYEE_NO");
71 if (oldPosition == null) {
72 oldPosition = no;
73 }
74 String name = rs.getString("EMPLOYEE_NAME");
75 String position = rs.getString("POSITION");
76
77 if (oldPosition.equals(no)) {
78 g.drawString(no + " " + name + " " + position, 50,
79 200 + lineCount * 50);
80 positionCount++;
81 lineCount++;
82 } 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
93 if (30 >= lineCount) {
94 lastNo = no;
95 lastName = name;
96 lastPosition = position;
97 flag = true;
98 return Printable.PAGE_EXISTS;
99 }
100 employeeCount++;
101 }
102 } 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
2import java.awt.Font;
3import java.awt.Graphics;
4import java.awt.print.PageFormat;
5import java.awt.print.Printable;
6import java.awt.print.PrinterException;
7import java.awt.print.PrinterJob;
8import java.sql.Connection;
9import java.sql.DriverManager;
10import java.sql.ResultSet;
11import java.sql.Statement;
12import java.text.SimpleDateFormat;
13import java.util.Date;
14
15/**
16 * 印刷
17 */
18public 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
30 public static void main(String[] args) {
31 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());
41 if (job.printDialog()) {
42 job.print();
43 }
44 rs.close();
45 stmt.close();
46 con.close();
47 } catch (Exception e) {
48 e.printStackTrace();
49 }
50 }
51
52 public int print(Graphics g, PageFormat pf, int pageIndex)
53 throws PrinterException {
54 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), 50, 50);
61 g.drawString("EMPLOYEE_NO EMPLOYEE_NAME POSITION", 50, 100);
62
63 if (flag) {
64 g.drawString(lastNo + " " + lastName + " " + lastPosition, 50,
65 200);
66 lineCount++;
67 flag = false;
68 }
69 while (rs.next()) {
70 String no = rs.getString("EMPLOYEE_NO");
71 if (oldPosition == null) {
72 oldPosition = no;
73 }
74 String name = rs.getString("EMPLOYEE_NAME");
75 String position = rs.getString("POSITION");
76
77 if (oldPosition.equals(no)) {
78 g.drawString(no + " " + name + " " + position, 50,
79 200 + lineCount * 50);
80 positionCount++;
81 lineCount++;
82 } 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
93 if (30 >= lineCount) {
94 lastNo = no;
95 lastName = name;
96 lastPosition = position;
97 flag = true;
98 return Printable.PAGE_EXISTS;
99 }
100 employeeCount++;
101 }
102 } 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