Ich versuche, eine JComboBox aus Daten in SqlServer zu füllen. Ich verwende WindowBuilder, wenn das einen Unterschied macht. Dies ist der Code, den ich bisher habe. Ich bin sehr neu in Java, also habe ich keine Ahnung, was ich tun soll.So füllen Sie JComboBox von SqlServer
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTable;
public class Customer extends JFrame{
private JFrame frame;
private JTable table;
private JTable tblInformation;
private JTable tblHistory;
/**
* Launch the application.
*/
public static void NewScreen()
{
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Customer window = new Customer();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Customer() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 819, 656);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JComboBox comboBox = new JComboBox();
comboBox.setBounds(372, 28, 152, 22);
frame.getContentPane().add(comboBox);
JLabel lblCustomer = new JLabel("Select customer");
lblCustomer.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCustomer.setBounds(245, 30, 121, 16);
frame.getContentPane().add(lblCustomer);
JLabel lblCustomerInformation = new JLabel("Information");
lblCustomerInformation.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCustomerInformation.setBounds(12, 74, 92, 16);
frame.getContentPane().add(lblCustomerInformation);
JLabel lblHistory = new JLabel("History");
lblHistory.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblHistory.setBounds(12, 334, 56, 16);
frame.getContentPane().add(lblHistory);
JButton btnEdit = new JButton("Edit");
btnEdit.setBounds(12, 569, 97, 25);
frame.getContentPane().add(btnEdit);
JButton btnExit = new JButton("Exit");
btnExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Welcome.main(null);
}
});
btnExit.setBounds(690, 569, 97, 25);
frame.getContentPane().add(btnExit);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0)
{
String custIDString, firstName, lastName, companyName, phoneNumber, street, city, state, zip;
int custID;
custIDString = JOptionPane.showInputDialog(null,"Enter customer ID", "Customer ID", JOptionPane.INFORMATION_MESSAGE);
custID = Integer.parseInt(custIDString);
firstName = JOptionPane.showInputDialog(null,"Enter first name", "First Name", JOptionPane.INFORMATION_MESSAGE);
lastName = JOptionPane.showInputDialog(null,"Enter last name", "Last Name", JOptionPane.INFORMATION_MESSAGE);
companyName = JOptionPane.showInputDialog(null,"Enter company name", "Company Name", JOptionPane.INFORMATION_MESSAGE);
phoneNumber = JOptionPane.showInputDialog(null,"Enter phone number (xxx-xxx-xxx)", "Phone Number", JOptionPane.INFORMATION_MESSAGE);
street = JOptionPane.showInputDialog(null,"Enter street", "Street", JOptionPane.INFORMATION_MESSAGE);
city = JOptionPane.showInputDialog(null,"Enter city", "City", JOptionPane.INFORMATION_MESSAGE);
state = JOptionPane.showInputDialog(null,"Enter state", "State", JOptionPane.INFORMATION_MESSAGE);
zip = JOptionPane.showInputDialog(null,"Enter zip", "Zip", JOptionPane.INFORMATION_MESSAGE);
}
});
btnAdd.setBounds(235, 569, 97, 25);
frame.getContentPane().add(btnAdd);
table = new JTable();
table.setBounds(257, 168, 137, -66);
frame.getContentPane().add(table);
tblInformation = new JTable();
tblInformation.setBounds(12, 96, 422, 156);
frame.getContentPane().add(tblInformation);
tblHistory = new JTable();
tblHistory.setBounds(12, 357, 422, 177);
frame.getContentPane().add(tblHistory);
JButton btnDelete = new JButton("Delete");
btnDelete.setBounds(474, 569, 97, 25);
frame.getContentPane().add(btnDelete);
}
public class TestComboBox extends JComboBox
{
private Connection sqlCon;
private Statement st;
public TestComboBox()
{
super();
initComponents();
}
private void initComponents()
{
try
{
st = sqlCon.createStatement();
loadComboBox();
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
}
public void loadComboBox()
{
this.removeAllItems();
this.addItem("Please select");
try
{
ResultSet rs = st.executeQuery("select FirstName, LastName from Customer");
while (rs.next())
{
this.addItem(rs.getString("FirstName") + "" + rs.getString("LastName"));
}
}
catch (SQLException sqle)
{
System.out.println(sqle);
}
}
}
}
Machen Sie kleine Schritte. Laden Sie zuerst das Kombinationsfeld mit fest codierten Werten. Holen Sie dann Sachen aus einer Datenbank und legen Sie sie auf der Konsole ab. Dann lege die beiden zusammen. Sie werden mehr auf diese Weise lernen. –