File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Tomcat and the fly likes ~~ Newbie question - database ~~ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "~~ Newbie question - database ~~" Watch "~~ Newbie question - database ~~" New topic
Author

~~ Newbie question - database ~~

Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, good day, everyone here ! i'm new in struts, can i know how to connect database using struts? say mySQL, i try to find it by googles, but still can't figure out the exact answer i want ....so kindly please guide me here ..thank you very much for your time !
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

There is nothing special in Struts about connecting to a database. You do it the same way you would using any J2EE technology.

The preferred way in a web app is using a Connection Pool. The setup of a connection pool is dependent on your servlet containter (Tomcat, Resin, Webshere, Orion, etc).

For example, if using Tomcat, you would do it this way. In your server.xml file in the tomcat/conf directory, you would define a new context:



And then in your web.xml file:



And then in your Action or Data Access Object or POJO to get a connection:



And that's that. Nothing Struts specific. So the question now is, which Servlet Container are you using so we can move this discussion there.


GenRocket - Experts at Building Test Data
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, Gregg Bolinger , was impressed with your explanation, you have immediately bring me to the point, which i couldn't get information from books. thank you very much !!!

for my case, i using tomcat for development, as you mentioned using action, Data Access Object or POJO to connect database, do you mind to explain more on POJO ? and also data access object..is it a helper class ? which way is the best for connection, thank you very much for your time again ....and again
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Alvin chew:
for my case, i using tomcat for development, as you mentioned using action, Data Access Object or POJO to connect database, do you mind to explain more on POJO ? and also data access object..is it a helper class ? which way is the best for connection, thank you very much for your time again ....and again


POJO, or Plain Old Java Object, is just that. A plain java object that is agnostic to what type of application it is being used in. Basically, in your case, there would be nothing J2EE specific about any of the code. And you could take that class and use it in a desktop application or any other J2EE application.

A data access object isn't much different than a POJO except I use the term to mean an object that access a database for whatever reason. Something that should be done outside of your actions.

Both DAOs and POJOs can also be called "helper classes" if you wish. I have a lot of these in the current app I am working on.

HTH
Shankar Narayana
Ranch Hand

Joined: Jan 08, 2003
Posts: 134
Hi Gregg,

The commons DBCP doesn't work properly, the connection reuse is never done, i was trying the application with DBCP with initial connections set at 25 and when i hit the page 26th time, the page never shows up, the error cannot retrieve connection from the pool. Although my application always closes the connection after using it. I have seen a lot of people complaining about this, don't know whether they have found the solutions for it or not.

I am now using the PoolMan which is based on JDBC 2.0 and which lacks some of the methods like returning the Generated keys for which i had to synchronize the objects to get the functionality. Except this, poolman is very efficient in reusing the connections.

If you are using the Commons DBCP haven't you got this problem anytime, if so how to solve?.


"Failure is not when you fall down; its only when you fail to get up again."
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Shankar Narayana:
If you are using the Commons DBCP haven't you got this problem anytime, if so how to solve?.


I have not ran across this problem yet. I know that Connector/J has some packages for Connection Pools but I have not used them and I don't know how well they work. But they should have the functionallity you are missing by using the other DataSource.

I am going to go ahead and move this thread to the Tomcat forum since it seems to be getting specific for that app server.
[ August 15, 2004: Message edited by: Gregg Bolinger ]
Alvin chew
Ranch Hand

Joined: Jan 08, 2004
Posts: 834
hi, Gregg .. which datasource you recommend for mysql connection ? there have several like ibatis, hibernate ..and so on, is it necessary to have this rather than use raw jdbc ? thank you very much !
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

"iBatis, Hibernate and so on" are not DataSources.

iBatis is a data access framework. It depends on XML and SQL.

Hibernate is an ORM tool, bridging Objects to relational databases. It also relies on XML, but a proprietary query language (not SQL).

It is not *necessary* to use either of these API's (and there are dozens more), though many people would tell you to at least attempt to use some sort of database abstraction layer.


A DataSource is any object that implements javax.sql.DataSource, which is part of the JDBC API. So any JDBC driver provider should provide you with one. For example, com.mysql.jdbc.jdbc2.optional.MysqlDataSource for the connectorj driver package. (Don't go looking for it; if you've downloaded connectorj.jar, then you have it). You can then use the DataSource to obtain a connection. (I haven't mentioned anything about pooling yet).

Now we mention pooling: the whole point of DataSources (from my perspective) is to get one of two things: managed transactions and connection pooling.

Connection Pools, therefore (like poolman and DBCP) should also provide you with a javax.sql.DataSource. Look in the API for DBCP's org.apache.commons.dbcp.datasources package. Most classes (all?) will implement javax.sql.DataSource. But all you *really* care about is getting a connection right? So you configure the pool to use your jdbc driver's connection classes, and then use the pool's DataSource class to retrieve a connection.

As for the reported error with DBCP, I've never run across this error myself. I've used both Tomcat 4.1 and 5.0 series (which have 2 different versions of DBCP) and it's always worked.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Originally posted by Alvin chew:
hi, Gregg .. which datasource you recommend for mysql connection ? there have several like ibatis, hibernate ..and so on, is it necessary to have this rather than use raw jdbc ? thank you very much !


Yeah, what Mike said.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ~~ Newbie question - database ~~