Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how to set the beans object into an arraylist

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.

 
Marshal
Posts: 67424
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
 
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic