未在组合框中禁用数据-使用Netbeans的Sql和Java

未在组合框中禁用数据-使用Netbeans的Sql和Java,java,mysql,sql,netbeans,combobox,Java,Mysql,Sql,Netbeans,Combobox,我有一个名为user的表,我想从中在组合框中显示用户类型。我已经为连接创建了一个单独的类 package testdb; import java.sql.*; import javax.swing.*; public class MySqlConnect { Connection conn = null; public static Connection ConnectDB(){ try{ Class.forName("com.mysql.jdbc.Driver");

我有一个名为user的表,我想从中在组合框中显示用户类型。我已经为连接创建了一个单独的类

package testdb;
import java.sql.*;
import javax.swing.*;


public class MySqlConnect {
Connection conn = null;
public static Connection ConnectDB(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
        JOptionPane.showMessageDialog(null, "Connected to database");
        return conn;

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

}

}
我在其上创建组合框的表单具有以下代码

package testdb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JOptionPane;


public class Combo extends javax.swing.JFrame {
    Connection conn = null;
    PreparedStatement pst = null;
    ResultSet rs = null;


public Combo() {
    initComponents();
}

private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    combo1 = new javax.swing.JComboBox<>();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jLabel1.setText("User Type");

    combo1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    combo1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            combo1ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(41, 41, 41)
            .addComponent(jLabel1)
            .addGap(73, 73, 73)
            .addComponent(combo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(181, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(43, 43, 43)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(combo1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(237, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

private void combo1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
    // TODO add your handling code here:

    try{
        conn = MySqlConnect.ConnectDB();
        String s = "Select * from user";
        pst = conn.prepareStatement(s);
        rs = pst.executeQuery();

        while(rs.next()){
            String cb = rs.getString("username");
            combo1.addItem(cb);
        }
    }
    catch (Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}                                      

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(Combo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(Combo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(Combo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(Combo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new Combo().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JComboBox<String> combo1;
private javax.swing.JLabel jLabel1;
// End of variables declaration                   
}

组合框仍然不显示数据库中的任何值。有人能告诉我我的代码出了什么问题吗?

我一直在努力,终于找到了!我使用了一个名为fillCombo的函数,然后调用了它!成功了。这就是我喜欢编码的地方,当你最终解决它的时候,你只需要不断地尝试一个新的方法,感觉非常棒

现在我必须解决我遇到的下一个问题,删除重复值。我要继续努力