array list

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shanmukhi
    New Member
    • Jan 2007
    • 58

    array list

    hi,

    how can i attach an iterator to array list and how can i get objects from database into that array list and how can i display that data on browser....

    can any one help me........
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by shanmukhi
    hi,

    how can i attach an iterator to array list and how can i get objects from database into that array list and how can i display that data on browser....

    can any one help me........
    Now step by step will do it.

    Code:
     ArrayList list = new ArrayList(); 
    Iterator iterator = list.iterator();
    Creates the iterator from the list. Are you using jdbc to get the data from the database? How do you want to present the data to the web page? Are you using JSP? What have you done so far?

    Comment

    • shanmukhi
      New Member
      • Jan 2007
      • 58

      #3
      Originally posted by r035198x
      Now step by step will do it.

      Code:
       ArrayList list = new ArrayList(); 
      Iterator iterator = list.iterator();
      Creates the iterator from the list. Are you using jdbc to get the data from the database? How do you want to present the data to the web page? Are you using JSP? What have you done so far?

      im using hibernate......
      see my code
      Code:
      org.hibernate.Transaction tx = session.beginTransaction();   
      
      //like createstatement in jdbc and cityes is my table in mysql 
       Query q = session.createQuery("from cityes");
                
                  Iterator it=q.iterate();
                  
                  while(it.hasNext())
                  {
                  	System.out.println(it.next());
                  	
                  }
      but iam not getting the data from table.........
      my output is null

      is there any way to retrieve by linking iterator to array list..
      can u suggest a way to write code

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by shanmukhi
        im using hibernate......
        see my code
        Code:
        org.hibernate.Transaction tx = session.beginTransaction(); 
         
        //like createstatement in jdbc and cityes is my table in mysql 
        Query q = session.createQuery("from cityes");
         
        Iterator it=q.iterate();
         
        while(it.hasNext())
        {
        	System.out.println(it.next());
         
        }
        but iam not getting the data from table.........
        my output is null

        is there any way to retrieve by linking iterator to array list..
        can u suggest a way to write code
        Hold on there. Are getting null printed to the screen or are you getting nothing printed to the screen?

        Comment

        • shanmukhi
          New Member
          • Jan 2007
          • 58

          #5
          Originally posted by r035198x
          Hold on there. Are getting null printed to the screen or are you getting nothing printed to the screen?
          my output printed as null...

          i changed my code again as like this

          Code:
           SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
          		      session =sessionFactory.openSession();    
          		      Query q = session.createQuery(" from cityes ");
          		      Iterator it;
          		      for (it=q.iterate(); it.hasNext();) {
          		        cityes c = (cityes) it.next();
          		        System.out.println("city: " + c.getCity());
          		        System.out.println("state: " + c.getState());
          		        System.out.println("country: " + c.getCountry());
          in this scenario i didn't understand the use of

          cityes c=(cityes)it.ne xt();

          can u explain what is the use of this line?

          now iam getting ouput as

          city: mangalore
          Hibernate: select cityes0_.city as city0_, cityes0_.state as state0_0_, cityes0_.countr y as country0_0_ from cityes cityes0_ where cityes0_.city=?
          state: karnataka
          country: india

          city: hyderabad
          Hibernate: select cityes0_.city as city0_, cityes0_.state as state0_0_, cityes0_.countr y as country0_0_ from cityes cityes0_ where cityes0_.city=?
          state: andhra
          country: india

          one thing here i want to know ,that is why im getting Hibernate: statement after displaying city:mangalore.

          how can i get city,state,coun try in sequence.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by shanmukhi
            my output printed as null...

            i changed my code again as like this

            Code:
            SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
            		 session =sessionFactory.openSession(); 
            		 Query q = session.createQuery(" from cityes ");
            		 Iterator it;
            		 for (it=q.iterate(); it.hasNext();) {
            		 cityes c = (cityes) it.next();
            		 System.out.println("city: " + c.getCity());
            		 System.out.println("state: " + c.getState());
            		 System.out.println("country: " + c.getCountry());
            in this scenario i didn't understand the use of

            cityes c=(cityes)it.ne xt();

            can u explain what is the use of this line?

            now iam getting ouput as

            city: mangalore
            Hibernate: select cityes0_.city as city0_, cityes0_.state as state0_0_, cityes0_.countr y as country0_0_ from cityes cityes0_ where cityes0_.city=?
            state: karnataka
            country: india

            city: hyderabad
            Hibernate: select cityes0_.city as city0_, cityes0_.state as state0_0_, cityes0_.countr y as country0_0_ from cityes cityes0_ where cityes0_.city=?
            state: andhra
            country: india

            one thing here i want to know ,that is why im getting Hibernate: statement after displaying city:mangalore.

            how can i get city,state,coun try in sequence.
            Code:
             cityes c=(cityes)it.next();
            it.next() moves to the next item in the iterator. This will be an Object but you will want to type cast it to a cityes object to be able to call it's getCountry() method (and all of its other method)

            Can you post the complete program including the cityes class if you have it.

            Comment

            • shanmukhi
              New Member
              • Jan 2007
              • 58

              #7
              Originally posted by r035198x
              Code:
               cityes c=(cityes)it.next();
              it.next() moves to the next item in the iterator. This will be an Object but you will want to type cast it to a cityes object to be able to call it's getCountry() method (and all of its other method)

              Can you post the complete program including the cityes class if you have it.

              This is gettingcityes.j ava
              where i written code to get data

              Code:
              package cities;
              import java.util.Iterator;
              
              import org.hibernate.Query;
              import org.hibernate.Session;
              import org.hibernate.SessionFactory;
              import org.hibernate.cfg.Configuration;
              public class gettingcityes {
              	public static void main(String[] args)
              	{
              		Session session = null;   
              		try{
              		      SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
              		      session =sessionFactory.openSession();    
              		      Query q = session.createQuery(" from cityes ");
              		      Iterator it=q.iterate();
              		      for (it=q.iterate(); it.hasNext();) {
              		        cityes c = (cityes) it.next();
              		        System.out.println("city: " + c.getCity());
              		        System.out.println("state: " + c.getState());
              		        System.out.println("country: " + c.getCountry());
              		        }
              		      
              		   
                          	session.close();
              		    
              		 }		  			 
              		           	
              	catch(Exception e)
              	{   
              		System.out.println(e.getMessage());  
              	}
              	finally{   
              		}
              }}
              cityes.java is my POJO class
              where i written setters and getter methods..

              Code:
              package cities;
              
              public class cityes {
              		 
              	                    private String city;  
              		    private String state;  
              		    private String country; 
              		    private long id;  
              		
              		public String getCity() 
                                               {
              		return city;
              		}
              		public void setCity(String city) 
                                              {
              		this.city = city;
              		}
              		public String getCountry() 
                                             {
              		return country;
              		}
              		public void setCountry(String country) 
                                              {
              		this.country = country;
              		}
              			public String getState() 
                                                {
              		return state;
              		}
              		public void setState(String state) 
                                                 {
              		this.state = state;
              		}
              	}

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by shanmukhi
                This is gettingcityes.j ava
                where i written code to get data

                Code:
                package cities;
                import java.util.Iterator;
                 
                import org.hibernate.Query;
                import org.hibernate.Session;
                import org.hibernate.SessionFactory;
                import org.hibernate.cfg.Configuration;
                public class gettingcityes {
                	public static void main(String[] args)
                	{
                		Session session = null; 
                		try{
                		 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
                		 session =sessionFactory.openSession(); 
                		 Query q = session.createQuery(" from cityes ");
                		 Iterator it=q.iterate();
                		 for (it=q.iterate(); it.hasNext();) {
                		 cityes c = (cityes) it.next();
                		 System.out.println("city: " + c.getCity());
                		 System.out.println("state: " + c.getState());
                		 System.out.println("country: " + c.getCountry());
                		 }
                 
                 
                	session.close();
                 
                		 }					 
                 
                	catch(Exception e)
                	{ 
                		System.out.println(e.getMessage()); 
                	}
                	finally{ 
                		}
                }}
                cityes.java is my POJO class
                where i written setters and getter methods..

                Code:
                package cities;
                 
                public class cityes {
                 
                	 private String city; 
                		 private String state; 
                		 private String country; 
                		 private long id; 
                 
                		public String getCity() 
                {
                		return city;
                		}
                		public void setCity(String city) 
                {
                		this.city = city;
                		}
                		public String getCountry() 
                {
                		return country;
                		}
                		public void setCountry(String country) 
                {
                		this.country = country;
                		}
                			public String getState() 
                {
                		return state;
                		}
                		public void setState(String state) 
                {
                		this.state = state;
                		}
                	}
                Change your exception handling to

                Code:
                 catch(Exception e) 
                	{ 
                		e.printStackTrace(); 
                	}
                Remove the finally block. Now run it and tell us what it prints.

                Note it is common practice to name classes beginning with a capital letter e.g GettingCityes and Cityes

                Comment

                • shanmukhi
                  New Member
                  • Jan 2007
                  • 58

                  #9
                  Originally posted by r035198x
                  Change your exception handling to

                  Code:
                   catch(Exception e) 
                  	{ 
                  		e.printStackTrace(); 
                  	}
                  Remove the finally block. Now run it and tell us what it prints.

                  Note it is common practice to name classes beginning with a capital letter e.g GettingCityes and Cityes
                  i got the same result........

                  then-
                  what is the difference between getMessage and printStackTrace

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by shanmukhi
                    i got the same result........

                    then-
                    what is the difference between getMessage and printStackTrace
                    Not much.
                    I can't see why you are having the hibernate statement printed. I thought may be It was ... ah but then it couldn't have been. Are you sure you've compiled the files again before testing?

                    Comment

                    • shanmukhi
                      New Member
                      • Jan 2007
                      • 58

                      #11
                      Originally posted by r035198x
                      Not much.
                      I can't see why you are having the hibernate statement printed. I thought may be It was ... ah but then it couldn't have been. Are you sure you've compiled the files again before testing?
                      im using eclipse........

                      so no need of bothering about those things ........

                      and can u tell me how can i perform updations using hibernate...... .
                      im able to insert but how to delete......... ...

                      Comment

                      Working...