473,732 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting dynamic records in database using Servlets and JDBC

29 New Member
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.getPara meter() 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.createState ment("DELETE FROM email WHERE email="+'"'+req uest.getParamet er("email1")+'" ');
ResultSet rs=st.executeQu ery();


The solution can also be in MVC pattern.


It would be so helpful, if u provide me the solution.
Jul 3 '07 #1
8 8311
r035198x
13,262 MVP
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.getPara meter() 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.createState ment("DELETE FROM email WHERE email="+'"'+req uest.getParamet er("email1")+'" ');
ResultSet rs=st.executeQu ery();


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 New Member
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.getPar ameter()" and returning the ResultSet.
Jul 4 '07 #3
r035198x
13,262 MVP
When it is executed a compile time error is generated.
The problem is with the "request.getPar ameter()" and returning the ResultSet.
Post the errors that you got then
Jul 4 '07 #4
koneru9999
6 New Member
i think u have to use executeUpdate() rather than ecexuteQuery().
and also one more change

Statement st = con.createState ment();
String sql = "DELETE FROM email WHERE email="+"'+requ est.getParamete r("email1")'" ";
ResultSet rs=st.executeUp date(sql);

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

Statement st = con.createState ment();
String sql = "DELETE FROM email WHERE email="+"'+requ est.getParamete r("email1")'" ";
ResultSet rs=st.executeUp date(sql);

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

then is there any need to get record into resultset????
Jul 5 '07 #7
cygsoft
29 New Member
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 New Member
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
7350
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 backend and get/set information for me. When returning database records from the bean, what common methods to people use? I've seen records returned as arraylists, but I'm not sure I like this method too well. To get the information in the next column, I can use nextElement(), but then I'm...
3
9642
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. The app is JAVA with JDBC. In testing the users see no problems and think the app is running okay. I turned a database monitor on the app and see that the database is getting SQL return coes of -301 for a variety of database accesses (some times the same access works, okay other times -301)....
3
1998
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 off, by the tine I import a few hundred thousand records the database is _huge_. Like 500 meg, although the csv files are only 25 meg in total. When I compact the database its still 100meg. How do I keep the size down? Second, I did a manual delete of 300,000 records yesterday by doing a...
1
1887
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. The database performs many complex calculations and displays the results in both forms and reports. In the process of developing the database we "entered" 12 sample clients. Before packaging the database we remove the sample clients by deleting them from the main table, which cascades the...
1
2396
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 pretty well up until I made the mistake of deleting about 80 records from the Orders table. 80 out of a 1000 records. Now our data entry form shows our customer addresses, but not customer order history. When looking at all of the tables, customer, payments, orders, they still have all of the...
6
4560
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 directories (from the code behind), the application restarts and all active sessions are terminated. This error is also described in detail here:...
8
4654
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 all my JDBC code. it is working very nicely and displaying records. now i wrote all the JDBC code in .java and i am accessing that code in jsp file. but this time i am getting only exceptions not records. i am keeping my programs here. please crosscheck once and tell where i am wrong code ...
11
3673
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 by entering the Date, Shift (morn, eve, or night) and the 'employee name'. There is another table which assigns an ID to the Shifts, i.e. 1,2 and 3 for morn, eve & night shifts respectively. From the mother table, the incentive is calculated datewise for each employee as per his shift duty. In...
6
7973
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 PreparedStatements only (aka dynamic SQL). Every night, there is a ETL which deletes most of the data in the database and fills it all new. We observed very bad performance for some statements that ran for minutes (The queried table is about 100,000 records and the result is about 500 rows)....
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8772
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9303
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9177
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4803
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3258
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2176
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.