Solved

Data not updating -Why

Posted on 2003-11-20
13
303 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
  • 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
 
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 150 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
In this article, I show you step by step with screenshots to assist you - HOW TO: Deploy and Install the VMware vCenter Server Appliance 6.5 (VCSA 6.5), with some helpful tips along the way.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

757 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