• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Database interaction

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Michael Eller
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Michael Eller
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic