newbie: making data object to be displayed and printed

From: Johan (johankees*AT*_at_ilse*DOT*.nl)
Date: 12/25/03

  • Next message: Silvio Bierman: "Re: Servlet Threads Database Connections"
    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


  • Next message: Silvio Bierman: "Re: Servlet Threads Database Connections"

    Relevant Pages

    • RE: Userform to add and update a table
      ... One of the most common scenarios in application development is to display ... the walkthrough illustrates many of the basic ... Access to a server with the Pubs SQL Server sample database. ... Creating the Windows Form. ...
      (microsoft.public.sqlserver.programming)
    • Re: Selecting from Datasets
      ... SQL provider manage Connection pooling which mean whatever sqlConnection ... instance you call it's just one virtual connection with the database managed ... > If I then have a datagrid and I only want it to display Field 1 and field ... > is no longer displayed on the page, so there shouldn't be a memory ...
      (microsoft.public.dotnet.languages.csharp)
    • Re: Picture property for Access 2K forms
      ... and it works great...until you look at the database size involved. ... or by simply scaling down the image display in the ... > supported by those filters. ... To associate a graphic file with an OLE Server, ...
      (microsoft.public.access.devtoolkits)
    • Re: Difference between storing files on folder and in mysql db
      ... a separate instance of an image display script (and a separate ... connection to the DB server) to display each image. ... connection if images were stored in the file system. ... image comes from the database or the file system. ...
      (comp.lang.php)
    • Re: Unknown Maiden Name [WAS how-to ruin a perfectly good FGS]
      ... through at the beginning of my database, ... or display your information in the fields you intended for them. ... the names in a manner you didn't intend and the meaning has become ... If my data is on the Internet and people don't understand it, ...
      (soc.genealogy.methods)