?
Solved

JSP Insert Bean and multiple tables

Posted on 2003-02-25
4
Medium Priority
?
496 Views
Last Modified: 2010-04-01
Hi,

first of all sorry my english!
I'm new to JSP and especially Javabean issues but i know something about Java.
I need to build a form (html) where i insert values in two tables. Imagine my tables like this:

TABLE_A           TABLE_B
Field_ID_A        Field_ID_B
Field_integer     Field_ID_A (FK)

I know that i should do this using a Javabean instead of wrting <% %> tags inside HTML. Why? Because it's more business logic!

I'm building a JSP to do this. Two questions for you: Can i create a JavaBean that insert records in any table (saving time in future jsp's)?

Can i insert in more than one table within the JavaBean like i want to do?

Best regards,
Bruno Martins
0
Comment
Question by:bmartins
[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
4 Comments
 
LVL 14

Expert Comment

by:kennethxu
ID: 8019443
1. JavaBean is a Java Class that follows certain naming convention. so whatever a Java Class can do, JavaBean can do.
2. in jsp, you can access any java object, you are not just limited to the beans.

>> Can i create a JavaBean that insert records in any table (saving time in future jsp's)?
theoretically yes. but practically no because the column count and data type is different.

>> Can i insert in more than one table within the JavaBean like i want to do?
of course, you can, just as you do in any regular java class.
0
 

Expert Comment

by:Brcko
ID: 8024036
you can try this

Build bean class by table b

public class MyBeanB {
  int id;
  int parentId;

  public MyBeanB () {
  }
 
  public void setId(int id) {
    this.id = id;
  }
  public int getId() {
    return this.id;
  }

  // same for parentId, alert for naming conventions for Beans
}

Build bean class by table a

public class MyBeanA {
  int id;
  int field;
  ArrayList childs; //this is list of childs of type MyBeanB

  public MyBeanA () {
  }
 
  public void setId(int id) {
    this.id = id;
  }
  public int getId() {
    return this.id;
  }

  // same for parentId, alert for naming conventions for Beans

  public MyBeanB getChild(int index) {
    return (MyBeanB) childs.get(index);
  }

  and so on...
}

BTW:
it's (by me) not good idea insert database operations in bean
better is write special object (in fact, this object will be a part of bussines logic, bean is mostly used for describing format of data and sending them)

public class DBHandler {
  public MyBeanA getObjectA(int id) {
    select from table a where Field_id_a = id
    MyBeanA bean = // create from db record
    select from table b where Field_id_a = id
    while records {
      MyBeanB beanb = // create from db record
      bean.addChild(beanb);
    }
  }
}

P.S> it's writed in "ultra" pseudo code, but maybe it helps you
0
 

Author Comment

by:bmartins
ID: 8024491
Hi,

kennethxu first of all thanks for your comments.
Brcko your object DBHandler. Is it available from a JSP? Or is not necessary?
0
 

Accepted Solution

by:
Brcko earned 300 total points
ID: 8026612
no, it's not necessary,
but if you make the methods of DBHandler static, you must not create object DBHandler

also you can create Singleton class for DBConnection handling (as you can see in book from Marty Hall, don't know, how is original name, have only translated version)

then in JSP you can use something like ...

MyBean bean = DBHandler.getObjectA(request.getParameter("object_id"));

and after this normal use of
<jsp:g(s)etProperty name="bean" property="id"/>

One note:
if you don't user servlets || struts || forwarding || ..
and you want create only simple page to display, add, delete, edit this object, you don't need to use beans
0

Featured Post

Independent Software Vendors: 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

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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…

752 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