Hi, This is the real code, database name is kics , table name is dateframe and fields are date, style, description, fabricgsm, agegroup, size, measurementsunits..
Here i want to validate componentTextField, TFstyle, TFdescription, TFfabricGSM, TFagegroup, TFsize, TFmeasurements; if these fields are null or left blank and click the save button, i must get the message box as "XYZ(date,style....) field should be entered".
public class dateFrame extends JFrame { JTextField componentTextField = new JTextField(); JTextField TFstyle; JTextField TFdescription; JTextField TFfabricGSM; JTextField TFagegroup; JTextField TFsize; JTextField TFmeasurements; public static void main(String[] args) { dateFrame df = new dateFrame(); } public dateFrame() { super("Date Frame"); dateFramePanel dfp = new dateFramePanel(); save s = new save(); getContentPane().add(dfp, BorderLayout.PAGE_START); getContentPane().add(s, BorderLayout.SOUTH); pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocation(350, 250); setResizable(false); setVisible(true); } class dateFramePanel extends JPanel { DateChooser dc = new DateChooser(); public dateFramePanel() { super(new GridLayout(0, 2)); JLabel Ldate = new JLabel("Date"); JLabel Lstyle = new JLabel("Style"); JLabel Ldescription = new JLabel("Description"); JLabel LfabricGSM = new JLabel("Fabric & GSM"); JLabel Lagegroup = new JLabel("Age group"); JLabel Lsize = new JLabel("Size"); JLabel Lmeasurements = new JLabel("Measurement units (CM / INCH)"); TFstyle = new JTextField(10); TFdescription = new JTextField(10); TFfabricGSM = new JTextField(10); TFagegroup = new JTextField(10); TFsize = new JTextField(10); TFmeasurements = new JTextField(10); add(Ldate); add(dc); add(Lstyle); add(TFstyle); add(Ldescription); add(TFdescription); add(LfabricGSM); add(TFfabricGSM); add(Lagegroup); add(TFagegroup); add(Lsize); add(TFsize); add(Lmeasurements); add(TFmeasurements); setPreferredSize(new Dimension(700, 200)); } } class save extends JPanel { public save() { JButton save = new JButton("Save"); add(save); save.addActionListener(new JDBC()); } }
class JDBC implements ActionListener { public void actionPerformed(ActionEvent e) { Connection con = null; Statement stmt; String loadDriver = "sun.jdbc.odbc.JdbcOdbcDriver"; try { Class.forName(loadDriver); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/kics", "root", ""); stmt = con.createStatement(); String exe = "insert into kics.dateframe(date,style,description,fabricgsm,agegroup,size,measurementsunits)values('" + componentTextField.getText() + "', '"+ TFstyle.getText() + "','" + TFdescription.getText() + "','" + TFfabricGSM.getText() + "','" + TFagegroup.getText() + "','"+ TFsize.getText() + "', '"+ TFmeasurements.getText() + "')"; stmt.executeUpdate(exe); } catch (Exception e1) { System.out.println("Exception found"); System.err.println(e1.getMessage()); } finally { try { con.close(); } catch (Exception e1) { System.out.println("Exception found"); System.err.print(e1.getMessage()); } } } }