



import java.awt.print.*; import java.awt.*; import java.sql.*; import java.util.*; import javax.swing.*; import java.awt.event.*; import javax.swing.table.*; import java.io.*; import javax.swing.JFileChooser; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JDialog; import javax.swing.JFileChooser;
class JT implements ActionListener {
JButton b=new JButton("Print");
Vector JButton= new Vector();
JFileChooser fc;
static private final String newline = "\n";
JButton b1=new JButton("Save");
JTextArea log;
JTable table ;
File file;
public JT()
{
//***********Button*****************
//b.addActionListener(printAction);
b.setBounds(10,100,100,50);
b1.setBounds(10,200,100,50);
b1.addActionListener(this);
/* ActionListener printAction = new ActionListener() { public void actionPerformed(ActionEvent e) {
try
{
table.print();
}
catch (PrinterException pe)
{
System.err.println("Error printing: " + pe.getMessage());
}
}
};*/ //************
Vector columnNames = new Vector(); Vector data = new Vector(); JPanel p=new JPanel();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:supermarket.mdb");
String sql = "Select * from bill data";
Statement stmt = con.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 se) { //System.out.println(e); }
table= new JTable(data, columnNames); table.setVisible(true);
TableColumn col; for (int i = 0; i < table.getColumnCount(); i++) { col = table.getColumnModel().getColumn(i); col.setMaxWidth(250); } JScrollPane scrollPane = new JScrollPane( table ); p.add( scrollPane ); JFrame f=new JFrame(); f.add(p); p.add(b); p.setBackground(Color.pink); p.add(b1); f.setSize(600,400); f.setVisible(true);
fc = new JFileChooser();
}//constructor
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1)
{
int returnVal; int flag; do { flag=0; final JFileChooser fc = new JFileChooser(); file = fc.getSelectedFile(); //here when i put it in do while loop it opens from MY DOCUMENTS which i dont want..it must retain its previous location returnVal = fc.showSaveDialog(null); if (returnVal == JFileChooser.APPROVE_OPTION) { try {
file = fc.getSelectedFile();
String[] files=new File(file.getParent()).list();
for(int i=0;i String filename =file.getName(); } }
//now you can access definately access database just type your own database name and see magic here 'supermarket' is my database name and 'bill' is my table name if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
String fileName = file.getName();
Writer output=null;
output=new BufferedWriter(new FileWriter(file));
//output.write(textArea.getText());
output.close();
}
catch(Exception e2){};
}
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(table);
oos.close();
}
catch(IOException fe)
{
System.out.println("Problem creating table file: " + fe);
return;
}
System.out.println("JTable correctly saved to file " + filename);
}
public static void main(String[] args)
{
JT hh=new JT();
}
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.