• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JSP update  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
   <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
   </head>
   <body>
      <%
           Class.forName("com.mysql.jdbc.Driver").newInstance();
           
           Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/reg","root","");
         
           
           String place=request.getParameter("place");
           String price=request.getParameter("price");
         
           String fulname=request.getParameter("fulname");
           String str="Update mm set fulname=? place=? price=? where fulname='"+fulname+"'";
           PreparedStatement ps=con.prepareStatement(str);
           ps.setString(1, fulname);
           ps.setString(2,place);
           ps.setString(3,price);
           int i=ps.executeUpdate();        
      if(i>0)
           {
              {%>
             
            <script type="text/javascript">alert("Booking Successful Mr  "+"<%=session.getAttribute("fulname")%>");
                   window.location='nbook.jsp';
                     
                  </script>
             
             
             
             
              <%}
               
               
               
           }
           else
           {
               out.println("update  failed");
           }
           
         
           
           %>
       
   </body>
</html>


database fields are fname,lname,email,fulname,place,price;
i want to update only place and price but update failed.
 
Bartender
Posts: 20125
103
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Sanjay!

You've committed 2 offences against good Enterprise Java practices.

First, you directly acquired your database connection from the DriverManager instead of using a Connection Pool. That's forgiveable while you're still learning, but it's inefficient and discouraged for real-world programming.

More seriously, you put your logic in the JSP using scriptlets. This will cause the Bear to growl at you.

Scriptlet use is discouraged these days. Among the reasons why it's discourages, you've already seen.There's virtually no way to put a breakpoint into scriptlet code and debug it.

Instead you should be using a Java servlet (Controller) to do your database logic and have it forward the results as bean objects for the JSP to display. Servlets are much easier to debug.
 
Sheriff
Posts: 23968
50
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, let's get rid of the JSP code, which just obfuscates the actual problem, and leave only the JDBC code which is actually the problem.



And let's move the thread to the JDBC forum, since it's a JDBC problem. The surrounding JSP code just confuses the issue and (as Tim said) shouldn't even be there.
 
Paul Clapham
Sheriff
Posts: 23968
50
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. Clearly you know how to write a PreparedStatement, so why are you still generating the SQL with String concatenation? I'd suggest it would be better like this:



And then, there isn't much point in setting the "fulname" column to the same value which is already there. So let's leave that out:



And finally we come to the main problem. I assume "the update failed" was accompanied by an error message, with more information, thrown by the JSP? If not, then that just means that it would be better to test this kind of code outside of the web application, or to arrange things so that you get to see the actual error message from the database server. Which would have told you that the SQL statement wasn't valid SQL. In your case you forgot to separate the parts of the "SET" clause with commas, as required by SQL. Thus:

 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!