wood burning stoves 2.0*
The moose likes Servlets and the fly likes Database interaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Database interaction" Watch "Database interaction" New topic
Author

Database interaction

Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
Hi,
I am looking for a tutorial that walks the process of getting form data from JSP page, to a bean, then to a database.
I have seen lots of bits and pieces on this....but I am looking for "start to finish" example.
OR
If a best practice recommendation....i.e. use html and servlets with JSP.

Anyway, I can get the data to a bean (java class)...i believe by using setter functions for each form data item. But then how do I call a method to process the data and save it to the database? I have the database connectivity part.

A kick in the right direction would be greatly appreciated.

Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Best practice is to ensconce any DB interaction in the model layer of the application, completely divorced from any UI.

In other words, write your business layer as if it could be used from any type of application: web, command line, Swing, etc.

Perhaps this article might be helpful.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
Right...
I understand that and that is what I am working to.
What I need is an actual example (code) that does that. For example I have a JSP page that contains a form.
The form's action calls a java class. The java class contains the setters to grab the form data.
So now it is sitting still. How do I make a call to another method to send the data to a database? I have a class that handles that database and that is working fine. Just how do I get my form data there?

I think I should have a method in the class that gets the form data...but how do I call it? Or can I just capture the POST data in the method then call the database save method in my other class?

v/r
Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

I think you are thinking of it from the wrong direction.

You are focusing on pushing the form data down into the business layer. Rather, think of it from the other direction. Imagine what business operation you are trying to model and create an API that is suitable. This API should be able to accept data from any source (e.g. the fact that it comes from a form submission in a web app should be moot).

Then you'll know how to write the servlet code to call that business API.

Frequently, if a large number of data items are needed to perform an operation, rather than having a method with a bazillion parameters, the data will be abstracted into a model class. For example, let's say the business operation is to create a new user. Rather than passing the individual properties (first name, last name, address and so on) a class that models a User will be defined and passed to the operation.
Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
Bear Bibeault wrote:I think you are thinking of it from the wrong direction.

You are focusing on pushing the form data down into the business layer. Rather, think of it from the other direction. Imagine what business operation you are trying to model and create an API that is suitable. This API should be able to accept data from any source (e.g. the fact that it comes from a form submission in a web app should be moot).

Then you'll know how to write the servlet code to call that business API.

Frequently, if a large number of data items are needed to perform an operation, rather than having a method with a bazillion parameters, the data will be abstracted into a model class. For example, let's say the business operation is to create a new user. Rather than passing the individual properties (first name, last name, address and so on) a class that models a User will be defined and passed to the operation.


I like the idea....but the question remains. I have a UI with a button on it. When the user clicks the button, I want the data to go to the database. So in your model above, if I have a class that models, lets say the new user, how do I get that class linked up to the button event and then to the database. My brain is short circuiting on how to connect them.

v/r
Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

The aren't connected. That's the whole point.

The button submits a request to the server -- it doesn't know or care anything about the DB or what's going to happen with the data.

The servlets that's invoked gathers the form data from the request, packages it up in whatever form the business API wants, and invokes the business API. The servlet doesn't know or care about anything to do with the DB.

The business layer does its thing. it doesn't know or care anything about where the data came from.

Each layer is separated from the other and doesn't know or care anything about what the other layer is doing or how it does it.
Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
OK
I understand what you are saying...and this is what I want to achieve.
What I want is and example.
Can you show me (or point me to) how it is done.
That is what I want. I am learning here!

I understand the concepts...and I can even implement it in PHP...I want to learn how Java/JSP/Servlets do it.

Thank you for your patience and time!

v/r
Mike
Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
Do you mean...I can just gather the data and make the method call to the java class that handles the database from the doPost method of the servlet? No other methods are required?
Have I been over complicating this?

v/r
Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

I believe you have been over-complicating this.

The whole point of proper Separation of Concerns is that it makes things easy. Organized, well-structured code is easy. Things get complicated when they are disorganized.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Michael Eller wrote:Do you mean...I can just gather the data and make the method call to the java class that handles the database from the doPost method of the servlet?


Sure. Why not?

pseudo untested code


Lots missing from the above example like validation and exception handling. But that should give you the idea. SomeBizObject might be a service or a DAO but the gist is that it handles all the saving of your User for you. As Bear suggested:


In other words, write your business layer as if it could be used from any type of application: web, command line, Swing, etc.


SomeBizObject doesn't give a crap about your servlet or JSP. All it cares is that you gave it what it needed to persist your User.

GenRocket - Experts at Building Test Data
Michael Eller
Ranch Hand

Joined: Feb 28, 2004
Posts: 32
Well Bear...and Gregg,

I sure do appreciate the assistance.
The guidance you have provided has me on my way...probably to my next question...but we will wait and see.

Again Thanks for the information, guidance, and patience.

v/r
Mike
 
GeeCON Prague 2014
 
subject: Database interaction