aspose file tools*
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
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to get data from a database" Watch "How to get data from a database" New topic
Author

How to get data from a database

Bill Fences
Greenhorn

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..

ID321,Cletus,23..
ID123,CousinDaisy,23..
ID144,BossHog,23..
ID113,Flash,23..
etc...

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.

Bill
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)
{
ps.close();
con.close();
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 (rs.next() )
{
Meterronde meterronde = new Meterronde(rs);
findPlanningsAantallen(meterronde, datumAS);
rondeLijst.add(meterronde);
}
rs.close();
ps.close();
con.close();
return rondeLijst;
} /* rs != null */

} // end findMeterrondes
 
wood burning stoves
 
subject: How to get data from a database