bmartins
asked on
JSP Insert Bean and multiple tables
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
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
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
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
ASKER
Hi,
kennethxu first of all thanks for your comments.
Brcko your object DBHandler. Is it available from a JSP? Or is not necessary?
kennethxu first of all thanks for your comments.
Brcko your object DBHandler. Is it available from a JSP? Or is not necessary?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.