?
Solved

JSP Insert Bean and multiple tables

Posted on 2003-02-25
4
Medium Priority
?
501 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
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

569 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