0

I am interested in getting out the value from the DATABASE and get it into textbox value property, here is my code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Your Data</title>
</head>
<body>
    <%! ResultSet rs; %>
    <%! Connection con;%>
    <%! PreparedStatement pstmt;%>
    <%! String sql = "SELECT * FROM USERS WHERE USERNAME = ?"; %>
    **<%! String unm,pwd; %>**
<h3>Update Your Details</h3>
<hr/>

<form action="UpdateSrvlt" method="post">
    <%
        try{
        Class.forName("com.mysql.jdbc.Driver");
        }catch(ClassNotFoundException cnfe){
            System.out.println("Error :"+cnfe.getMessage());
        }
        try{
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","");
        pstmt = con.prepareStatement(sql);
        pstmt.setString(1,request.getParameter("username"));
        rs = pstmt.executeQuery();
        while(rs.next()){
            **unm = rs.getString("username");**
            System.out.println("Username at Update.jsp: "+unm);
            **pwd = rs.getString("password");**
            System.out.println("Password at Update.jsp: "+pwd);

        }
        }catch(SQLException se){
            System.out.println("Error :"+ se.getMessage());
        }       
    %>
    <table>
        <tr>
            <td align="left">
                Username :
            </td>
            <td align="left">

                <input type="text" name="username" value="${unm}"/>
            </td>
        </tr>
        <tr>

            <td align="left">
                Password :
            </td>
            <td>
                <input type="text" name="password" value="${pwd}"  />
            </td>
        </tr>

        <tr>
            <td><input type="submit" value="Update" /></td>
        </tr>
        <%
            pstmt.close();
            con.close();
        %>
    </table>
    </form>
</body>
</html>

Anybody know the reason what is the reason for getting null..inspite of getting variable value.

Zealous
  • 33
  • 1
  • 1
  • 7

2 Answers2

0

Try this:

<body>
<%! 
    String unm="", pwd="";
%>
<form>
<%
    //your code to get userName and password here
    pageContext.setAttribute("unm", unm);
    pageContext.setAttribute("pwd", pwd);
%>
<input type="text" name="username" value="${unm}"/>
<input type="text" name="password" value="${pwd}"/>
</form>
</body>
Bhushan
  • 6,151
  • 13
  • 58
  • 91
  • I am getting null values..in these two textboxes. via any of the method. Earlier I have tried out the second one and I got the null printed in the textboxes. – Zealous Feb 03 '14 at 15:03
  • @Zealous Please have a look at update, I have tested it, and its working. – Bhushan Feb 04 '14 at 04:27
0
<input type="text" name="username" value="<%=unm%>"/>
<input type="text" name="password" value="<%=pwd%>"/>
Saurabh Kachhia
  • 300
  • 3
  • 12