File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes jsp and jdbc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp and jdbc" Watch "jsp and jdbc" New topic

jsp and jdbc

viral patel

Joined: Feb 25, 2003
Posts: 16
can i excute multiple query in jsp page to fill database. If i can, can you give me example of it.
Andy Bowes
Ranch Hand

Joined: Jan 14, 2003
Posts: 171
In short there are many ways of executing SQL queries against the DB from the JSPs however in short there should be NO direct link between the database layer and the presentation tier.
Separate your application out into 3 distinct layers Presentation, Control/Business, and Persistence ( more commonly known as Model View Controller orMVC). Only put code that accesses the database in the Persistence layer. The Control layer is responsible for receiving the requests from the Presentation layer i.e. the JSPs and then calling the appropriate objects/methods in the Persistence tier.
This separation of concerns makes the application much more maintainable i.e. you can change the DB structure without needing to rewrite the all of the JSPs in the application.
In EJB terms the Presentation tier will be JSPs, the Control/Business tier will be a combination of servlets and Session Beans and the Persistence layer will be Entity Beans. This same tiered approach can also be acheived perfectly well without EJBs by using the Data Access Object patterns to encapsulate the interactions with the DB

Andy Bowes<br />SCJP, SCWCD<br />I like deadlines, I love the whoosing noise they make as they go flying past - Douglas Adams
Peter Daly

Joined: Jan 04, 2002
Posts: 17
While Andy is right that you really should not directly access a database from JSP's from an architecture, it doesn't mean it can't be done.
A JSP can execute java code just like a normal java object, and more than one sql update can be run the same way it can me done in Java.
get a connection and statement object then:
stmt.executeUpdate("INSERT SQL1...");
stmt.executeUpdate("INSERT SQL2...");
stmt.executeUpdate("INSERT SQL3...");
Computer Books
[ March 13, 2003: Message edited by: Peter Daly ]
I agree. Here's the link:
subject: jsp and jdbc
It's not a secret anymore!