472,337 Members | 1,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 software developers and data experts.

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

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

Similar topics

3
by: Dan | last post by:
Hello, I'm getting started on my first java project. I'm likely going to use jsp/servlets connected to javabeans. The beans will connect to my...
3
by: JDPope | last post by:
I have a situation which I cannot get a good lead on how to resolve. One of the applications I support uses the Hibernate software to generate SQL....
3
by: Mike Turco | last post by:
I'm working on an application that imports and exports tons of CSV data, like 64,000 records per file, and six or seven files in a set. First...
1
by: Coy Howe | last post by:
This one seems bizarre! We have a database consisting of a main table and 12 - 15 "sub" tables, which are connected via cascading relationships. ...
1
by: KC | last post by:
Hello, I am using Access 2002. WinXP, Template from MS called Orders Mgmt DB. I have tweaked this DB to work for our small co. It has worked...
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these...
8
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote...
11
by: shriil | last post by:
Hi I have this database that calculates and stores the incentive amount earned by employees of a particular department. Each record is entered...
6
by: =?ISO-8859-1?Q?Tim_B=FCthe?= | last post by:
Hi, we are building a Java webapplication using JSF, running on websphere, querying a DB2 9 on Suse Enterprise 10. The app uses JDBC and...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.