Results 1 to 5 of 5
  1. #1
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default Collecting ResultSet data and Comparing

    Hi everyone,

    I have problem with Collecting data from sql query into Array or ArrayList and comparing them with outside values.
    Java Code:
    public void roomCheck()throws SQLException,ClassNotFoundException{
    	
    	
        String roo = jTextField11.getText();
       
    	   
        try {
        	
        
    		databaseConnection();
    		
    		stm=con.createStatement();
    		rs=stm.executeQuery("select * from reservation");
    		
            int room = Integer.parseInt(roo);
            
    		while(rs.next()){
      
    			ArrayList roomNumbers= new ArrayList();
    			roomNumbers.add(rs.getInt("odano"));
                 Iterator it=roomNumbers.iterator();
                 
                 while(it.hasNext()){
                	 
                 if(it.next().equals(room)){
                	 
                 JOptionPane.showMessageDialog(null,"Warning !!!! The room is full");
                 }
                 else{
                	 reservationAdd();
                 }
                 }
                 }
    	
    		}
    		catch(SQLException e){
    			
    			e.getCause();
    		}
    		stm.close();
    		con.close();
    		
    	}
    here my problem is ArrayList gets only one data, when I sort them with
    System.out.println(it.next()) ;
    I can see the datas without problem however when I compare them in if else condition, it compares only one data and pass directly reservationAdd() method.
    even if condition is wrong else statements (reservationAdd()) running, how do you think I should go here ?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As noted in your previous thread, please don't cross-post the same question in multiple fora without providing links from one to the other. This goes against forum etiquette and may upset some forum members (many who would otherwise help you). Best of luck.

  3. #3
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    844
    Rep Power
    6

    Default

    Hi,
    There are major logical loopholes in your code.
    These things u need to do.
    1.First,you catch everything into ArrayList.
    ArrayList roomNumbers=null;
    while(rs.next())
    {
    roomNumbers= new ArrayList();
    roomNumbers.add(rs.getInt("odano"));
    }
    2.Then using for loop u check like this below

    for(int i=0;i<roomNumbers.size();i++)
    {
    //check for roo not equal to null and check for integer then
    if satisfied then typecast to int and retreive the arraylist value using rooNumbers.get(i) and convert to int and compare.
    }
    Ramya:cool:

  5. #5
    casid is offline Member
    Join Date
    Nov 2008
    Posts
    18
    Rep Power
    0

    Default

    Sorry at first I didint understand, for now I wont create two thread at the same topic
    so Sorry, if you have chance to remove one ,go ahead, no problem I couldnt realize it.


    thank you RamyaSivakanth, I will try it and tell the result.
    Last edited by casid; 06-08-2009 at 10:00 PM.

Similar Threads

  1. Comparing two data types in order to store in array
    By gwithey in forum New To Java
    Replies: 3
    Last Post: 05-01-2009, 11:27 AM
  2. Comparing two images
    By GhosT in forum Advanced Java
    Replies: 13
    Last Post: 04-25-2009, 01:37 AM
  3. Comparing Strings
    By souFrag in forum Advanced Java
    Replies: 5
    Last Post: 05-21-2008, 10:03 AM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 10:44 AM
  5. comparing
    By Feng in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 10:40 AM

Posting Permissions

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