• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

~~ Newbie question - database ~~

 
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
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
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.
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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

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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?.
 
Gregg Bolinger
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

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
Posts: 834
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
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

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.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic