By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 820 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

array list

P: 58
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........
Jan 16 '07 #1
Share this Question
Share on Google+
10 Replies


10K+
P: 13,264
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.

Expand|Select|Wrap|Line Numbers
  1.  ArrayList list = new ArrayList(); 
  2. Iterator iterator = list.iterator();
  3.  
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?
Jan 16 '07 #2

P: 58
Now step by step will do it.

Expand|Select|Wrap|Line Numbers
  1.  ArrayList list = new ArrayList(); 
  2. Iterator iterator = list.iterator();
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. org.hibernate.Transaction tx = session.beginTransaction();   
  2.  
  3. //like createstatement in jdbc and cityes is my table in mysql 
  4.  Query q = session.createQuery("from cityes");
  5.  
  6.             Iterator it=q.iterate();
  7.  
  8.             while(it.hasNext())
  9.             {
  10.                 System.out.println(it.next());
  11.  
  12.             }
  13.  
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
Jan 16 '07 #3

10K+
P: 13,264
im using hibernate......
see my code
Expand|Select|Wrap|Line Numbers
  1. org.hibernate.Transaction tx = session.beginTransaction(); 
  2.  
  3. //like createstatement in jdbc and cityes is my table in mysql 
  4. Query q = session.createQuery("from cityes");
  5.  
  6. Iterator it=q.iterate();
  7.  
  8. while(it.hasNext())
  9. {
  10.     System.out.println(it.next());
  11.  
  12. }
  13.  
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?
Jan 16 '07 #4

P: 58
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

Expand|Select|Wrap|Line Numbers
  1.  SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  2.               session =sessionFactory.openSession();    
  3.               Query q = session.createQuery(" from cityes ");
  4.               Iterator it;
  5.               for (it=q.iterate(); it.hasNext();) {
  6.                 cityes c = (cityes) it.next();
  7.                 System.out.println("city: " + c.getCity());
  8.                 System.out.println("state: " + c.getState());
  9.                 System.out.println("country: " + c.getCountry());
  10.  
in this scenario i didn't understand the use of

cityes c=(cityes)it.next();

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_.country 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_.country 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,country in sequence.
Jan 16 '07 #5

10K+
P: 13,264
my output printed as null...

i changed my code again as like this

Expand|Select|Wrap|Line Numbers
  1. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  2.          session =sessionFactory.openSession(); 
  3.          Query q = session.createQuery(" from cityes ");
  4.          Iterator it;
  5.          for (it=q.iterate(); it.hasNext();) {
  6.          cityes c = (cityes) it.next();
  7.          System.out.println("city: " + c.getCity());
  8.          System.out.println("state: " + c.getState());
  9.          System.out.println("country: " + c.getCountry());
  10.  
in this scenario i didn't understand the use of

cityes c=(cityes)it.next();

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_.country 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_.country 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,country in sequence.
Expand|Select|Wrap|Line Numbers
  1.  cityes c=(cityes)it.next();
  2.  
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.
Jan 16 '07 #6

P: 58
Expand|Select|Wrap|Line Numbers
  1.  cityes c=(cityes)it.next();
  2.  
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.java
where i written code to get data

Expand|Select|Wrap|Line Numbers
  1. package cities;
  2. import java.util.Iterator;
  3.  
  4. import org.hibernate.Query;
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.cfg.Configuration;
  8. public class gettingcityes {
  9.     public static void main(String[] args)
  10.     {
  11.         Session session = null;   
  12.         try{
  13.               SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  14.               session =sessionFactory.openSession();    
  15.               Query q = session.createQuery(" from cityes ");
  16.               Iterator it=q.iterate();
  17.               for (it=q.iterate(); it.hasNext();) {
  18.                 cityes c = (cityes) it.next();
  19.                 System.out.println("city: " + c.getCity());
  20.                 System.out.println("state: " + c.getState());
  21.                 System.out.println("country: " + c.getCountry());
  22.                 }
  23.  
  24.  
  25.                 session.close();
  26.  
  27.          }                       
  28.  
  29.     catch(Exception e)
  30.     {   
  31.         System.out.println(e.getMessage());  
  32.     }
  33.     finally{   
  34.         }
  35. }}
  36.  
cityes.java is my POJO class
where i written setters and getter methods..

Expand|Select|Wrap|Line Numbers
  1. package cities;
  2.  
  3. public class cityes {
  4.  
  5.                         private String city;  
  6.             private String state;  
  7.             private String country; 
  8.             private long id;  
  9.  
  10.         public String getCity() 
  11.                                  {
  12.         return city;
  13.         }
  14.         public void setCity(String city) 
  15.                                 {
  16.         this.city = city;
  17.         }
  18.         public String getCountry() 
  19.                                {
  20.         return country;
  21.         }
  22.         public void setCountry(String country) 
  23.                                 {
  24.         this.country = country;
  25.         }
  26.             public String getState() 
  27.                                   {
  28.         return state;
  29.         }
  30.         public void setState(String state) 
  31.                                    {
  32.         this.state = state;
  33.         }
  34.     }
  35.  
Jan 16 '07 #7

10K+
P: 13,264
This is gettingcityes.java
where i written code to get data

Expand|Select|Wrap|Line Numbers
  1. package cities;
  2. import java.util.Iterator;
  3.  
  4. import org.hibernate.Query;
  5. import org.hibernate.Session;
  6. import org.hibernate.SessionFactory;
  7. import org.hibernate.cfg.Configuration;
  8. public class gettingcityes {
  9.     public static void main(String[] args)
  10.     {
  11.         Session session = null; 
  12.         try{
  13.          SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  14.          session =sessionFactory.openSession(); 
  15.          Query q = session.createQuery(" from cityes ");
  16.          Iterator it=q.iterate();
  17.          for (it=q.iterate(); it.hasNext();) {
  18.          cityes c = (cityes) it.next();
  19.          System.out.println("city: " + c.getCity());
  20.          System.out.println("state: " + c.getState());
  21.          System.out.println("country: " + c.getCountry());
  22.          }
  23.  
  24.  
  25.     session.close();
  26.  
  27.          }                     
  28.  
  29.     catch(Exception e)
  30.     { 
  31.         System.out.println(e.getMessage()); 
  32.     }
  33.     finally{ 
  34.         }
  35. }}
  36.  
cityes.java is my POJO class
where i written setters and getter methods..

Expand|Select|Wrap|Line Numbers
  1. package cities;
  2.  
  3. public class cityes {
  4.  
  5.      private String city; 
  6.          private String state; 
  7.          private String country; 
  8.          private long id; 
  9.  
  10.         public String getCity() 
  11. {
  12.         return city;
  13.         }
  14.         public void setCity(String city) 
  15. {
  16.         this.city = city;
  17.         }
  18.         public String getCountry() 
  19. {
  20.         return country;
  21.         }
  22.         public void setCountry(String country) 
  23. {
  24.         this.country = country;
  25.         }
  26.             public String getState() 
  27. {
  28.         return state;
  29.         }
  30.         public void setState(String state) 
  31. {
  32.         this.state = state;
  33.         }
  34.     }
  35.  
Change your exception handling to

Expand|Select|Wrap|Line Numbers
  1.  catch(Exception e) 
  2.     { 
  3.         e.printStackTrace(); 
  4.     }
  5.  
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
Jan 16 '07 #8

P: 58
Change your exception handling to

Expand|Select|Wrap|Line Numbers
  1.  catch(Exception e) 
  2.     { 
  3.         e.printStackTrace(); 
  4.     }
  5.  
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
Jan 16 '07 #9

10K+
P: 13,264
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?
Jan 16 '07 #10

P: 58
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............
Jan 16 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.