newbie: making data object to be displayed and printed
From: Johan (johankees*AT*_at_ilse*DOT*.nl)
Date: 12/25/03
- Previous message: killerTomato: "Servlet Threads Database Connections"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 25 Dec 2003 13:55:58 +0100
Hi,
I'm working on an program that should select data from an database and
display it on the screen (console). I also want to be able to print the
data.
I can get the data from the database and make a string object to be
displayed on the screen. The problem is that I can't reuse the same
method for printing, so I figured that I'm not doing this the right way.
Here's what the program already 'does':
Load the JDBC driver
Make a Connection
Retrieve Records
Display Records --> only as an String
Catch Exceptions
Close Connection
Here's a part of the code:
try {
// check if the driver is loaded
if (!con.isLoaded()) {
// if not connect to the database and load the driver
con.connect();
}
// prepare statement
con.statement = (Statement) con.connection.createStatement();
ResultSet results = con.statement.executeQuery(read.setQuery());
// retrieve data from the database
while (results.next()) {
int id = results.getInt("id");
Data date = results.getDate("date");
float km = results.getFloat("km");
float time = results.getFloat("time");
float tiz = results.getFloat("tiz");
int avghartrate = results.getInt("avghartrate");
String output = id + " | " + date + " | " + km + " | " + time + " | "
+ tiz + " | " + avghartrate;
System.out.println(output);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (NullPointerException npe) {
npe.printStackTrace();
}
So the question is: how do I make an object that can be reused for both
displaying to the screen and printing?
TIA
- Previous message: killerTomato: "Servlet Threads Database Connections"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|