In Netbeans, how do I run an SQL Query through binding values to a component like JComboBox

Posted on 2009-02-21
Last Modified: 2013-11-23
Please view thee image. I can get a list of the database, but I really want to view just one item by querying for example "SELECT name FROM people", that way my combobox will get the exact value from the database without any extra information. I have used the procedure part of NetBeans from the database services but im sure ive seen a querying section which I can target from something like this.
Question by:namsu55
    LVL 86

    Expert Comment

    Are you not using TopLink?

    Author Comment

    I tried the top link ill show you what it gives me:


    I would prefer to get something like 2 as that is the value of the field I am targeting.

    But I dont actually know how to run a proper query inside netbeans for the binding part. If I had a query, then I could select certain values and only those would come up in the list.

    Author Comment

    I could use an array or something to do the sql part for me couldnt I and then just load the combo box up with some values.
    LVL 15

    Accepted Solution

    Actually, I answered on
    Is is what you are asking?

    private void jComboBox1_itemStateChanged(ItemEvent e) {
            String comboBoxValue;
            String Parameter;
            String[] pair;
            int beginIndex, endIndex;
            comboBoxValue = String.valueOf( jComboBox1.getItemAt(jComboBox1.getSelectedIndex() ));
            beginIndex = comboBoxValue.indexOf("[") +1;
            endIndex = comboBoxValue.indexOf("]");
            Parameter = comboBoxValue.substring(beginIndex,endIndex);
            System.out.println("Parameter : " + Parameter);
            pair = Parameter.split("=");
            System.out.println("value of figher : " + pair[1]);
    //        System.out.println(e.toString() + e.getStateChange());
    //        System.out.println(jComboBox1.getItemAt(jComboBox1.getSelectedIndex() )) ;

    Open in new window

    LVL 86

    Expert Comment

    I'm not at all clear on what your required functionality is. If you show me what you think some *actual* sql would look like, that should clarify it
    LVL 92

    Expert Comment

    you can use a custom renderer (or just a toString()) to allow you to store any kind of data in your combos model.

    So you could for example store the ids
    or use a class that has the id and the string to display

    let me know if you have any questions

    LVL 92

    Expert Comment

    so for example if you had a Data class that stored the details of each entry in the combo you would store the actual Database objects in the list model, and then to get selected you can use:

    Database db = (Database) combo.getSelectedItem();

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now