Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within JDBC and Relational Databases
Search Coderanch
Advance search
Google search
Register / Login
Forums
Register
Login
Databases
»
JDBC and Relational Databases
servlet and database
supriya lal
,
Greenhorn
Mar 06, 2013 06:06:56
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
The "res" is not becoming more than zero and hence the else block in
servlet
is executing.The database is not getting updated.Please help
[u]SERVLET[/u] import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class addproductservlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List msg=new LinkedList(); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); HttpSession session= request.getSession(); String userid=(String)session.getAttribute("user"); String prodname=request.getParameter("product"); int price=Integer.parseInt(request.getParameter("price")); int quantity=Integer.parseInt(request.getParameter("quantity")); addproductmodel ob=new addproductmodel(); int res=0; try { res= ob.addproduct(userid,prodname,price); out.println("after res"); } catch (SQLException ex) { out.println("error"); } if(res>0) { msg.add("product is added successfully"); request.setAttribute("msg", msg); RequestDispatcher view= request.getRequestDispatcher("product.jsp"); view.forward(request,response); } else { msg.add("error in adding product"); request.setAttribute("msg", msg); RequestDispatcher view= request.getRequestDispatcher("product.jsp"); view.forward(request,response); } } } [u]model[/u] import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class addproductmodel { int addproduct(String userid, String prodname, int price) throws SQLException { int r=0; String url = "jdbc:db2://localhost:50000/MYEMANDI:" + "user=db2admin;password=p@ssw0rd;"; try{ Class.forName("com.ibm.db2.jcc.DB2Driver"); } catch(Exception e) { e.printStackTrace();} try{ Connection cn = DriverManager.getConnection(url); PreparedStatement stmt=cn.prepareStatement("insert into product values(?,?,?)"); stmt.setString(1,userid ); stmt.setString(2, prodname); stmt.setInt(3, price); r=stmt.executeUpdate(); } catch(Exception e) {System.out.println("error in catch");} return r; }}
Swastik Dey
,
Bartender
staff
Mar 06, 2013 06:25:39
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Are you getting any exception?
Prabhakar Reddy Bokka
,
Ranch Hand
Mar 06, 2013 07:10:41
(0 cow)
(0 skulls)
+Pie
Number of slices to send:
Send
Looks like there is an issue with the
JDBC
code.
Test
your JDBC code with a simple
java
class and then try this...
Post by:
autobot
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
This thread has been viewed 934 times.
Similar Threads
detect the session attributes after the session is TimeOut?
Unable to update database
Issue with Invoking UserServlet after logging in successfully form LoginServet
Unable to find specific product
Show only one record instead of specific record when onclick
More...
All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:06:42
.