Data not updating -Why

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
Jasbir21Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimmackCommented:
Difficult to answer without seeing the code.  You may be missing a call or doing something in the wrong order.
Jasbir21Author Commented:
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
Jasbir21Author Commented:
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

Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

TimYatesCommented:
<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...
TimYatesCommented:
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.
jimmackCommented:
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!) ;-)
TimYatesCommented:
oooh...you got a job?  Coooooooool! :-) :-)
jimmackCommented:
Mmm.  J2ME too :-D
Jasbir21Author Commented:
Tim,
  I tried that, thanks.but the value is still inserting.thanks
jimmack,
  ok, i would try it now.
thanks

 
Jasbir21Author Commented:
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
jimmackCommented:
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 ;-)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jasbir21Author Commented:
hi,
  it works thank you so much.

jimmackCommented:
;-) No problem
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.