JComboBox with database Oracle.
Hi! I have a database in Oracle and a graphical app in Java. In that, I have a JComboBox to show the available names of the users who are in my ddbb now. I was finding a solution and and I failed, because I want to fill the component dinamically, would it be right with a Vector list?
Thank you!
Re: JComboBox with database Oracle.
Can you make a small simple program that uses a JComboBox to show the problem?
The program should compile and execute.
Re: JComboBox with database Oracle.
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.
--- Update ---
I solved the problem, thank you. If it is useful for anyone, here it is:
Code :
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.