473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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........
Jan 16 '07 #1
10 3223
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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

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

Similar topics

6
by: Harry Overs | last post by:
My program needs to take a pointer to BYTE array (unsigned char*) and convert it into a STL list so that each BYTE in the array has its own element in the list, i.e. if the array has hundred bytes...
1
by: aemazing | last post by:
hello, im new to the forum and i wanted to help with my c++ program. the teacher wants us to design a progrm that would keep track of airplanes awaitin landing at an airport. the program will...
1
by: aemazing | last post by:
i've been tryin to do the following - -Add a new flight number to the end of the queue (got it done) -LAnd the plane at the front of the queue - problems wit it- -display the queue - got it done...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
1
by: cyrvb | last post by:
Hello, I'm a very very newbie in C# I did start 2 days ago, I get Visual Stuido 2005 C# I try to understand how to manage the arrays I did write this
10
by: javuchi | last post by:
I just want to share some code with you, and have some comments and improvements if you want. This header file allocates and add and delete items of any kind of data from a very fast array: ...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
4
by: Bob Bedford | last post by:
Hello, I've an array of array(1,2,3,4). I'd like to retrieve the values in 3 and 4 giving the values 1 and 2. How can I do that, how to search in an array of array ? in short, how to write the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.