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

Thread: Returning multiple results in JDBC

  1. #1
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default Returning multiple results in JDBC

    i want to get multple data from specific criteria from by mysql , however i'm only getting the first row that meets the criteria ere is the code:

    Java Code:
    try
    		{
    			Connection con = DriverManager.getConnection("jdbc:mysql://localhost/?","?","?");
    			Statement stat = con.createStatement ();
    			ResultSet rs = stat.executeQuery("SELECT * FROM ncu_courses WHERE user_name='" + userName +"'");
    			
    			while (rs.next ())
    			{
    				int x = 0;
    				currentCourses[x] = rs.getString("course");
    				lecturer [x] = rs.getString("lecturer");
    				time [x] = rs.getString("time");
    				day [x] = rs.getString("day");
    				x++;
    			}
    		}
    		
    		catch (SQLException e)
    		{
                            System.out.println ("SQL Error " +e.getMessage ());
    			e.printStackTrace ();
    		}

  2. #2
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    You put this line in the wrong place:
    Java Code:
    int x = 0;

  3. #3
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by JeffGrigg View Post
    You put this line in the wrong place:
    Java Code:
    int x = 0;
    ok but i don't think that's the problem

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

    Default

    Quote Originally Posted by mDennis10 View Post
    ok but i don't think that's the problem
    Well, it is; put that line above your while loop and see for yourself.

    kind regards,

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

  5. #5
    JeffGrigg is offline Member
    Join Date
    Aug 2011
    Posts
    95
    Rep Power
    0

    Default

    Quote Originally Posted by mDennis10 View Post
    ok but i don't think that's the problem
    Hint: It's why this line isn't doing anything useful:
    Java Code:
    x++;

  6. #6
    Willriker is offline Member
    Join Date
    Aug 2011
    Posts
    47
    Rep Power
    0

    Default

    You increment the value of X, but then you set it right back to 0 when the loop begins agian.

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

    Default

    Unrelated to this, but learn about PreparedStatements as this should really be one:
    Java Code:
    			Statement stat = con.createStatement ();
    			ResultSet rs = stat.executeQuery("SELECT * FROM ncu_courses WHERE user_name='" + userName +"'");
    Also read up on SQL injection as to one of several reasons why you should.

  8. #8
    mDennis10 is offline Member
    Join Date
    Jul 2011
    Posts
    76
    Rep Power
    0

    Default

    Quote Originally Posted by Willriker View Post
    You increment the value of X, but then you set it right back to 0 when the loop begins agian.
    yes your right didn't realize that i was doing that

Similar Threads

  1. Method Arrays's: Returning multiple values
    By noobForever in forum New To Java
    Replies: 6
    Last Post: 11-02-2010, 01:07 PM
  2. Returning Multiple Value's
    By Hollywood_99 in forum New To Java
    Replies: 8
    Last Post: 10-22-2010, 10:09 AM
  3. JDBC that multiple methods will use
    By evermore in forum JDBC
    Replies: 3
    Last Post: 03-16-2010, 08:27 AM
  4. Replies: 5
    Last Post: 07-18-2008, 05:30 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
  •