Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: JComboBox with database Oracle.

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question 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!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Thabby (December 12th, 2012)

  4. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    6
    My Mood
    HaHa
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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(
    "jdbcracle: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.

Similar Threads

  1. Java Barcode Program with Oracle database
    By techsing14 in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: December 6th, 2012, 05:19 AM
  2. using 2 jcombobox and mysql database
    By cnjiru in forum JDBC & Databases
    Replies: 0
    Last Post: June 14th, 2012, 09:47 AM
  3. Inserting Large XML file into Oracle Database through java
    By hari_582 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 15th, 2012, 03:41 AM
  4. Help with UTF-8 characters into Oracle database
    By Scomage in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 13th, 2012, 12:26 PM
  5. Database connectivity problem-Oracle
    By Entrant in forum JDBC & Databases
    Replies: 3
    Last Post: October 11th, 2009, 10:08 AM

Tags for this Thread