0

i'm just making a assignment that connect database to jFrame and i need some help with this. i already try to compile the java and thats ok, but when i press the button nothing happen.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class Assignment extends JFrame implements ActionListener{
JLabel label, l1, l2, l3, l4, l5;
JButton Add,Delete,Update,Display,Exit;
JTextField tf1, tf2, tf3, tf4, tf5;
Connection conn = null;
Statement stmt = null;
/**
 * Create the frame.
 */
Menu() { 
    setVisible(true);
    setSize(500, 500);
    setLayout(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("JDBC");
    label = new JLabel("Database"); //Label
    Add = new JButton("Add Data"); //Button
    Delete = new JButton("Delete Data");
    Update = new JButton("Update Data");
    Display = new JButton("Display Data");
    Exit = new JButton("Exit");
    tf1 = new JTextField(); //textfield
    tf2 = new JTextField(); 
    tf3 = new JTextField();
    tf4 = new JTextField();
    /**
    * Set bounds.
    */
    Add.addActionListener(this);
    Delete.addActionListener(this);
    Update.addActionListener(this);
    Display.addActionListener(this);
    Exit.addActionListener(this);
    /**
    * add frame.
    */
}
public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals("Add")){ //i already clicked on the button but it doesnt works
        Add();
    }
    else if(e.getActionCommand().equals("Delete")){} //i havent code for this
    else if(e.getActionCommand().equals("Update")){}
    else if(e.getActionCommand().equals("Display")){}
    else if(e.getActionCommand().equals("Exit")){
        System.exit(0);
    }
    }
/**
 * Create the second frame.
 */
public void Add() { 
    JFrame frm = new JFrame();
    frm.setVisible(true);
    frm.setSize(500, 500);
    frm.setLayout(null);
    frm.setTitle("JDBC");
    l1 = new JLabel("ID : "); //Label
    l2 = new JLabel("Name : ");
    l3 = new JLabel("Adress : ");
    l4 = new JLabel("Gender : ");
    l5 = new JLabel("IP : ");       
    /**
    * Set bounds.
    */
    /**
    * add frames.
    */
    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn = DriverManager.getConnection("jdbc:mysql://localhost/academic", "root", "abc"); //connecting
        stmt = conn.createStatement();
        String sql;
        sql = "INSERT INTO student VALUES(" +
              "'" + tf1.getText() + "'," + 
              "'" + tf2.getText() + "'," +
              "'" + tf3.getText() + "'," +
              "'" + tf4.getText() + "'," +
              tf5.getText() + ")";
        stmt.executeUpdate(sql);
        stmt.close();
        conn.close();
    }catch(SQLException se){
        se.printStackTrace();
    }catch(Exception e){
        e.printStackTrace();
    }
}
/**
* Launch the application.
*/
public static void main(String args[]) { 
    new Menu();
}
}

any idea guys ? please help me

Kira Katou
  • 158
  • 2
  • 2
  • 16
  • 1
    Aside from anything else, you should really, really **really** fix the SQL Injection attack in your code. Use parameterized SQL with `PreparedStatement`. – Jon Skeet Mar 11 '16 at 19:15
  • And how come you do not use the `@Override` notation for `actionPerformed` method. If you implement the ActionListener interface, you always have to override the method and add the `@override` notation to the method. – Jernej K Mar 11 '16 at 19:18
  • you are missing setActionCommand for your buttons – Raghu K Nair Mar 11 '16 at 19:20
  • Variable names and method names should NOT start with an upper case character. – camickr Mar 11 '16 at 19:24
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Mar 11 '16 at 21:57

2 Answers2

1

You should be setting Action Command for your button

 Add = new JButton("Add Data"); //Button
 Add.setActionCommand("Add");

Same way for other buttons as well

Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45
  • (1+) That is the action command for the button defaults to the text of the button. So you can either change your if statement to check for "Add Data" or you need to set the ActionCommand to "Add". – camickr Mar 11 '16 at 19:22
  • Yes right camickr. by default ActionCommand will be the same as the text – Raghu K Nair Mar 11 '16 at 19:23
  • i already check about it.. but now my problem is i can't compile it anymore cause of Menu() – Kira Katou Mar 12 '16 at 12:12
0

in main method when you say new Menu(), its creating an instance of Java.awt.Menu, if you need to call the menu() method in your code, you need to use Assignment instance and call Menu() method.

shark
  • 117
  • 1
  • 5