栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

获取选定的JComboBox值并添加到SQL查询中以获取第二个JComboBox

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

获取选定的JComboBox值并添加到SQL查询中以获取第二个JComboBox

我假设您想从第一个JComboBox中选择一个值,然后单击一个按钮来处理所选数据并将新数据加载到第二个JComboBox中。

在这种情况下,您需要JButton而不是JComboBox的ActionListener:

jButton1.addActionListener(new ActionListener() {    @Override    public void actionPerformed(ActionEvent e) {        selectedName = (String) jComboBox1.getSelectedItem();         }});

您还需要将所选值存储在变量中。该

getSelectedItem()
方法返回一个Object,因此在您的情况下需要将其强制转换为String。

由于我们向按钮添加了ActionListener,因此您不需要此按钮:

jComboBox1.addActionListener(new java.awt.event.ActionListener() {    public void actionPerformed(java.awt.event.ActionEvent evt) {        jComboBox1ActionPerformed(evt);    }});

在您的 createConnection类中 (通过命名约定,类名应以大写字母开头):

如果不使用try-with-resources语句,则应在catch块之后关闭连接。

    } finally {        if (conn != null) { try {     conn.close(); } catch (SQLException ex) {     ex.printStackTrace(); }        }    }

您需要将selectedName变量传递给

getEmpLocation()
方法:

public List<String> getEmpLocation(String name) {

您应该使用PreparedStatement而不是Statement:

String query = "SELECT first_name FROM employees WHERe last_name = ?";PreparedStatement ps = conn.prepareStatement(query);ps.setString(1, name); ResultSet results = ps.executeQuery();

老实说,我不知道您要通过选择查询实现什么。首先,此选择查询将不起作用。表名是LOCATIONS而不是位置,并且它没有称为emp_name的列。

"SELECt country_id FROM hr.location WHERe hr.location.emp_name = ?"

如果您想获取位置,则应使用如下查询:

"SELECt dep.department_name, loc.city, cou.country_name FROM employees emp, departments dep, locations loc, countries cou WHERe emp.last_name = ? AND emp.department_id = dep.department_id  AND dep.location_id = loc.location_id AND loc.country_id = cou.country_id"

您可以选择要使用部门,城市或国家/地区名称的位置。但是我的主要问题是,如果您首先选择姓氏并将其放在JComboBox中,则很可能只会获得一行数据,因此使用第二个JComboBox毫无意义。让我们从另一侧解决这个问题。如果先选择位置然后再选择员工该怎么办。那可以解决这个问题。

快速示例: 您从数据库中选择所有名字,然后可以选择适当的名字。

从数据库中选择所有名字:

    public List<String> getEmpFirstName() {        List<String> list = new ArrayList();        Connection conn = createConnection();        try { Statement stmt = conn.createStatement(); String query = "SELECt DISTINCT first_name "      + "FROM hr.employees "      + "ORDER BY first_name"; ResultSet results = stmt.executeQuery(query); while (results.next()) {     list.add(results.getString("first_name")); }        } catch (Exception e) { System.out.println("Exception = " + e);        } finally { if (conn != null) {     try {         conn.close();     } catch (SQLException ex) {         ex.printStackTrace();     }          }        }        return list;    }

使用PreparedStatement根据名字选择姓氏:

    public List<String> getEmpLastName(String name) {        List<String> list = new ArrayList();        Connection conn = createConnection();        try { String query = "SELECt last_name "      + "FROM employees "      + "WHERe first_name = ?"; PreparedStatement ps = conn.prepareStatement(query); ps.setString(1, name);  ResultSet results = ps.executeQuery(); while (results.next()) {     list.add(results.getString("last_name")); }        } catch (Exception e) { System.out.println("Exception = " + e);        } finally { if (conn != null) {     try {         conn.close();     } catch (SQLException ex) {         ex.printStackTrace();     }          }        }        return list;    }

更新您的ActionListener:

    jButton1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {     // Store selected value     selectedName = (String) jComboBox1.getSelectedItem();     // Create Connection and pass selected value to getEmpLastName     createConnection c1 = new createConnection();     names = c1.getEmpLastName(selectedName);     // Clear your second comboBox and fill with data     jComboBox2.removeAllItems();          for (String lastName : names) {         jComboBox2.addItem(lastName);     }    }        });

尝试选择常用名称,例如Alexander,David,James,John,Julia等。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418655.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号