Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,639 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,080 people online right now. Registration is fast and FREE... Join Now!




Updating JTable from data recieved from resultset

 
Reply to this topicStart new topic

Updating JTable from data recieved from resultset

nick2price
post 12 Oct, 2008 - 07:34 AM
Post #1


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


I have an athletics database, where the user updates an event and round e.g. 100M run Round 1. I then have a query which returns the 3 fastest times from each round for whatever event.
Now on my JTable gui, the user chooses what event and round to update through 2 JCombo boxes, one for events, one for rounds. Now if the user chooses the round name final, i need the JTable to update the table with the three fastest times from rounds 1, 2 and 3.
Whats the best way to do this? I was thinking putting all the different result sets into an array, and then through an if loop have somthing like
CODE

if(roundType.equals (final))



then in the body of this loop, create an instance of my JTable setting the rows for each column the the resultset data.
Is this the right approach or any other advise?
cheers
User is offlineProfile CardPM

Go to the top of the page

Unknown Hero
post 12 Oct, 2008 - 11:20 AM
Post #2


New D.I.C Head

Group Icon
Joined: 4 Sep, 2007
Posts: 35



Thanked 7 times

Dream Kudos: 50
My Contributions


Can you please post a screenshot of what you've done so far? On that way I would be able to completely understand your question.
User is offlineProfile CardPM

Go to the top of the page

nick2price
post 12 Oct, 2008 - 12:00 PM
Post #3


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


I will try and explain this the best i can
IPB Image

Above is a screenshot of what i have done. The user chooses an event from the combobox on the left. Events are 100, 200, 400 and 800M run.
Then the user chooses a round. Rounds are round 1, 2, 3 and final.
Once these have been choosen, r.g. 100M run, round 1, the user enters the information into the JTable and clicks save. This is then saved in my database via JDBC.
Now lets say the user enters the information for 100M run, rounds 1, 2, and 3. The final is determined by the 3 fastest from each round.
I ahve done the following query and resultset to return the 3 fastest into a list.
CODE
public List<Integer> getFastest(String eveType, String roundType)  
    {
        
        String roundType2 = roundType;
        String eveType2 = eveType;
        
     String SELECT_TOP_TIMES =
      "select TOP 3 r.result " +
      "from tblResults as r " +
      "where r.Event_ID = " +
            "(select e.ID " +
            "from tblEvent as e " +
            "where e.Event_Name = ?) " +
      "and r.Round_ID = " +
              "(select ro.ID " +
            "from tblRound as ro " +
            "where ro.Round_Number = ?) " +
            "order by r.result";

    
    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Integer> topTimes = new ArrayList<Integer>();
    
    try
    {
        con = DatabaseUtils.connect(DRIVER, URL);  
        ps = con.prepareStatement(SELECT_TOP_TIMES);
        
        for(int i = 0; i <= 2; i++)
        {
        ps.setString(1, eveType2);
        ps.setString(2, roundType2);
        }
        rs = ps.executeQuery();
        
        while (rs.next())
        {
            int result = rs.getInt(1);
            topTimes.add(result);
        }
        
        //System.out.println(topTimes);

    }
    catch(Exception e)
        {
            System.out.println(e);
            DatabaseUtils.rollback(con);
            e.printStackTrace();
        }
    finally
        {
            DatabaseUtils.close(rs);
            DatabaseUtils.close(ps);
            DatabaseUtils.close(con);
        }
return topTimes;


}


This might need to change however because i need more than the 3 fastest times returned, i need their name etc. I can just change this to an object i create.
Now in another class, i am testing out this method just by doing
CODE
    List<Integer> bestTimes = sql.getFastest(sType, sType2);
    System.out.println(bestTimes);

Where sType and sType2 are parameters for eventName and RoundNumber. This works fine and returns the 3 fastest times for whatever event/round i choose.
What i need to do now though is somehow create a method that will return the three fastest for each event/round into an object or somthing, so i can then load the names and nationalities into the JTable for final.
So my whole goal is if the user chooses final from the JCombo box, the 3 fastest from each round should be in there...apart from the times.
Just lemme know if you wanna see my classes so far.
cheers
User is offlineProfile CardPM

Go to the top of the page

nick2price
post 13 Oct, 2008 - 01:43 AM
Post #4


D.I.C Head

**
Joined: 23 Nov, 2007
Posts: 231



Thanked 6 times
My Contributions


Anyone have an opion?
User is offlineProfile CardPM

Go to the top of the page

pbl
post 13 Oct, 2008 - 07:07 PM
Post #5


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


QUOTE(nick2price @ 12 Oct, 2008 - 08:34 AM) *

then in the body of this loop, create an instance of my JTable setting the rows for each column the the resultset data.
Is this the right approach or any other advise?
cheers


Hope you won't create an instance of JTable every time.

You build one only once and then populate it with the data contained in your ResultSet
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 04:27AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month