jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Querying a Database from a session bean using JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Querying a Database from a session bean using JDBC" Watch "Querying a Database from a session bean using JDBC" New topic

Querying a Database from a session bean using JDBC

Fiona Healy

Joined: Jan 27, 2005
Posts: 21

I am developing an application that requires the querying of an external database i.e. user requests a number of fields which are stored in the applications database and these are then used to query a second database for the actual values. If anyone can understand what i am talking about, can you direct me as to whats the best approach to doing this. I am using MySQL DB and have been advised to use JDBC in a session bean?

Any ideas?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33102

Basically you have two queries. So the high level approach is:
1) Query external database (database 1)
2) Loop through result set and store fields in variables (or List if multiple rows.)
3) Build sql string for second query. I recommend using prepared statements so you can easily substitute in the values.
4) Run second query (database 2)
5) Loop through result set and process actual values

The logic behind separating the queries is so you can close the first connection sooner. Take a look at Sun's JDBC tutorial to get started with JDBC.

For the future, I recommend posting this type of question in the JDBC forum since it is more about JDBC than session beans.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to JDBC...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link: http://aspose.com/file-tools
subject: Querying a Database from a session bean using JDBC
It's not a secret anymore!