2

I know that we can retrieve text from a text box or text area and then insert the data into a table. How can we do the opposite? That is, how to place all the data back into specific text fields or areas from a database based on some condition?


Update:

I have to do a mini-project. Its a HR Information System project. I must be able to update the details of an employee based on his ID. The way I have designed and envisioned it so far is as follows: There is a dropdown list of IDs. I select one and click OK using a form handler. It then forwards to a servlet that displays the form that I had made while adding an employee. Only, instead of being blank, these text fields consist of the data that I had inserted when adding the abovementioned employee. So, now, how do I extract these column values and put it back into text fields. I have experimented with setting the field values as column attribute names, but all that gets displayed is the name, and not the value. For example, when I set value=firstname (as specified in my database), the data in the textfield is "firstname" and not what the employee's first name actually is. Maybe I'm going about it the wrong way. Can someone please tell me exactly how to retrieve these values?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Akshay
  • 21
  • 1
  • 1
  • 2
  • You set the value attributes of the input elements in the html you generate with data queried from the database. For a more specific answer you might need to further refine your question. – Ioan Alexandru Cucu Nov 23 '10 at 07:47
  • Maybe your question should be more specific, saying what technologies you use, and what is the precise point that you don't understand. – KLE Nov 23 '10 at 07:57
  • 2
    As an alternative, you could also follow a tutorial for the precise technologies you are using, you will probably understand how it is done. – KLE Nov 23 '10 at 07:58
  • You've now two unregistered accounts. Please go back to the same PC/browser which you used to post the question and register the account. This way you can use the same account from every PC/browser. – BalusC Nov 24 '10 at 11:24

3 Answers3

1

Use servlet's doGet() method to preprocess the request (you should call the URL of this servlet in browser).

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Employee employee = getItSomehow();
    request.setAttribute("employee", employee);
    request.getRequestDispatcher("/WEB-INF/edit.jsp").forward(request, response);
}

Use JSP EL to display it in HTML input field's value attribute.

<input name="firstname" value="${employee.firstname}" />

However, this sets doors open to XSS attacks. Use JSTL fn:escapeXml() to prevent it.

<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...
<input name="firstname" value="${fn:escapeXml(employee.firstname)}" />

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

It seems like BalusC has a good answer for it but I wanted to elaborate a little bit more on the getItSomehow() method. Since you have not specified what database you are using I will give you an example of how to do it if you were using mysql.

[Step 1]: A simple google search will tell you how to get the correct jdbc

    try{
        Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e){
        throw new AssertionError(e);
    }

[Step 2]: Now you will get a connection to the db and get a statement ready

        String url = "jdbc:mysql:///"+database_name;
        Connection con = DriverManager.getConnection(url);

        Statement stmnt = con.createStatement();

[Step 3]: Finally you will execute your select statement

        ResultSet rs = stmnt.executeQuery(
             "SELECT "+ "values you want to retrieve separated by commas"
           +" FROM "+ "table name"
           +" WHERE "+ "condition eg. id=123";

Now that you have a set of results int he ResultSet object you can parse it to get the data you requested. You could use the following:

        if(rs.next()){
               employee.setXXXX(rs.getString("XXXX"));
               employee.setYYYY(rs.getString("YYYY"));
         }
0

You can retreive data in JSP in this way:

  1. retreive data in bean object
  2. set bean object in request object
  3. dispatch your request to the JSP

Follow this code:

<%
    forumbean bean=(forumbean)request.getAttribute("userdata");
    String name=bean.getName();
    out.print("<input type='text' id='name' name='name' value='"+name+"'>");
%>

You can use expression language also to avoid scriplet tag.

bluish
  • 26,356
  • 27
  • 122
  • 180
coder25
  • 2,363
  • 12
  • 57
  • 104