我只是做一個連接數據庫到jFrame的任務,我需要一些幫助。我已經嘗試編譯的Java和多數民族權利,但是當我按下按鈕什麼都沒有發生。我執行的操作不起作用,它很容易編譯
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();
}
}
任何想法的傢伙?請幫我
除了其他任何東西,你應該真的,真的**真的**在你的代碼中修復SQL注入攻擊。用'PreparedStatement'使用參數化的SQL。 –
你怎麼不爲'actionPerformed'方法使用'@ Override'表示法。如果你實現了ActionListener接口,你總是必須重寫方法並在方法中添加'@ override'表示法。 –
您缺少按鈕的setActionCommand –