将MySQL查询的结果设置到JComboBox中 [英] Set result of MySQL query into a JComboBox

查看:69
本文介绍了将MySQL查询的结果设置到JComboBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Window Builder(Swing)在Eclipse中设计一个应用程序.我有一个选择代码的mysql查询,我需要将查询结果放入组合框,我不知道该怎么做.抱歉,如果我的讲得不好,我的英语说得不好.

I'm designing an application in Eclipse using Window Builder (Swing). I have a mysql query that select a code and I need to put the result of the query in a combobox and I don't know how to make that. Sorry if I'm not explaning well, my English isn't flow.

comboBox.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            Connection conexion=null;
            try{
                Class.forName("com.mysql.jdbc.Driver");
                conexion=DriverManager.getConnection("jdbc:mysql://localhost/gestion_alumnos","root","");
                Statement selcod=conexion.createStatement();
                ResultSet rs=selcod.executeQuery("select clave from alumnos");
                while(rs.next()){
                    comboBox.addItem(rs.getString(1));
                }
                ResultSet rs2=selcod.executeQuery("select nombre, apellidos, edad, calle, numero, codigopostal from alumnos where clave="+comboBox.getSelectedItem());
                txtNombre1.setText(rs2.getString(2));
                txtApellidos1.setText(rs2.getString(3));
                txtEdad1.setText(rs2.getString(4));
                txtCalle1.setText(rs2.getString(5));
                txtNumero1.setText(rs2.getString(6));
                txtCP1.setText(rs2.getString(7));

            } catch(SQLException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } catch(ClassNotFoundException ex){
                JOptionPane.showMessageDialog(null, ex.getMessage());
            } finally{
                try{
                    if(conexion!=null){
                        conexion.close();
                    }
                } catch(SQLException exc){
                    JOptionPane.showMessageDialog(null, exc.getMessage());
                }
            }
        }
    });

推荐答案

您可以使用loadcombo()来向组合框加载数据库值.

You can use a loadcombo() to load your combobox with database values.

void loadcombo() {
    try
    {
    // Your database connections 

rs= st.executeQuery("select Column from Table");
    while(rs.next()){                            
        jComboBox.addItem(rs.getString(1));
    }
    con.close();
    }
    catch(Exception e)
    {
        System.out.println("Error"+e);
    }    
}

ResultSet rs2=selcod.executeQuery("select nombre, apellidos, edad, calle, numero, codigopostal from alumnos where clave="+comboBox.getSelectedItem());
            txtNombre1.setText(rs2.getString(2));
            txtApellidos1.setText(rs2.getString(3));
            txtEdad1.setText(rs2.getString(4));
            txtCalle1.setText(rs2.getString(5));
            txtNumero1.setText(rs2.getString(6));
            txtCP1.setText(rs2.getString(7));

在这种情况下,如果您手动编写列名,则需要以1开头,例如 rs.getString(1)

In this if you are manually writing column names then you need to start with the 1 like rs.getString(1)

只需将其替换为

ResultSet rs2=selcod.executeQuery("select nombre, apellidos, edad, calle, numero, codigopostal from alumnos where clave="+comboBox.getSelectedItem());
          if(rs2.next()){
            txtNombre1.setText(rs2.getString(1));
            txtApellidos1.setText(rs2.getString(2));
            txtEdad1.setText(rs2.getString(3));
            txtCalle1.setText(rs2.getString(4));
            txtNumero1.setText(rs2.getString(5));
            txtCP1.setText(rs2.getString(6));
          }

这篇关于将MySQL查询的结果设置到JComboBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆