?
Solved

Bean to bean communication

Posted on 2003-03-23
7
Medium Priority
?
536 Views
Last Modified: 2008-03-06
Hi!

I am running a JSP web application which displays data from a mySQL database. I have developed a collection of Beans to take care of a lot of the calculations, databaseaccess and so on.

Fx. I have a DatabaseBean with the method :  

                   ResultSet executeQuery(String query, String username, String password)

I can then from the JSP use this method like this:

<%
ResultSet rs = DatabaseBean.executeQuery("Select username from user where userID=3","myname","mypw");
%>

I would very much like to be able to let the beans communicate with each other, but I don't know how! Fx. it would be nice to use the allready written DatabaseBean-methods in my other beans, or just to exchange variables between beans.

At the moment I can only make beans communicate via the jsp-pages, and it is troublesome!

Can you help?


0
Comment
Question by:NicolaiKrog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 4

Expert Comment

by:thanassis
ID: 8190159
A bean is a java class. So you can make other classes that use your bean classes.
For example you have a bean that collect passengers (constracted from the file passList.java).
You have another that has airplanes(airplanesList.java)
and one more that has airseats(airseatsList.java)

You can make a java class that use the above to constract a trip:

class Trip{
 PassList;
 AirPlaneList;
 AirSeatsList;
 DbConnection;
 
}

andin your jsp page you can use only the trip bean!
0
 
LVL 3

Expert Comment

by:allahabad
ID: 8190743
There is two way to do this :
1. You can create  your class instance (which is your bean) for all your beans with <%jsp:useBean ..> by giving it relevant scope., and use then bean name(id) to invoke any method on that class.

<jsp:useBean id="yourBeanName" class="xx.yy.YourBeanClass" scope="request"/>
// give full qualified path name
// Here yourBeanName , reference of YourBeanClass will be created and destroyed for each request.

2. Other way to import that class in page directive, and create the instance in the JSP when you need that, and then call its method.
<%@ page import="xx.yy.YourBeanClass" %>
...
...
<%  //for ex.
   YourBeanClass bean  = new YourBeanClass();
   String name = bean.getName();
%>
0
 

Author Comment

by:NicolaiKrog
ID: 8191445
Hi, thanks for the comments!

I fear that I have not made my question perfectly clear, - I am new in this environment, but I am not sure that your comments actually are answers to my question.
Let me try with a concrete (but invented) example:

First we have a DatabaseBean:

class DatabaseBean {
    public ResultSet exectuteQuery(String query){
         ResultSet rs=null;
         // code that connects to a mySQL database.
         // executes the command "query" and puts
         // the result in "rs".
         return rs;
    }
}

Then we want to create an InformationBean, that also uses
the method executeQuery that is implemented in DatabaseBean, - something like this:

class InformationBean{
import DatabaseBean;
public String getUsername(int id){
  ResultSet rs=DatabaseBean.executeQuery
  ("Select username from persons where userID="+id);
  rs.next();
  return rs.getString("username");
}

This does of course NOT work,- but can you tell me how to make it work?? This is my question!

Lets just for now forget all about jsp; this problem is about how the beans can communicate without the JSP-pages!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:kennethxu
ID: 8192832
I guess you closed the connection in exectuteQuery() method. once you close your connection, ResultSet is closed too.
0
 
LVL 3

Accepted Solution

by:
allahabad earned 500 total points
ID: 8192834
class InformationBean{
dbBean DatabaseBean;
public String getUsername(int id){
  dbBean = new DatabaseBean(); // get DatabaseBean object
ResultSet rs=DatabaseBean.executeQuery(
 ("Select username from persons where userID="+id); // call the method
}
0
 
LVL 14

Expert Comment

by:kennethxu
ID: 8192835
may be you want to look into disconnected RowSet:
http://java.sun.com/j2se/1.4/docs/api/javax/sql/RowSet.html
0
 

Author Comment

by:NicolaiKrog
ID: 8194241
THANK YOU!

Yes, it was that simple! - Embarrasing......
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Check out the latest tech news, community articles, and expert highlights in August's newsletter.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question