469,945 Members | 2,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

Deleting dynamic records in database using Servlets and JDBC

29
Hi,

I have got the codings for inserting and displaying the records in the database. But i couldn't able to delete a specific row, which is created dynamically.

i.e, if i give any one of the field value in a text box, then using the request.getParameter() method i have to delete the entire record with the value given in the textbox.

I need the jdbc codings....... for that........

i have tried the following codings but it does not work.

Statement st = con.createStatement("DELETE FROM email WHERE email="+'"'+request.getParameter("email1")+'"');
ResultSet rs=st.executeQuery();


The solution can also be in MVC pattern.


It would be so helpful, if u provide me the solution.
Jul 3 '07 #1
8 7871
r035198x
13,262 8TB
Hi,

I have got the codings for inserting and displaying the records in the database. But i couldn't able to delete a specific row, which is created dynamically.

i.e, if i give any one of the field value in a text box, then using the request.getParameter() method i have to delete the entire record with the value given in the textbox.

I need the jdbc codings....... for that........

i have tried the following codings but it does not work.

Statement st = con.createStatement("DELETE FROM email WHERE email="+'"'+request.getParameter("email1")+'"');
ResultSet rs=st.executeQuery();


The solution can also be in MVC pattern.


It would be so helpful, if u provide me the solution.
What happens when you run the program? Do you get an exception?
Jul 3 '07 #2
cygsoft
29
What happens when you run the program? Do you get an exception?
When it is executed a compile time error is generated.
The problem is with the "request.getParameter()" and returning the ResultSet.
Jul 4 '07 #3
r035198x
13,262 8TB
When it is executed a compile time error is generated.
The problem is with the "request.getParameter()" and returning the ResultSet.
Post the errors that you got then
Jul 4 '07 #4
i think u have to use executeUpdate() rather than ecexuteQuery().
and also one more change

Statement st = con.createStatement();
String sql = "DELETE FROM email WHERE email="+"'+request.getParameter("email1")'"";
ResultSet rs=st.executeUpdate(sql);

i think it will work for u...
check once.
Jul 5 '07 #5
r035198x
13,262 8TB
i think u have to use executeUpdate() rather than ecexuteQuery().
and also one more change

Statement st = con.createStatement();
String sql = "DELETE FROM email WHERE email="+"'+request.getParameter("email1")'"";
ResultSet rs=st.executeUpdate(sql);

i think it will work for u...
check once.
Erm, executeUpdate returns an int
Jul 5 '07 #6
ooh..sorry yar...

then is there any need to get record into resultset????
Jul 5 '07 #7
cygsoft
29
One small change.....
In case of MVC architecture, is it possible to delete the individual records by calling bean file.

It works well for inserting and displaying the records.........
I just give here the servlet and bean file for inserting and displaying.
Can anyone modify it and make it use for deleting the records...........

Servlet file

Expand|Select|Wrap|Line Numbers
  1.  import javax.servlet.*;
  2. import javax.servlet.http.*;
  3. import java.io.*;
  4. import java.sql.*;
  5.  
  6. public class dbServlet extends HttpServlet
  7. {
  8. public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
  9. {
  10. res.setContentType("text/html");
  11. PrintWriter pw=res.getWriter();
  12. try
  13. {
  14. dbBean bean=new dbBean();
  15. bean.connect();
  16. RequestDispatcher rd=null;
  17.  
  18. if((req.getParameter("action")).equals("insert"))
  19. {
  20. bean.setName(req.getParameter("name1"));
  21. bean.setAge(Integer.parseInt(req.getParameter("age1")));
  22. bean.insert();
  23. rd=req.getRequestDispatcher("/insert.jsp");
  24. rd.forward(req,res);
  25. }
  26.  
  27.  
  28. if((req.getParameter("action")).equals("display"))
  29. {
  30.  
  31.  
  32. bean.getName();
  33. bean.getAge();
  34. ResultSet rs=bean.display();
  35. req.setAttribute("RSObject",rs);
  36. rd=req.getRequestDispatcher("/display.jsp");
  37. rd.forward(req,res);
  38.  
  39. }
  40. }
  41. catch(Exception e)
  42. {
  43. System.out.println(e);
  44. }
  45. }
  46. }
Bean file

Expand|Select|Wrap|Line Numbers
  1.  import java.sql.*;
  2.  
  3. public class dbBean 
  4. {
  5.  
  6. Connection con=null;
  7. String name;
  8. int age;
  9.  
  10. public void connect()
  11. {
  12. try
  13. {
  14. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 
  15. con = DriverManager.getConnection("jdbc:odbc:data");
  16. }
  17. catch(Exception e)
  18. {
  19. System.out.println(e);
  20. }
  21. }
  22.  
  23. public void setName(String name)
  24. {
  25.  
  26. this.name=name;
  27. }
  28.  
  29. public void setAge(int age)
  30. {
  31.  
  32. this.age=age;
  33. }
  34.  
  35. public void insert()
  36. {
  37. try
  38. {
  39. PreparedStatement stmt = con.prepareStatement("insert into login(name,age) VALUES (?,?)");
  40.  
  41. stmt.setString(1,name);
  42. stmt.setInt(2,age);
  43. stmt.executeUpdate();
  44. }
  45. catch(Exception e)
  46. {
  47. System.out.println(e);
  48. }
  49. }
  50.  
  51. public String getName()
  52. {
  53. return name;
  54. }
  55.  
  56. public int getAge()
  57. {
  58. return age;
  59. }
  60.  
  61.  
  62. public ResultSet display()
  63. {
  64. ResultSet rs=null;
  65. try
  66. {
  67. Statement stmt = con.createStatement();
  68.  rs=stmt.executeQuery("select * from login");
  69. }
  70. catch(Exception e)
  71. {
  72. System.out.println(e);
  73. }
  74. return rs;
  75. }
  76.  
  77.  
}

and the Result set is returned to jsp where JSP displays the records.............

Pls can anyone modify it and make it use for deleting the records......
Jul 5 '07 #8
cygsoft
29
Yes,
the final resultset has to be returned after deletion and displayed.
Jul 5 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Dan | last post: by
6 posts views Thread by Martin Bischoff | last post: by
6 posts views Thread by =?ISO-8859-1?Q?Tim_B=FCthe?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.