Reputation: 158
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
Upvotes: 0
Views: 40
Reputation: 117
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.
Upvotes: 0
Reputation: 3942
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
Upvotes: 1