I solved the problem, thank you. If it is useful for anyone, here it is:
public class Ventana extends JFrame {
private JComboBox jComboBox1 = new JComboBox();
public Ventana() {
try {
jbInit();
DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e) {
System.out.println("clase no encontrada");
System.err.println (e) ;
System.exit (-1) ;
}
try {
// open connection to database
Connection connection = DriverManager.getConnection(
"jdbc
racle:thin:@localhost:1521:XE",
"", // ## fill in User here
"" // ## fill in Password here
);
// build query
String query = "SELECT * From USUARIO";
Statement st = connection.createStatement();
// execute query
ResultSet rs = st.executeQuery (query);
while ( rs.next () ) {
String nombreCompleto = rs.getString("USU_NOMBRE");
modeloCombo.addElement(nombreCompleto);
}
rs.close();
jComboBox1.setModel(modeloCombo);
//connection.close () ;
}
catch (java.sql.SQLException e) {
System.err.println (e) ;
System.exit (-1) ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize( new Dimension(400, 300) );
jComboBox1.setBounds(new Rectangle(155, 80, 130, 55));
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBox1_actionPerformed(e);
}
});
this.getContentPane().add(jComboBox1, null);
}
I had to use the ComboBoxModel component.
--- Update ---
I solved the problem, thank you. If it is useful for anyone, here it is:
public class Ventana extends JFrame {
private JComboBox jComboBox1 = new JComboBox();
public Ventana() {
try {
jbInit();
DefaultComboBoxModel modeloCombo = new DefaultComboBoxModel();
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch (ClassNotFoundException e) {
System.out.println("clase no encontrada");
System.err.println (e) ;
System.exit (-1) ;
}
try {
// open connection to database
Connection connection = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE",
"", // ## fill in User here
"" // ## fill in Password here
);
// build query
String query = "SELECT * From USUARIO";
Statement st = connection.createStatement();
// execute query
ResultSet rs = st.executeQuery (query);
while ( rs.next () ) {
String nombreCompleto = rs.getString("USU_NOMBRE");
modeloCombo.addElement(nombreCompleto);
}
rs.close();
jComboBox1.setModel(modeloCombo);
//connection.close () ;
}
catch (java.sql.SQLException e) {
System.err.println (e) ;
System.exit (-1) ;
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.getContentPane().setLayout( null );
this.setSize( new Dimension(400, 300) );
jComboBox1.setBounds(new Rectangle(155, 80, 130, 55));
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jComboBox1_actionPerformed(e);
}
});
this.getContentPane().add(jComboBox1, null);
}
I had to use the ComboBoxModel component.