【问题标题】:JComboBox Help to delete data from databaseJComboBox 帮助从数据库中删除数据
【发布时间】:2011-05-16 11:59:46
【问题描述】:

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

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

现在如何从数据库中删除 JComboBox 中选定的员工?

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

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

【问题讨论】:

    标签: java database jcombobox


    【解决方案1】:
    • 创建一个包含两个字段的 Employee 对象。
    • 重写 toString() 方法以输出员工姓名。
    • 将 Employee 对象放入 JComboBox 中会显示名称,但您可以将所选对象强制转换为 Employee 并取出 id。

    【讨论】:

    • public String toString(){ return name;}
    • 只是一个简单的两个变量 Employee 对象。
    【解决方案2】:

    你可以试试这个:

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

    JComboBox 的方法getSelectedItem() 返回一个可以转换为放置在组合框中的任何对象的对象。要获取用于渲染项目的组件,请调用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;
        }
    
    }
    

    现在您想在组合框中添加项目的位置使用combo.addItem(empObject);。它将在组合框中显示员工的姓名,当您执行 getSelectedItem() 时,它将返回员工对象,您将获得名称和 ID 都属于该 emp 对象。

    【讨论】:

    • @Andrew:我试图用一个例子来解释这件事。对吗?
    【解决方案3】:

    我不想出示身份证。

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

    屏幕截图

    你要解雇哪个John Smith

    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;
        }
    }
    

    例如输入输出

    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>
    

    【讨论】:

    • 就没有“解雇”你而言,这是“提前”吗? ;)
    • 两者兼得!并确定答案! ;)
    【解决方案4】:

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

        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);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多