• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to set the beans object into an arraylist

 
bandaru Babu
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("jdbcdbc: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.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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.
 
avi sinha
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
 
David Newton
Author
Rancher
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
Rancher
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic