i want to write the data to file, and data must be copy from java frame java frame contains 7-8 labels n below that 1 table.
i want store all this data to 1 file. plz help me
import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.JScrollPane; import java.awt.Container; import java.awt.Dimension; import java.awt.Insets; import java.awt.*; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import java.sql.*;
public class CopyFrameDataToFile implements ActionListener { String name, course, subject; int rollNo, marks; JFrame frame; JLabel lab1, lab2, lab3, lab4, lab5; JTextField text1, text2, text3, text4, text5; JButton button;
public void createUI() { frame = new JFrame("Copy Frame Data To File Example"); frame.setLayout(null); frame.setSize(400,300); lab1 = new JLabel("Enter Name : "); lab2 = new JLabel("Enter Roll No : "); lab3 = new JLabel("Enter Course : "); lab4 = new JLabel("Enter Subject : "); lab5 = new JLabel("Enter Marks : "); Container pane =frame.getContentPane(); pane.setLayout(null); Insets insets = pane.getInsets(); Dimension size; JPanel p = new JPanel(); text1=new JTextField(10); text1.setText(""); text1.setEditable(true); p.add(lab1); p.add(text1); pane.add(p); size = p.getPreferredSize(); p.setBounds(100 + insets.left, 50 + insets.top, size.width, size.height); p = new JPanel(); text2=new JTextField(10); text2.setText(""); text2.setEditable(true); p.add(lab2); p.add(text2); pane.add(p); size = p.getPreferredSize(); p.setBounds(100 + insets.left, 80 + insets.top, size.width, size.height); p = new JPanel(); text3=new JTextField(10); text3.setText(""); text3.setEditable(true); p.add(lab3); p.add(text3); pane.add(p); size = p.getPreferredSize(); p.setBounds(100 + insets.left, 110 + insets.top, size.width, size.height); p = new JPanel(); text4=new JTextField(10); text4.setText(""); text4.setEditable(true); p.add(lab4); p.add(text4); pane.add(p); size = p.getPreferredSize(); p.setBounds(100 + insets.left, 140 + insets.top, size.width, size.height); p = new JPanel(); text5=new JTextField(10); text5.setText(""); text5.setEditable(true); p.add(lab5); p.add(text5); pane.add(p); size = p.getPreferredSize(); p.setBounds(100 + insets.left, 170 + insets.top, size.width, size.height); p = new JPanel(); pane.add(p); button = new JButton("SUBMIT"); button.addActionListener(this); p.add(button); size = p.getPreferredSize(); p.setBounds(200 + insets.left, 200 + insets.top, size.width, size.height); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }// createUI() closed
continue.....
public void actionPerformed(ActionEvent e) { button = (JButton)e.getSource(); name = text1.getText(); rollNo = Integer.parseInt(text2.getText()); course = text3.getText(); subject = text4.getText(); marks = Integer.parseInt(text5.getText()); submitValue(); System.out.println("Value Submitted Successfully"); } public void submitValue() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:swing"); String sql = "INSERT INTO employee(name, rollNo, course, subject, marks) Values('"+name+"','"+ rollNo+"','"+course+"','"+subject+"','"+marks+"')"; Statement st = con.createStatement(); st.execute(sql); JOptionPane.showMessageDialog(null, "Record Added Succesfully.","Record Added", JOptionPane.INFORMATION_MESSAGE); } catch(Exception e) { JOptionPane.showMessageDialog(null, e.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } } public static void main(String args[]) { CopyFrameDataToFile cfdf = new CopyFrameDataToFile(); cfdf.createUI(); }// main() closed }// class closed
Ads