aspose file tools*
The moose likes JDBC and the fly likes how to set the beans object into an arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to set the beans object into an arraylist " Watch "how to set the beans object into an arraylist " New topic
Author

how to set the beans object into an arraylist

bandaru Babu
Greenhorn

Joined: Jan 09, 2009
Posts: 5
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

Joined: Jan 10, 2002
Posts: 61768
    
  67

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.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453



making the code readable


SCJP 5.0 SCWCD 5.0
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 29, 2008
Posts: 12617

The code posted originally shouldn't even compile (I think; hard to read).
avi sinha
Ranch Hand

Joined: Mar 15, 2009
Posts: 453

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to set the beans object into an arraylist