Solved

Data not updating -Why

Posted on 2003-11-20
13
305 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exception creating bean of class 5 184
maven webservice example issue 2 134
mask rule in struts validation.xml 5 87
jdbc error in jsp application 20 45
This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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