Skip to main content

How to print database report in hard copy

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
mozum
Offline
Joined: 2012-05-01

Can any one help me on how to print database report in hard copy. The code I used only print lain paper when I run the program. Find below the program which is a part of program the do other task.

printbtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){

Vector columnNames = new Vector();
Vector data = new Vector();
String host = "jdbc:odbc:staffsalary";
try {
Connection connect =DriverManager.getConnection(host);
String sql = "Select * from payroll";
Statement stmt = connect.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++) {
columnNames.addElement( md.getColumnName(i) );
}
while (rs.next()) {
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++) {
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception a) {
System.out.println( a );
}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
MessageFormat header = new MessageFormat("Page {0,number,integer}");
MessageFormat footer = new MessageFormat("TID 5013 Assignment #1");
try {
table.print(JTable.PrintMode.FIT_WIDTH, header, footer);
} catch (Exception er) {
JOptionPane.showMessageDialog(null,"Cannot print" + er.getMessage());
}
}
});