Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How can i get multiple records from mySQl database

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Location
    Jamaica
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How can i get multiple records from mySQl database

    i want to get all the records that has a particular user name from my database and store each into a parallel array. The problem i am having is that its only getting the last record. How can i get the all record ere is an example of the code.
    		String [] day = new String [5];
    		String [] time = new String [5];
    		String [] course = new String [5];
     
    		try
    		{
    			Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/somedatase","some name","");
    			Statement state = connect.createStatement ();
    			ResultSet rs = state.executeQuery("Select * FROM time_table WHERE user_name='" + user +"'");
     
    			int x = 0;
    			while (rs.next ())
    			{
    				day[x] = rs.getString("day");
    				time[x] = rs.getString ("time");
    				course[x] = rs.getString ("course");
    			}
     
    			connect.close ();
    			state.close ();
    			rs.close ();
    		}//end try
     
    		catch (SQLException e)
    		{
    			e.printStackTrace ();
    		}//end catch
    Last edited by mDennis10; September 2nd, 2011 at 05:30 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How can i get multiple records from mySQl database

    Word of advice: map the data to an object - parallel arrays can get ugly very fast. That being said, add some println's to your while loop. If you did this an printed out the variable x, you will realize you never increment this value, and thus continually set index 0 of your arrays for each loop.

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Location
    Jamaica
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How can i get multiple records from mySQl database

    didn't reallize i did that thanks alot

Similar Threads

  1. Replies: 2
    Last Post: July 15th, 2011, 02:39 AM
  2. [SOLVED] Connection to MySql Database
    By dego89 in forum JDBC & Databases
    Replies: 8
    Last Post: May 26th, 2011, 08:56 AM
  3. MySQL Database
    By pjeremy90 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 12th, 2010, 10:34 AM
  4. [SOLVED] Get Data From Database MySQL using ComboBox
    By Simple in forum Java Theory & Questions
    Replies: 2
    Last Post: June 4th, 2010, 02:45 AM
  5. Download File from Mysql database
    By Puk284 in forum Java Servlet
    Replies: 0
    Last Post: April 24th, 2010, 07:43 AM