• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JSP update  RSS feed

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>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
       <title>JSP Page</title>
           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);
           int i=ps.executeUpdate();        
            <script type="text/javascript">alert("Booking Successful Mr  "+"<%=session.getAttribute("fulname")%>");
               out.println("update  failed");

database fields are fname,lname,email,fulname,place,price;
i want to update only place and price but update failed.
Posts: 20125
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.
Posts: 23968
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
Posts: 23968
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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!