
Hi I have created a swing application which enter some data and fetch some data from DB . Now I want a print feature by which i want to take a print out of that data in form of table. What should I do to achieve this.
Regards,
am2085

Hi Friend,
Try the following code:
import java.io.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class JTableDatabase extends JFrame{
ResultSet rs;
JTableDatabase(){
final Vector columnNames = new Vector();
final Vector data = new Vector();
JPanel panel=new JPanel();
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st = con.createStatement();
ResultSet rs= st.executeQuery("Select * from data");
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 );
}
}
catch(Exception e){}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
add(panel);
}
public static void main(String arg[])
{
try
{
JTableDatabase frame=new JTableDatabase();
frame.setSize(450,200);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}
For more information, visit the following link:
http://www.roseindia.net/java/example/java/swing/Print.shtml
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.