• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP returning old data, database was changed correctly!

 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am updating an existing record on a database using the HTML entry form and the changes that i submit do in fact update the ACCESS database fine, but the .jsp page is picking up the previous data, as if it's reading the database before the actual update, see "problem area" highlighted.

So i submit the HTML, changes are made, but it comes back with the old data, but when i do a manual REFRESH, the correct data comes back! so is the jsp:forward getting the data from a stored buffer in ACCESS possibly?
Thanks very much for any insights!

[/I]

<jsp:useBean id="empInfo"
<% empInfo.updateDatabase(); %> >>>>>>>3b.

<jsp:forward page="PresentChangeOfEmployeeData.jsp"/> >>>>>>>4.
---------------------------------------------------------------------------------
3. com.ora.jsp.beans.employee23.EmployeeInfoBean - Bean-update DB with change data
---------------------------------------------------------------------------------

public class EmployeeInfoBean {
public void updateDatabase(){ <------3b.
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn =
DriverManager.getConnection("jdbcdbc:example");
String sql = "UPDATE EMPLOYEEINFO SET " +
"NAME=?, ADDRESS=?, PHONE=? WHERE ID=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, address);
statement.setString(3, phone);
statement.setInt(4, id);
statement.executeQuery();
statement.close();
conn.close();
}
---------------------------------------------------------------------------------
4. PresentChangeOfEmployeeData.jsp - read database and display changes <-------4.
---------------------------------------------------------------------------------

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn =
DriverManager.getConnection("jdbcdbc:example");
Statement statement = conn.createStatement();
String sql = "SELECT * FROM EMPLOYEEINFO WHERE ID = " + employeeID;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
%>
<TR><TD ALIGN="right" WIDTH="50%">Name:</TD>
<TD WIDTH="50%"><%= rs.getString("NAME") %></TD>

==============================================================================
all code listed:
==============================================================================

------------------------------------------------------------------------------
1, InputEmployeeInfo.html
------------------------------------------------------------------------------
<HTML>
<HEAD><TITLE>Change of Information</TITLE></HEAD>
<BODY>
<TABLE WIDTH="100%" BORDER="0" BGCOLOR="navy">
<TR ALIGN="center">
<TD><FONT SIZE="7" COLOR="yellow">Employee Information</FONT></TD>
</TR>
</TABLE>
<CENTER>
<FONT SIZE="5" COLOR="navy">
Please Enter Your Information<BR>
Fill in all fields
</FONT>
</CENTER>
<TABLE WIDTH="100%">
<FORM NAME="updateInfo"
ACTION="./UpdateEmployeeInfo.jsp" <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 2.
METHOD="POST">
<TR><TD WIDTH="40%" ALIGN="right">Current ID: </TD>
<TD WIDTH="60%"><INPUT TYPE="text" NAME="id"></TD>
</TR>
<TR><TD WIDTH="40%" ALIGN="right">New Name: </TD>
<TD WIDTH="60%"><INPUT TYPE="text" NAME="name" VALUE="Mickey"></TD>
</TR>
<TR><TD WIDTH="40%" ALIGN="right">New Address: </TD>
<TD WIDTH="60%">
<INPUT TYPE="text" NAME="address" VALUE="St. Louis, MO">
</TD>
</TR>
<TR><TD WIDTH="40%" ALIGN="right">New Phone: </TD>
<TD WIDTH="60%">
<INPUT TYPE="text" NAME="phone" VALUE="555-555-1234">
</TD>
</TR>
<TR><TD COLSPAN="2" ALIGN="center">
<INPUT TYPE="submit" NAME="btnSubmit" VALUE="Update Profile"></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML
--------------------------------------------------------------------------------
2. UpdateEmployeeInfo.jsp:
--------------------------------------------------------------------------------
<HTML>
<HEAD><TITLE>Updating Employee Information</TITLE></HEAD>
<BODY>
<jsp:useBean id="empInfo"
class="com.ora.jsp.beans.employee23.EmployeeInfoBean" <<<<<<<<<<<<<<<3a.
scope="request"/>

<jsp:setProperty name="empInfo" property="*" />

<% empInfo.updateDatabase(); %> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<3b.

<jsp:forward page="PresentChangeOfEmployeeData.jsp" /> <<<<<<<<<<<<<<<<<<<<<<<<<4.
</BODY>
</HTML>
---------------------------------------------------------------------------------
3. com.ora.jsp.beans.employee23.EmployeeInfoBean <--------------------------------3a
---------------------------------------------------------------------------------
package com.ora.jsp.beans.employee23;

import java.sql.*;

public class EmployeeInfoBean {
private String name, address, phone;
private int id;

public void setName(String input){
name = input;
}
public String getName(){
return name;
}
public void setAddress(String input){
address = input;
}
public String getAddress(){
return address;
}
public void setPhone(String input){
phone = input;
}
public String getPhone(){
return phone;
}
public void setId(int input){
id = input;
}
public int getId(){
return id;
}
public void updateDatabase(){ <-----------------------------------------------3b.
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn =
DriverManager.getConnection("jdbcdbc:example");
String sql = "UPDATE EMPLOYEEINFO SET " +
"NAME=?, ADDRESS=?, PHONE=? WHERE ID=?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, address);
statement.setString(3, phone);
statement.setInt(4, id);
statement.executeQuery();
statement.close();
conn.close();
}
catch (Exception e) {}
}
}
---------------------------------------------------------------------------------
4. PresentChangeOfEmployeeData.jsp
---------------------------------------------------------------------------------
<HTML>
<HEAD><TITLE></TITLE></HEAD>
<BODY>
<%@ include file="CompanyBanner.html"%>
<%@ page import="java.sql.*" %>
<jsp:useBean id="empInfo"
class="com.ora.jsp.beans.employee23.EmployeeInfoBean"
scope="request"/>
<CENTER>
<FONT SIZE="5" COLOR="navy">
Your New Information
</FONT>
</CENTER>
<TABLE WIDTH="100%" BORDER="1">
<%
int employeeID = empInfo.getId();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection conn =
DriverManager.getConnection("jdbcdbc:example");
Statement statement = conn.createStatement();
String sql = "SELECT * FROM EMPLOYEEINFO WHERE ID = " + employeeID;
ResultSet rs = statement.executeQuery(sql);
while(rs.next()){
%>
<TR><TD ALIGN="right" WIDTH="50%">Name:</TD>
<TD WIDTH="50%"><%= rs.getString("NAME") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Address:</TD>
<TD WIDTH="50%"><%= rs.getString("ADDRESS") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Phone Number:</TD>
<TD WIDTH="50%"><%= rs.getString("PHONE") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Work Status:</TD>
<TD WIDTH="50%"><%= rs.getString("WORKSTATUS") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Total Sick Days:</TD>
<TD> <%= rs.getString("TOTALSICKDAYS") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Taken Sick Days: </TD>
<TD><%= rs.getString("TAKENSICKDAYS") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Total Personal Time(in hours): </TD>
<TD><%= rs.getString("TOTALPERSONALTIME") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Taken Personal Time(in hours): </TD>
<TD><%= rs.getString("TAKENPERSONALTIME") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Health Care Plan:</TD>
<TD WIDTH="50%"><%= rs.getString("HEALTHCAREPLAN") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Dental Plan:</TD>
<TD WIDTH="50%"><%= rs.getString("DENTALPLAN") %></TD>
</TR>
<TR><TD ALIGN="right" WIDTH="50%">Vision Plan:</TD>
<TD WIDTH="50%"><%= rs.getString("VISIONPLAN") %></TD>
</TR>
<% }//end while loop
} // end try block
catch (Exception e) {};
%>
</TABLE>
<%@ include file="ch24_SiteNavigator.html" %>
</BODY>
</HTML>

[/CODE]
 
Stefan Evans
Bartender
Posts: 1745
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See the JSP FAQ on how to prevent your jsp being cached.
 
bob connolly
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well i tried the following and still getting that problem, might you have any other ideas?

 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob,
Javaranch tip:

If you are going to post more than a line or two of your code, wrap that
code in a set of UBB Code tags.
Doing so will help to preserve your code's indenting, making it easier to read.
If it is easier to read, more people will actaully read it and you will
stand a better chance of getting help with your question.
See UseCodeTags for more
help with UBB code tags.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic