• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to set the beans object into an arraylist

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.io.*;
import java.sql.*;

public class JdbcRetrieval
{
ArrayList retrieval(String COURSE,String YEAR)throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:total","system","oracle");
Statement stmt=con.createStatement();

String query="select SNAME,HTICKETNO from total where COURSE='"+COURSE+"' and YEAR='"+YEAR+"'";
System.out.println(query);

ResultSet res=stmt.executeQuery(query);
// CourseStudents cs1=new CourseStudents();
// ArrayList a1=new ArrayList();
while(res.next())
{
// System.out.println(res.getString(1)+" "+res.getInt(2));
ArrayList al=new ArrayList(new CourseStudents());
cs1.setStuname(res.getString(1));
cs1.setStuno(res.getInt(2));

}
res.close();
stmt.close();
con.close();
return cs1;

}

}


this is my jdbc program which is called by a servlet. in this program table records are retrieved correctly. my question is " how can i add the beans(CourseStudents) to an ArrayList for all the records of a table. please help me in this problem.
Thanks in advance.

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


making the code readable
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
res.next() will iterate over the rows (one row per loop) . in the loop a new bean will be created and will be added in the list.
try it. you haven't imported the util package . i have done it for you.



avi sinha
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

making the code readable


Not really. Without indentation it's still pretty tough. Make it easy on the people trying to help.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code posted originally shouldn't even compile (I think; hard to read).
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:The code posted originally shouldn't even compile (I think; hard to read).



of course sir definitely the code will not compile

* improper imports
* conflict in return type

i just wanted to understand the code that's why i just copied the code and pasted within the code block.

should we wait for the topic creator to make the code readable ??? what's your opinion sir ??

avi sinha
 
reply
    Bookmark Topic Watch Topic
  • New Topic