JComboBox帮助从数据库中删除数据

时间:2011-05-16 11:59:46

标签: java database jcombobox

我在数据库employee中有一个表,由两列 ID NameLastName 组成。

我到了将第二列中的数据添加到JComboBox中,就像在快照中一样!

enter image description here

现在我该如何从DB中删除JComboBox中的选定员工?

我想添加名称如此I122-Name的ID,并使用split方法提取ID,但我不想显示ID。

有没有办法将JComboBox中的每个名称与包含员工ID的隐藏值相关联?

4 个答案:

答案 0 :(得分:4)

  • 创建一个包含两个字段的Employee对象。
  • 覆盖toString()方法以输出员工姓名。
  • 将Employee对象放在JComboBox中,将显示名称,但您可以将所选对象强制转换为Employee并提取ID。

答案 1 :(得分:3)

你可以试试这个:

创建一个带有字段名称和id的Employee类,然后创建一个实现ListCellRenderer并扩展JLabel的类。将此类作为渲染器添加到JComboBox。现在,您可以将Name设置为JLabel中的文本。 现在,当您访问comboBox的元素时,它将返回JLabel,您可以将名称作为可见值获取,并将id作为您在JLabel中设置的隐藏值。

JComboBox的方法getSelectedItem()返回一个Object,可以将其强制转换为放置在组合框中的任何Object。要获取用于呈现项目的组件,请致电getRenderer()

注意: - 您可以使用其他组件,然后使用JLabel。


演示: -

public class ComboRenderer extends JLabel implements ListCellRenderer{
    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index,
            boolean isSelected, boolean cellHasFocus) {

        if(value != null){
            Employee emp = (Employee) value;
            setText(emp.getName());
            return this;
        }
        return null;
    }

}

现在,您要将项目添加到comboBox,请使用combo.addItem(empObject);。它将在comboBox中显示employee的名称,当你执行getSelectedItem()时,它将返回employee对象,你将获得name和id都属于该emp对象。

答案 2 :(得分:1)

  

我不想出示身份证。

这个问题已经收到了2个好的答案,但是我想补充第3个问题,如果只是为了解决是否要显示ID的问题(这不是问题的一部分,但本来应该是)。

屏幕截图

你要解雇哪个 John Smith

enter image description here

SackEmployee.java

import java.awt.*;
import javax.swing.*;

class SackEmployee {
    public static void main(String[] args) {
        SwingUtilities.invokeLater( new Runnable() {
            public void run() {
                Employee[] workforce = {
                    new Employee("Ali Ben Messaoud", 9823),
                    new Employee("Jane Brewer", 6348),
                    new Employee("John Smith", 1247),
                    new Employee("John Smith", 4385)
                };

                JComboBox employeeCombo = new JComboBox(workforce);

                EmployeeCellRenderer employeeCellRenderer = new EmployeeCellRenderer();
                employeeCombo.setRenderer(employeeCellRenderer);

                int result = JOptionPane.showConfirmDialog(
                    null,
                    employeeCombo,
                    "Fire Employee?",
                    JOptionPane.OK_CANCEL_OPTION);
                // cast selected item back to Employee.
                Employee employee = (Employee)employeeCombo.getSelectedItem();
                System.out.println( "Fire '" + employee + "' now?" );
                System.out.println( "Proceed: " + (result==JOptionPane.OK_OPTION) );
            }
        });
    }
}

class Employee {

    int id;
    String name;

    Employee(String name, int id) {
        this.id = id;
        this.name = name;
    }

    public String getIdString() {
        return "ID-" + id;
    }

    public String toString() {
        return getIdString() + ": " + name;
    }
}

class EmployeeCellRenderer implements ListCellRenderer {

    JLabel label = new JLabel();

    public Component getListCellRendererComponent(
        JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {

        Employee employee = (Employee)value;
        // distinguish between people of same name by adding ID.
        label.setText(employee.name + " (" + employee.getIdString() + ")");

        return label;
    }
}

E.G。 I / O

prompt>java SackEmployee
Fire 'ID-9823: Ali Ben Messaoud' now?
Proceed: false

prompt>java SackEmployee
Fire 'ID-1247: John Smith' now?
Proceed: true

prompt>java SackEmployee
Fire 'ID-4385: John Smith' now?
Proceed: false

prompt>

答案 3 :(得分:1)

这里是删除数据库中整行的简单代码。

    try{
      String str;
      con = DriverManager.getConnection("jdbc:mysql://:3306/database","user","password");
      stat = con.createStatement();
      ResultSet rs = stat.executeQuery("SELECT column_name FROM table_name where column_name = '"+jComboBox1.getSelectedItem().toString()+"';");
      while(rs.next()){
         Str = rs.getString(column_name);
      }
      stat.executeUpdate("DELETE FROM table_name where column_name = +"str"+");
    }
    rs.close();
    stat.close();
    con.close();
    catch(Exception e){
      System.out.println(e);
    }