JSP - display single record from database

Posted on 2005-05-17
Last Modified: 2010-07-27
I have a jsp which needs to be populated with values from a query in an Access database. The query only returns one row.
My jsp consists of text boxes which need to be filled with the fields in the query.

How is this done?  
Question by:rwallacej
    LVL 29

    Expert Comment

    ResultSet rs = //from your Access db
    <% while ( ){//move resultset onto the next row %>
    A line of db stuff <%= rs.getString("id") %><!-- get by column name -->
    A line of db stuff <%= rs.getString(2) %><!-- get by column index (start from 1)-->
    <% } %>
    When you get a resultset, you need to call as a query returns a resultset witht cursor positioned before the first row initially. This method works for single or multiple rows.

    Author Comment

    how do I get a ResultSet?  Please give an example , thanks
    LVL 29

    Accepted Solution

    Somehting like this:

          String      place;
          Connection dbconn;
          ResultSet results;
          PreparedStatement sql;
                      int      latitude,longitude,easting,northing;
                      boolean      doneheading = false;
                      dbconn = DriverManager.getConnection("jdbc:mysql://","demo","");
                      place = request.getParameter("place");
                      sql = dbconn.prepareStatement("SELECT * FROM gazetteer WHERE feature = '" + place + "'");
                      results = sql.executeQuery();

    Except you need to use the driver for Acesss having set up your datasource as an ODBC datasource.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    The first time you look at a web page and its source code, you are probably a little intimidated by the use of symbols and jargon that really looks foreign to you. You might not even know where to start to begin learning what it all means. That’…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now