Results 1 to 5 of 5
  1. #1
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default JDBC with Swing question

    ok hi im pretty new to JDBC i was just wondering if anyone can help with this question i have ok here's the question. i was just wondering is that when you get a resultsset back from the database can you display all the results for that query in the different JLabels. like the first row received back will be display in the first jlabel and the second row received back will be display in the second jlabel and so on...

    im sorry if i posted this in the wrong section im only a new member to this website. any help with the question mentioned above would be great thank you.

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

    Default

    Check out camickr's Table from Database
    Table From Database Java Tips Weblog

    db

  3. #3
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    ya i seen using Jtables to display resultset i was wondering if there is a way to display the results in JLabels or is that process the same for both jtables and jlabels?

  4. #4
    Join Date
    Jan 2011
    Posts
    23
    Rep Power
    0

    Default

    yes there is .... you have to :

    - store the JDBC variables into another variable

    for example :

    Statement sqlCommand = conn.createStatement();
    Statement sqlCommand2 = conn.createStatement();


    ResultSet res ;
    ResultSet res2;

    res = sqlCommand.executeQuery("SELECT * FROM Person WHERE personID = 1")
    res2 = sqlCommand2.executeQuery("SELECT * FROM Person WHERE personID = 2")

    while (res.next()) {
    firstname1 = res.getString("firstname");
    lastname1 = res.getString("lastname");
    }

    while (res2.next()) {
    firstname2 = res2.getString("firstname");
    lastname2 = res2.getString("lastname");
    }

    So now you have stored all the variables you got from the database in java variables...Next step is making getter methods for the java variables:

    public String getFirstName1(){
    return firstname1;
    }

    public String getLastName1(){
    return lastname1;
    }

    public String getFirstName2(){
    return firstname2;
    }

    public String getLastName2(){
    return lastname2;
    }


    And the final step...you use the variables on the JLabels...for example

    private static JLabel label1, label2;

    label1 = new JLabel();
    label1.setText(getFirstName1() + " " + getLastName1() );

    label2 = new JLabel();
    label2.setText(getLastName2() + " " + getLastName2() );

    But be careful ..JLabel's only work with Strings

    I Hope this helps !
    Last edited by ady_bavarezu89; 04-22-2011 at 04:50 PM.

  5. #5
    hacker9116 is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Hi ady_bavarezu89 thanks for your reply i actually figured this out about a week ago i completely forgot to come back here and post my solution but it essentially the same as yours, thanks for your reply though i will change this post to SOLVED

Similar Threads

  1. JDBC MySql question
    By santa in forum New To Java
    Replies: 12
    Last Post: 01-18-2011, 03:47 PM
  2. A question on JDBC&SQL Server
    By an24 in forum JDBC
    Replies: 5
    Last Post: 01-18-2010, 11:23 AM
  3. JDBC statement question
    By nick2price in forum Advanced Java
    Replies: 21
    Last Post: 09-29-2008, 02:09 PM
  4. jdbc/ swing code problem
    By sami in forum Advanced Java
    Replies: 1
    Last Post: 08-13-2007, 02:26 PM

Tags for this Thread

Posting Permissions

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