This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes How to get data from a database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get data from a database" Watch "How to get data from a database" New topic

How to get data from a database

Bill Fences

Joined: Oct 23, 2004
Posts: 9
I'm having a bad hair day or something, if some one can give me some clue,

I have servlet that is initiated at start up

I query the database and get a record set let say 500 rows 30 columns ID, Name, Number etc...

1. how do I get the column name let say getColumnName("ID"), getColumnName("Name"), etc.. I iterating through the record set..


2. I want to eventually write these to a file pipe delimeted to ftp to another app.
a. - Would it be OK to write directly print.out(getColumnName("ID"), getColumnName("Name"), etc.. )?
b. - Or should I look at storing the records in a vector and then writing the data?

3. Later on I get a response from the app I sent the file to to tell me if the records were successful or fail and then I update the status of the record in the database.

If some one can give me an idea what the best apprach to this is I will love you forever.

Gert Cuppens
Ranch Hand

Joined: Jul 13, 2003
Posts: 87
You should work according to the Model-View-Controller design pattern. In short, the MVC contains :
- model : the JavaBeans correspond to the concepts of the user of the web application, like Client, Product, Order, Delivery, Invoice
- view : the Java Server Pages take care of the presentation layer
- controller :the servlet(s) take(s) care of the application flow.

And besides these kinds of objects, you have the DAO's or Data Access Objects. These are the objects who make connection with the database and put the resultset in a JavaBean. I'll give an example :

public ArrayList findMeterrondes()
throws SQLException
StringBuffer zoekString = new StringBuffer();
zoekString.append("select * from mlzro");
Connection con = getConnection();
PreparedStatement ps = con.prepareStatement(zoekString.toString());
ResultSet rs = ps.executeQuery();
if (rs == null)
return null;
else {
ArrayList rondeLijst = new ArrayList();
Calendar vandaag = Calendar.getInstance();
int jaar = vandaag.get(Calendar.YEAR);
int maand = vandaag.get(Calendar.MONTH);
int dag = vandaag.get(Calendar.DAY_OF_MONTH);
int datumAS = (jaar * 10000) + ((maand * 100) + 1) + dag;
while ( )
Meterronde meterronde = new Meterronde(rs);
findPlanningsAantallen(meterronde, datumAS);
return rondeLijst;
} /* rs != null */

} // end findMeterrondes
I agree. Here's the link:
subject: How to get data from a database
jQuery in Action, 3rd edition