Why Is It Bad To Put JDBC in a JSP?

Folks,

I know putting JDBC calls in a JSP is bad. I'm confronting some other developers who don't see the evils in it and I've never really had to convince anyone of this.

Aside from the obvious (exceptions don't get properly logged), can you all help me compile a list of all the reasons why putting (ultimately) DriverManager connections in a JSP is bad?

thank you,

Ty

[405 byte] By [TyroneHeda] at [2007-11-26 12:45:30]
# 1

Do one thing at a time. What I mean to say is, when I want to display I display. When I need to fetch I fetch. i.e., separating the Presentation from other layers. In J2EE it is equally important to have the system separate and this makes it easier for us to maintain it in the future.

See Stuts Implementation for more clarity on the same.

reflex2javaa at 2007-7-7 16:24:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

You are tightly coupling your data storage to display, and vice-versa.

Want to change how the data is displayed? Better know SQL and slog through all the database stuff so you know where all the values have to go. Better hope that by some accident with how you change your display you don't screw your db code, or leave db connections open (which can happen if some other error on the JSP occurs before the db connection closes).

Want to modify how your data is stored or how the tables are organized/retrieved? Your ODBC guys better know JSP/JSTL and be able to wade around the display to get to the sql code they really want to work on. In the meantime, hope they don't add extra output or empty lines to the JSP.

By including SQL in JSP you make it hard to change any one layer of the application independently of all the other layers. Why should your db guys need to know JSP? Why should your JSP guys need to know SQL? Better let the specialists do their own thing.

The SQL is done in a base, all java environment, and provides Data Transfer Objects that the JSP guys can see and use to get the data they need. The JSP then doesn't care about how the data is stored - it just cares about the DTOs. So you can completely change the data storage with 0 effect on the display. Similarly, you can use the same data access and DTOs to display on a Swing application or Java Console as well.

stevejlukea at 2007-7-7 16:24:22 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...