Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By JosAH

Thread: JComboBox with database making problem on retrive

  1. #1
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Question JComboBox with database making problem on retrive

    HI All!

    In my project i am using JComboBox with Database

    I wants to retrives the date from database to JComboBox

    my code is

    Java Code:
    public void showcom()
    {
         try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip","root","root");
            PreparedStatement ps=conn.prepareStatement("select EmployeeID from newemp");
            //PreparedStatement ps=conn.prepareStatement("select * from newemp");
            ResultSet rs=ps.executeQuery();
            if(rs.next())
            {
            combob.addItem(rs.getString("EmployeeID"));
            
            }
        }
            catch(Exception e)
            {
                
            }
    }
    I have near with 50 record in that table.

    but it showing the first employee id only, doesn't show the remaining Employee ID's
    what is the problem i did in my cod,
    can anyone Guide me..,

    Thank you..,

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JComboBox with database making problem on retrive

    You're only adding one record to your combo box, no matter how many records are stored in your table; add a loop somewhere in your code, not just an if-statement.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: JComboBox with database making problem on retrive

    Thank You Mr.Jos!
    i will try now self..,
    Thanks For your Idea sir..,

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: JComboBox with database making problem on retrive

    Yes, that 'if' should be a 'while'.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: JComboBox with database making problem on retrive

    sir!

    because of my mistaken i receive the same problem,
    the first records only repeated as numbers of rows..,

    how to i correct in that loop sir..,


    Java Code:
    if(rs.next())
            {
               for(int i=0;i<=rs.getRow();i++)
               {
            jComboBox1.addItem(rs.getString("EmployeeID"));
               }
            
            }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JComboBox with database making problem on retrive

    You're using the wrong method (read the API documentation for the ResultSet interface); you should use a combination of rs.next(), rs.getXXX() method calls.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: JComboBox with database making problem on retrive

    sir i try with google help,
    at the time i was found the fallowing samples sir..,

    Add Items in JComboBox from Access Database

    populate jCombobox with database data

    in that sample code are using the same methods that's why i use..,

    ok sir! please guide me, what i have to do now..,

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JComboBox with database making problem on retrive

    RoseIndia is an extremey crappy site; use a simple loop:

    Java Code:
    while (rs.next()) {
       // fetch a column and add it to your combo box
    }
    kind regards,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: JComboBox with database making problem on retrive

    Which is what I said...it's amazing how sometimes our posts simply go invisible.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JComboBox with database making problem on retrive

    Quote Originally Posted by Tolls View Post
    Which is what I said...it's amazing how sometimes our posts simply go invisible.
    Duh, but I said it first (with an excellent eloquent verbiage, if I may say so myself ;-)

    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JComboBox with database making problem on retrive

    OT (partly): I wish Google would catch on to whatever nefarious scheming keeps that worthless stinking waste of bandwidth at or near the top of their searches.

    The first time I discovered the site, I found a code 'example' that extended JFrame but wasn't used as one; the JFrame that was shown was declared and constructed inside a method; a local variable. Months later, they hadn't corrected the code. Chances are nobody even read my feedback.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,772
    Blog Entries
    7
    Rep Power
    21

    Default Re: JComboBox with database making problem on retrive

    Are you criticizing that beautiful gem? That source of wisdom? The holy knowledge base that enlightens millions of poor souls, searching for the ultimate truth? tsk, tsk ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: JComboBox with database making problem on retrive

    Their servers should be immersed in the Ganges. Along with their coders, who by no stretch of the imagination can be called programmers.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  14. #14
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: JComboBox with database making problem on retrive

    sorry for the late response sir..,
    i am living in out of city that's why i unable to get good internet connection,
    that is the only reason for my invisibility..,
    forgive me please for my invisibility with this Forum, while you response for my post..,

  15. #15
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Thumbs up Re: JComboBox with database making problem on retrive

    Thanks For My Guides

    Jest i try my self, it is working well

    instead of using "if" i used "while" loop and remove the "for" condition

    my code is

    Java Code:
     try
        {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn=(Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/payslip","root","root");
            PreparedStatement ps=conn.prepareStatement("select EmployeeID from newemp");
            ResultSet rs=ps.executeQuery();
            while(rs.next())
            {
                {
                 
            jComboBox1.addItem(rs.getString("EmployeeID"));
    
                }
            }
        }
            catch(Exception e)
            {
               JOptionPane.showMessageDialog(rootPane, e);
            }


    it add all the elements from my MySQL to Java Coding..,
    Thank you For All..,

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: JComboBox with database making problem on retrive

    It's not your invisibility.
    It's that you didn't seem to acknowledge the answers given by us here.
    If you didn't understand them then just ask, but not taking in what we say leaves us a little lost as to what problems you are having.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  17. #17
    raj.mscking@gmail.com's Avatar
    raj.mscking@gmail.com is offline Senior Member
    Join Date
    Jan 2012
    Location
    TamilNadu
    Posts
    290
    Rep Power
    3

    Default Re: JComboBox with database making problem on retrive

    ok sir!
    i can understand ..,

Similar Threads

  1. link jcombobox with database
    By amer in forum NetBeans
    Replies: 2
    Last Post: 10-15-2010, 09:32 PM
  2. Jcombobox with mysql database
    By kesariramachandran in forum AWT / Swing
    Replies: 2
    Last Post: 05-18-2010, 10:32 PM
  3. populate jCombobox with database data
    By joeyxaza in forum JDBC
    Replies: 0
    Last Post: 01-19-2009, 05:30 PM
  4. jComboBox and database
    By pravin2008 in forum AWT / Swing
    Replies: 3
    Last Post: 08-25-2008, 05:08 PM
  5. Replies: 2
    Last Post: 07-30-2008, 01:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •