我有一个 JComboBox 自动设置, 其方法可以在启动时从数据库中抓取全部 Id s, 我还有一个删除功能, 这将从数据库中删除选中的对象, 但是它不会从列表中删除 ID 。 执行删除方法时, 我是否可以更新 JComboBox? 如果有帮助, 我的代码会低于此值 。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;
public class EditMember extends JFrame {
String[] positions = {"", "Trainee", "Writer", "Moderator", "Administrator"};
JComboBox _id = new JComboBox(getId());
JComboBox _position = new JComboBox(positions);
JTextField _name = new JTextField(10);
JTextField _username = new JTextField(10);
JPasswordField _password = new JPasswordField(10);
JButton edit = new JButton("Edit Member");
JButton delete = new JButton("Delete Member");
JButton generate = new JButton("Generate Report");
JButton exit = new JButton("Exit");
JButton load = new JButton("Load in");
Toolkit kit = Toolkit.getDefaultToolkit();
Dimension screenSize = kit.getScreenSize();
int screenHeight = (int)screenSize.getHeight();
int screenWidth = (int)screenSize.getWidth();
public EditMember() {
super("Edit Member");
setLayout(new GridLayout(7,1,1,1));
add(_id);
add(load);
load.addActionListener(new LoadInListener());
add(new JLabel("Name:"));
add(_name);
add(new JLabel("Username:"));
add(_username);
add(new JLabel("Password:"));
add(_password);
add(new JLabel("Position:"));
add(_position);
add(edit);
add(delete);
delete.addActionListener(new DeleteListener());
add(generate);
add(exit);
exit.addActionListener(new ExitListener());
setSize(406, 200);
setResizable(false);
setLocation(screenWidth/4, screenHeight/4);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
new EditMember();
}
public Object[] getId() {
Connection con;
Statement stmt;
ResultSet rs;
//Object[] returnId;
ArrayList<Object> returnId = new ArrayList<Object>();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");
stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT `id` FROM main");
while(rs.next()) {
returnId.add(rs.getObject("id"));
}
con.close();
} catch(Exception e) {
e.printStackTrace();
}
return returnId.toArray(new Object[returnId.size()]);
}
public class ExitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
dispose();
}
}
public class LoadInListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String giveId = _id.getSelectedItem().toString();
populate(giveId);
}
}
public class DeleteListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
delete();
}
}
public void delete() {
Connection con;
Statement stmt;
ResultSet rs;
int idA;
String name, username, password, position;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
PreparedStatement pstmt = con.prepareStatement("DELETE FROM `main` WHERE ID = ?");
pstmt.setInt(1, (int)_id.getSelectedItem());
pstmt.execute();
// udpate JComboBox here
} catch(Exception e) {
e.printStackTrace();
}
}
public void populate(String a) {
Connection con;
Statement stmt;
ResultSet rs;
int idA;
String name, username, password, position;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:collegesys","root","0blivi0n");
stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery("SELECT * FROM main");
while(rs.next()) {
idA = rs.getInt("id");
name = rs.getString("name");
username = rs.getString("username");
password = rs.getString("password");
position = rs.getString("position");
if(Integer.parseInt(a) == idA) {
_name.setText(name);
_username.setText(username);
_password.setText(password);
_position.setSelectedItem(position);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}