[英]Data not being disaplyed in combo box - Sql and Java using Netbeans
I have a table called "user" from which I want to display the user types in a combo box. 我有一个名为“用户”的表,我想从中显示组合框中的用户类型。 I have created a separate class for the connection.
我为连接创建了一个单独的类。
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;
}
}
}
The form on which I've created the combo box has the following code. 我在其上创建了组合框的窗体具有以下代码。
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
}
The combo box still doesn't display any values from the database. 组合框仍然不显示数据库中的任何值。 Could someone please tell me what's wrong in my code?
有人可以告诉我我的代码有什么问题吗?
I kept trying and finally got it! 我不断尝试,终于明白了! I used a function called fillCombo an then called it!
我使用了一个名为fillCombo的函数,然后调用了它! It worked.
有效。 This is what I love about coding, you just got to keep trying an when you finally solve it, it feels just amazing.
这就是我对编码的热爱,当您最终解决它时,您只需继续尝试一下,感觉真是太神奇了。
Now I have to solve the next problem I encountered, removing duplicate values. 现在,我必须解决遇到的下一个问题,删除重复的值。 I'm going to keep trying!
我将继续尝试!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.