Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Data not updating -Why

Posted on 2003-11-20
13
Medium Priority
?
312 Views
Last Modified: 2010-04-01
I have a jsp called menu.jsp that has two buttons that is

add Location (from drop down list)
update Location(from drop down list)

If the admin wants to add a image, he chooses the location from the drop-down list, and then press add. ( a pop up pops up)
IF he likes teh update image or wordings, he chooses teh location from the drop dwon list and press updates.



The problem i am facing that it seems to me as if the bean can remember previous value.
It is like if i choose something, he doesn't get inserted or updated but the previous values gets inserted.

Pls help
0
Comment
Question by:Jasbir21
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9788153
Difficult to answer without seeing the code.  You may be missing a call or doing something in the wrong order.
0
 

Author Comment

by:Jasbir21
ID: 9788214
hi,
  this is the part of the window, if i would close the window without even entering any data, previous values get inserted:
<%@ page import="java.sql.*, com.dhal.*"%>

 
<jsp:useBean id="ad" class="com.dhal.admin" scope="session"/>
<jsp:setProperty name="ad" property="*"/>

<Html>

<Head>
<style type="text/css"> <!-- .inputimage { background-image: url((URL))} --> </style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!--Calendar Scripts -->
<script language="javascript" src="cal2.js"></script>
<script language="javascript" src="cal_conf2.js"></script>
<!--End Calendar Scripts-->

<script>
function myFunc(){
     window.opener.location.replace('menu.jsp');
     window.close();
}

</script>

<Title>

</Title>
</Head>
<Body BGColor="#FFFFF0">
<Font Size="2" Color="#FF0000" Face="Verdana">
<I> All fields marked with a red asterisk (*) must be filled in </I>
</Font>
<br>
<hr>


<form action="updateadmin.jsp" name="form1" method="post" >
<Font Size="4 Color="Black" Face="Garamond" Align="Center">

Image :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="file" name="path" class="inputimage">
</p>


Updates :&nbsp;&nbsp;&nbsp;&nbsp;

<textarea type="text" name="updates" rows="5" cols="15" wrap>
</textarea>



</p>



<input type=submit value=Update onClick="myFunc()">


</form>
<%
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;
PreparedStatement prep1,prep2,prep3;
prep1=null;
prep2=null;
String id="";
String updates="";
String path="";
ResultSet rs = null;
try
{
 
   
   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   statement=connection.createStatement();
   
   

   id=request.getParameter("id");


String query="select position from picture where position='"+request.getParameter("id")+"'";
rs=statement.executeQuery(query);

if(rs.next())
{
System.out.println("Updatea doing");
 updates=ad.getUpdates();
 path=ad.getPath();
String myed="update picture set path=?,updates=? where position='"+request.getParameter("id")+"'";
prep2=connection.prepareStatement(myed);

prep2.setString(1,path);
prep2.setString(2,updates);

prep2.executeUpdate();

System.out.println("*******");  
System.out.println("the id is ");
System.out.println(request.getParameter("id"));
System.out.println(updates);
System.out.println(path);

System.out.println("*****");
}

else{




System.out.println("Update cannot be done");

}



}//try
catch( SQLException ex )
{
   ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
   ex.printStackTrace() ;
}
/*
finally
{
   try { if( rs != null ) rs.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
   try { if( prep1 != null ) prep1.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}

try { if( prep2 != null ) prep2.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
  try { if( connection != null ) connection.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}


}

*/

%>
</body>
</html>
thanks
0
 

Author Comment

by:Jasbir21
ID: 9788251
I  put this so i know what is going on:
System.out.println("*******");  
System.out.println("the id is ");
System.out.println(request.getParameter("id"));
System.out.println(updates);
System.out.println(path);
and i found out that when this window is loaded, the previous value gets displayed.

It is as if the form action works when it is loaded

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 9788329
<jsp:useBean id="ad" class="com.dhal.admin" scope="session"/>

Should be:

<jsp:useBean id="ad" class="com.dhal.admin" scope="request"/>

or

<jsp:useBean id="ad" class="com.dhal.admin" scope="page"/>

I think...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9788358
From: http://javaalmanac.com/egs/javax.servlet.jsp/usebean.jsp.html

-------------

 There are four scopes available - page, request, session, and application.

  A page-scoped bean is available only within the JSP page and is destroyed when the page has finished generating its output for the request.
  A request-scoped bean is destroyed when the response is sent.
  A session-scoped bean is destroyed when the session is destroyed.
  An application-scoped bean is destroyed when the web application is destroyed.
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9788434
I'm sorry Jasbir21, but I haven't had the time to look through your code in detail, but I get the impression that you are trying to accept user input and process this in one page.

If this is the case, you need to split the page into two, one for the input and another to process the input.

If you are trying to do it all on one page, that would explain why the result is being "ignored".  It is using the data that was originally provided, not the updates that you have made.

Sorry if I've mis-understood.  I'm a little busy at the moment (for a change.  Yippee!) ;-)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9788458
oooh...you got a job?  Coooooooool! :-) :-)
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9788509
Mmm.  J2ME too :-D
0
 

Author Comment

by:Jasbir21
ID: 9788516
Tim,
  I tried that, thanks.but the value is still inserting.thanks
jimmack,
  ok, i would try it now.
thanks

 
0
 

Author Comment

by:Jasbir21
ID: 9788560
I am not sure what it means but i think you mean like this:



this is adminupdate.jsp
<%@ page import="java.sql.*, com.dhal.*"%>

 
<jsp:useBean id="ad" class="com.dhal.admin" scope="session"/>
<jsp:setProperty name="ad" property="*"/>

<Html>

<Head>
<style type="text/css"> <!-- .inputimage { background-image: url((URL))} --> </style>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<!--Calendar Scripts -->
<script language="javascript" src="cal2.js"></script>
<script language="javascript" src="cal_conf2.js"></script>
<!--End Calendar Scripts-->

<script>
function myFunc(){
     window.opener.location.replace('menu.jsp');
     window.close();
}

</script>

<Title>

</Title>
</Head>
<Body BGColor="#FFFFF0">
<Font Size="2" Color="#FF0000" Face="Verdana">
<I> All fields marked with a red asterisk (*) must be filled in </I>
</Font>
<br>
<hr>


<form action="trial.jsp" name="form1" method="post" >
<Font Size="4 Color="Black" Face="Garamond" Align="Center">

Image :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<input type="file" name="path" class="inputimage">
</p>


Updates :&nbsp;&nbsp;&nbsp;&nbsp;

<textarea type="text" name="updates" rows="5" cols="15" wrap>
</textarea>



</p>


<input type=button value=Update onClick="myFunc()">


</form>

</body>
</html>

processing is done here:
trial.jsp

<%
String connectionURL = "jdbc:mysql://localhost:3306/mydatabase?user=;password=";
Connection connection = null;
Statement statement = null;
PreparedStatement prep1,prep2,prep3;
prep1=null;
prep2=null;
String id="";
String updates="";
String path="";
ResultSet rs = null;
try
{
 
   
   
   Class.forName("com.mysql.jdbc.Driver").newInstance();
   connection = DriverManager.getConnection(connectionURL, "", "");
   statement=connection.createStatement();
   
    updates=ad.getUpdates();
 path=ad.getPath();

   id=request.getParameter("id");


String query="select position from picture where position='"+request.getParameter("id")+"'";
rs=statement.executeQuery(query);

if(rs.next())
{
System.out.println("Updatea doing");

String myed="update picture set path=?,updates=? where position='"+request.getParameter("id")+"'";
prep2=connection.prepareStatement(myed);

prep2.setString(1,path);
prep2.setString(2,updates);

prep2.executeUpdate();

System.out.println("*%%%%%%%1******");  
System.out.println("the id is ");
System.out.println(request.getParameter("id"));
System.out.println(updates);
System.out.println(path);

System.out.println("*****");
}

else{




System.out.println("Update cannot be done");

}



}//try
catch( SQLException ex )
{
   ex.printStackTrace() ;
}
catch( ClassNotFoundException ex )
{
   ex.printStackTrace() ;
}
/*
finally
{
   try { if( rs != null ) rs.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
   try { if( prep1 != null ) prep1.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}

try { if( prep2 != null ) prep2.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}
  try { if( connection != null ) connection.close() ; } catch( Exception ex ) {ex.printStackTrace() ;}


}

*/

%>

I don't know value is still inserting, do you mean two forms like this.

Pls see at it when you are free pls.

thanks
0
 
LVL 15

Accepted Solution

by:
jimmack earned 600 total points
ID: 9788634
I think you're going in the right direction ;-)

You need the SELECT stuff on one page (to allow the user to browse the images), and when they have selected the image they want, you need to send this data (using session variables/request parameters etc.) to a second page with the UPDATE code.

Basically, all the JSP code for the page is compiled and executed when the page is loaded, so you need the second page to do the update.  If it's all on one page, the update is done when the page is loaded, not after the user has made selections.

I hope this makes sense ;-)
0
 

Author Comment

by:Jasbir21
ID: 9789066
hi,
  it works thank you so much.

0
 
LVL 15

Expert Comment

by:jimmack
ID: 9789085
;-) No problem
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
On September 18, Experts Exchange launched the first installment of the Help Bell, a new feature for Premium Members, Team Accounts, and Qualified Experts. The Help Bell will serve as an additional tool to help teams increase question visibility.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

705 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