如何将所有值从数据库ID获取到jcombobox?

时间:2015-11-28 11:57:01

标签: java mysql database user-interface netbeans

我想从数据ID中获取所有值。

我已完成以下操作,只获取ID数据的第一个值。请帮助我们了解如何从中获取所有价值。

String sql = "select * from customer";
Connection cn = null;
PreparedStatement ps = null;
Statement st = null;
ResultSet rs = null;  

public custmanagement() {
    initComponents();        
    setTitle("Customer Management");
    setResizable(false);      

    fillcombo();
}

private void fillcombo() {
    try {            
        cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dms_db", "root", "");
        ps=cn.prepareStatement(sql);  

        rs=ps.executeQuery();
        while (rs.next()) {  
            String id = rs.getString("id");
            cboxid.addItem(id);
        }

    } catch (Exception e) {
           JOptionPane.showMessageDialog(null, e);
    }        
}

我已经知道我的问题。这是因为我想在jcombobox中选择id时检索所有数据......这是我的编码......

private void cboxidActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:
    String id = cboxid.getSelectedItem().toString();
    try {
        Class.forName("com.mysql.jdbc.Driver");
        cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dms_db", "root", ""); 
        ps=cn.prepareStatement("select * from customer where id=?");
        ps.setString(1, id);
        rs=ps.executeQuery();
        while (rs.next()) {
            lblname.setText(rs.getString("name"));
            lblic.setText(rs.getString("ic"));
            lbladdress.setText(rs.getString("address"));
            lblgender.setText(rs.getString("gender"));
            lblpackage.setText(rs.getString("package"));
            lblphone.setText(rs.getString("phone"));               


        }


    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, e);
    }
}

有人可以帮我解决这段代码的错误......

0 个答案:

没有答案