Loading the drivers

Hello everybody,

I am creating an application. I know that for database purpose I have to load the drivers, create connection object and make the Statements and PreparedStatement object. But I don't have to load the driver in every page where database access is required. Or I hope I don't have to create Connection Object in every page too, So what should I do for the application to be developed in an efficient manner. Please help me. Please advice me.

Thanking you a lots.

[497 byte] By [Zorama] at [2007-11-27 10:55:51]
# 1

> I am creating an application. I know that for

> database purpose I have to load the drivers, create

> connection object

Not always. Sometimes, the app server does it for you.

> and make the Statements and

> PreparedStatement object. But I don't have to load

> the driver in every page where database access is

> required.

Page?

> Or I hope I don't have to create Connection

> Object in every page too, So what should I do for the

> application to be developed in an efficient manner.

Page?

JSPs should never access any databases, in whatever way.

And connection handling should be done by the container.

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 2

Zoram,

Which application server are you using? They all support connection pooling... and connecting to the database directly from any web-page is a poor design. There's atleast an EJB layer between them... bone up on EJB's.

If it's worth doing, it's worth doing right. The first time.

Keith.

corlettka at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 3

But in my application I am using JSPs and Servlets only....

Zorama at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 4

JSPs should never access any databases, in whatever way.

Could you please iterate any dangers involved if a jsp accesses any database. 'Cause I have one jsp that invokes a connection to a database and fetches few records.

Is it not a good programming practice or does it hamper performance of the application?

It would be of a great help if you can explain for a java minnow like me,

passion_for_javaa at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 5

> But in my application I am using JSPs and Servlets

> only....

So you got the pure presentation layer there (V and C of MVC). How about a business logic and persistency layer, too?

Any scriptlet in a JSP is already a sign that the JSP is doing too much work.

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 6

> Could you please iterate any dangers involved if a

> jsp accesses any database.

The danger is an architectural one, because it closely ties your implementation of persistency to your user interface. Re-implement your application as a Swing stand-alone app and you're totally hosed because you have to write all of it again.

The only thing a JSP is to be concerned about is presenting the data it is given to the user.

> Is it not a good programming practice or does it

Not at all. It's actually what everyone everywhere teaches *not* to do, but to keep a clear separation of the layers.

> hamper performance of the application?

No.

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 7

Is it a good idea, if i create a jsp page say dbConfig.jsp and write the code for driver loading and creating Connection object there, and include this file in every page where database access is required using the <%@ include file="dbConfig.jsp" %>

contents of the dbConfig.jsp:

********************************************************************************

<%@ page import="java.sql.*" %>

<%

String connectionURL = "jdbc:mysql://localhost/healthac_hpi?user=healthac_healtha&password=claude01";

Connection connection = null;

Class.forName("com.mysql.jdbc.Driver").newInstance();

connection = DriverManager.getConnection(connectionURL);

%>

********************************************************************************

Or do I have a better option to adopt here. If yes, please give me a suggestion. I am just a beginner. Please help me.

Zorama at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 8

> Is it not a good programming practice or does it

> hamper performance of the application?

>

> It would be of a great help if you can explain for a

> java minnow like me,

Almost All Java Web Apps Need Model 2

http://www.fawcette.com/javapro/2002_06/online/servlets_06_11_02/

Understanding JavaServer Pages Model 2 architecture

http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

Web-Tier Application Framework Design

http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e/web-tier/web-tier5.html

~

yawmarka at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 9

> Is it a good idea, if i create a jsp page say

> dbConfig.jsp and write the code for driver loading

No.

Is there anything in the previous posts you didn't understand? About JSPs even having to avoid scriptlet code and not accessing databases, and about database connections being handled by the application server?

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 10

Yes Sir,

I am yet to understand where should I write the code for the loading of the driver.

Please help me

Zorama at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 11

> I am yet to understand where should I write the code

> for the loading of the driver.

Nowhere. The app server is responsible for that. Hence Keith's question to the beginning which one you're using.

And even if you'd have to, it'd be in just another Java class somewhere in your non-existant persistency layer of your application. Servlet and especially JSP shouldn't even be aware that a thing like a database exists.

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 12

Sir, I am using Tomcat 5.5

Do I need to write the driver loading code in a java bean and made it available to every page. Is it a good practice?

Zorama at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 13

Please Help!

Zorama at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 14

> Do I need to write the driver loading code in a java

> bean and made it available to every page. Is it a

> good practice?

It's hopeless, isn't it?

And read this, it might help:

http://www.developer.com/db/article.php/10920_2172891_3

CeciNEstPasUnProgrammeura at 2007-7-29 11:59:10 > top of Java-index,Java Essentials,Java Programming...
# 15

Hello Sir,

Do you mean here I need JNDI concept for the application..?

I am just a beginner, Can I still do it?

Zorama at 2007-7-29 11:59:14 > top of Java-index,Java Essentials,Java Programming...
# 16

Thank you sir,

Your suggestion was of great help...

Zorama at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...
# 17

You're not going to get a lot of 'friendly' advice about connecting to a database from within JSPs. I'm still kind of new myself, but I'm getting the impression that, perhaps culturally, java tends to be associated with feature-rich, heavily 'designed' applications... as opposed to a 'just get it done, and I'll do it over again if I have to' mindset. JSPs are really supposed to be a presentation-layer device for a larger application... not the application itself.

I have no idea what kind of load you're expecting, what kind of performance you need, and what your overall design goals are, but if you're looking for a thin technology that can be implemented quickly and works more along the lines that you appear to be thinking, you might try PHP instead of JSP. I'm not a guru in either technology, but from what I understand, people tend to open database connections in PHP on a per-request basis without much mishap. In java, that practice is frowned upon for some pretty good reasons.

Regardless of the technology you use, if you build your application this way, it will be less extensible/maintainable/scalable. It might be faster for you to implement, though... especially if you are inexperienced.

dcaudella at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...
# 18

> You're not going to get a lot of 'friendly' advice

> about connecting to a database from within JSPs. I'm

> still kind of new myself, but I'm getting the

> impression that, perhaps culturally, java tends to be

> associated with feature-rich, heavily 'designed'

> applications... as opposed to a 'just get it done,

> and I'll do it over again if I have to' mindset.

Not at all! Particularly in teams using agile methods, the "get it done" approach is heftily encouraged. But the "get it done" approach is not a "just hack any old rubbish together" approach, there is little more effort in separating your presentation and data access logic up-front than there is throwing the whole shebang together in a scriptlet. Good OO principles should apply, always, but this is not anathema to a quick and simple approach

> JSPs

> are really supposed to be a presentation-layer device

> for a larger application... not the application

> itself.

Quite

> I have no idea what kind of load you're expecting,

> what kind of performance you need, and what your

> overall design goals are, but if you're looking for a

> thin technology that can be implemented quickly and

> works more along the lines that you appear to be

> thinking, you might try PHP instead of JSP. I'm not

> a guru in either technology, but from what I

> understand, people tend to open database connections

> in PHP on a per-request basis without much mishap. In

> java, that practice is frowned upon for some pretty

> good reasons.

Reasons of maintainability. It's not "being purist", as seems to be the impression a lot of people get

> Regardless of the technology you use, if you build

> your application this way, it will be less

> extensible/maintainable/scalable. It might be faster

> for you to implement, though... especially if you are

> inexperienced.

I say carry on and make these mistakes. No amount of "knowing" a good practice, or being told about them, is a substitute for finding out for yourself. Nothing wrong with deliberately doing the wrong thing - when it doesn't matter, of course! - simply in order to prove to yourself that it's the wrong thing :-)

georgemca at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...
# 19

Thought I'd catch some flak for that one ;)

I'll probably catch some more here, but...

Java is inherently complicated, not because it is complicated to do things in it, but because it has an army of layers of technology that make it easy to do things if you know what you are doing, and difficult if you are new and trying to figure out what everything is for.

Just figuring out which java technologies to (not)use in your application implies some amount of design work and research. Each one has an intended use, and consequences for deviating from that intended use.

If he were just backing up his site with a suite of scripts, he wouldn't have quite the same problem. He'd use some scripts to build pages, and other scripts to access the database. And that would be all he'd need to know. He could still screw it up, by, taking it to the extreme, building his entire website out of one script... I guess this is the equivalent of building his entire website out of one jsp.

Here's the point: JSPs work in a similar way to PHP and a bunch of other scripting technologies. However, in the other scripting technologies, handling database within that scripting language is standard practice. Database access from a JSP is basically a design error, because java has other tools to do that. You need to learn these other tools too (not to mention oop) if you're going to use java. This is what I mean by 'designed' applications.

His mistake implies more than he is making a design error; it implies he may be using the wrong technology. He would not necessarily be making a mistake by asking basically the same question about driver persistence & database connections, but in regards to another language.

I can't emphasize this point enough: His original question escalates to a design issue specifically because of his use of JSPs and the JSP's role within Java itself.

dcaudella at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...
# 20

Yes, You are right.

Thanx for giving me some valuable suggestions.

I am relatively new to the area. So am trying to explore it.

Zorama at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...
# 21

I'm going to try to sum up what others have said and synthesize it down to a few statements.

1. JSP(s) should NEVER access the database directly. The JSP is the view layer of the applciation and should have no knowledge of the persistance of information. (I dont' entirely agree that there should be no scriptlet code in jsp(s) but it's a fair thing to try for).

2. The servlet (being the control layer) is a traffic cop. His job is to route traffic. As such he does not care nor should he have any knowledge of how the bus came into being, only where it came from and where it is going.

3. The model layer is where the business is done. This can take a number of forms or a combination of java classes, beans, ejbs, web services or any number of other possibilities. This is where the database comes into play.

4. In the model layer PuckStopper's rules of safe development #1 is DO NOT PASS CONNECTIONS TO EXTERNAL RESOURCES (databases for example) BETWEEN METHODS, CLASSES, OBJECTS, SERVICES OR ANY OTHER RESOURCE. *There is an exception to this rule which we'll talk about in a moment* If you create (initialize) a connection in a method, you should close it before you leave that method and the connection should not be given to any other method to use.

In a java web application context, database connections are cheap to acquire especially if you are using a connection pool. Leaving them open or causing them to become orphaned is VERY VERY expensive. Thus the logic of rule #1.

The one exception to this rule is a situation where you are managing a transaction across multiple database calls. In this case, create the connection at the top most level pass it down into calls making associated database operations and close at the same level you created it after the remaining work has been done. Good exception handling is a critical element in this scenario.

Just my 2 krupplenicks on the subject, your milage may of course vary.

PS.

puckstopper31a at 2007-7-29 11:59:15 > top of Java-index,Java Essentials,Java Programming...