将MySQL查询的结果设置为JComboBox

时间:2014-01-29 11:51:40

标签: java mysql eclipse swing

我使用Window Builder(Swing)在Eclipse中设计应用程序。 我有一个mysql查询,选择一个代码,我需要将查询的结果放在一个组合框中,我不知道如何做到这一点。 对不起,如果我没有解释好,我的英语不流畅。

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

2 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

JComboBox cmb = your-combo;
ResultSet rs = your-Result-set; 
while(rs.next()) {
    String result = rs.getString(1); // Retrieves the value of the designated column in the current row of this ResultSet object as a String
    if (result != null) {
        result = result.trim();
    }
    cmb.addItem(result);
} 
rs.close();

答案 1 :(得分:0)

您可以使用loadcombo()来加载具有数据库值的组合框。

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

如果您手动编写列名,那么您需要从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));
          }