Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to extract and print the contents of arraylist of java bean objects?

 
Shree Chinnu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.util.ArrayList;
//import java.util.Iterator;

public class ClassDetails {
public ArrayList<MYBean> getDetails(String aa){

Connection conn = null;
ResultSet rst =null;
PreparedStatement preparedStatement = null;
ArrayList<MYBean> MYList = new ArrayList<MYBean>();
try {
final String CONNECTION_STRING = "";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection(CONNECTION_STRING);
String strQuery = "SELECT * FROM XXX where xxx.yy = " +aa;
preparedStatement = conn.prepareStatement(strQuery);
rst = preparedStatement.executeQuery();

while (rst.next()) {
MYBean MyDetails = new MYBean();
MYList .add(MyDetails);
}
conn.close();
rst.close();
preparedStatement.close();
}
catch (ClassNotFoundException cnfex) {
cnfex.printStackTrace();
} catch (SQLException sqlex) {
sqlex.printStackTrace();
} catch (Exception excp) {
excp.printStackTrace();
}
return MYList ;
}
}


MYBean has about 6 fielda along with getter and setter methods.

MYlist is an array list of Mybean that has to be returned to another program.
How can I print the results that is returned by this program in another java program.
In other words how can i print the contents of this arraylist.

 
vicky ma
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write it to some file using IO streams and second program can read the same
<Crude method but if you dont want to change system architecture its best way to do .>
Hope some1 will come up with better solution <may be using some design patterns (i haven't learned them yet )>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "please help urgent" is not helpful. What would happen if all posts had such a title?

Please go back and change your post to add a more meaningful subject by clicking the button on your post.

Also, please read this for more information on why using "urgent" will actually hurt your chances of getting quick answers.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MYlist is an array list of Mybean that has to be returned to another program.

Could you describe the architecture of your app a little bit, that would help us answer this question. If I were doing this I would use either the messaging API or the web services API. But then I work primarily with Java EE apps.
 
Shree Chinnu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a web service. I have to send the array of java beans to the client and print the results.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic