data retrieval trouble

Thread Solved

Join Date: Apr 2008
Posts: 11
Reputation: timon_zed is an unknown quantity at this point 
Solved Threads: 0
timon_zed timon_zed is offline Offline
Newbie Poster

data retrieval trouble

 
0
  #1
Sep 13th, 2009
Hi there,
I have a servlet which is meant to retrieve data from the database by compairing the number entered via a web form. The trouble is that, it only gives me the last record in the database and not any other record. I have 5 records in the database table and i want to be able to retrieve any one of them using the servlet. The code for the jsp and the servlet is below. Can anyone please point out where my mistake is and how I can resolve it.
Thanks in advance.

*the JSP Code*

  1. <html>
  2. <head>
  3. <title>Check Your data</title>
  4. <body>
  5. <form name="check" method="post" action="checkAvailable">
  6. <table>
  7. <tr>
  8. <td><input name="command" type="hidden" value=""></td>
  9. </tr>
  10. <tr>
  11. <td>Product No: </td>
  12. <td><input name="prodNum" id="prodNum" type="text"></td>
  13. </tr>
  14. <tr>
  15. <td><input name="check" type="submit" id="search" value="Check"></td>
  16. <td>&nbsp;</td>
  17. </tr>
  18. </table>
  19. </form>
  20. </body>
  21. </head>
  22. </html>
the servlet
  1. public class checkAvailable extends HttpServlet
  2. {
  3. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  4. throws ServletException, IOException
  5. {
  6.  
  7. response.setContentType("text/html;charset=UTF-8");
  8. PrintWriter out = response.getWriter();
  9. String ProdID = new String("");
  10. String ProdName = new String("");
  11. String PAmt = new String("");
  12.  
  13. try
  14. {
  15. Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//the driver manager string
  16. Connection con = DriverManager.getConnection("jdbc:odbc:Args","admin","");
  17. Statement st = con.createStatement();
  18. ResultSet rs = st.executeQuery ("SELECT ProductID,PName,Stock FROM Product");
  19.  
  20. while(rs.next())
  21. {
  22. ProdID = rs.getString("ProductID");
  23. ProdName = rs.getString("PName");
  24. PAmt = rs.getString("Stock");
  25. }
  26. con.close();
  27. rs.close ();
  28. st.close ();
  29. }
  30. catch(Exception e)
  31. {
  32. System.out.print("Unable to find DB");
  33. }
  34. if(ProdID.equals(request.getParameter("prodNum")))
  35. {
  36. out.print("Prodcut ID : " +ProdID+ " Product Name : " +ProdName+ " Quantity"+PAmt);
  37. }
  38. else
  39. {
  40.  
  41. out.print("Please Enter Correct Product Number");
  42.  
  43. }
  44. out.close();
  45. }
  46. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,761
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 491
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: data retrieval trouble

 
0
  #2
Sep 13th, 2009
You essentially throw away almost all the records read by reading the database entries in the ResultSet loop by not doing anything. If you want to compare the user entered value with the ones present in the database, do the comparison in the ResultSet loop. Set a flag and break out of the loop if the comparison succeeds.

Also a few points worth mentioning:

- Class names in Java should begin with an uppercase character; the same applies for Servlets.

- Since the JVM maintains a string pool, try not to use new String() *ever* in your code. Just use the string literals and let the JVM do the heavy lifting of deciding whether to use the existing pool instance or create a new one i.e.
  1. // Instead of doing
  2. String name = new String("");
  3.  
  4. // Do
  5. String name = "";
  6.  
  7. // Or better yet; though depends on the particular use case
  8. String name = null;

- For production/real use, don't prefer a JDBC-ODBC bridge driver but a pure Java Type 4 driver. Even if you are trying out things, doing things the right way would be much preferred. Use a database like MySQL or Derby along with the type 4 drivers which come along with it.

- You can simplify things a *lot* by modifying the query instead of jumping through all those hoops yourself. Just add an additional WHERE clause in your query and check if the returned ResultSet has at least one record; if yes, then the product was found, if no, it wasn't.

- Use PreparedStatement for efficiency since databases and drivers are known to perform certain optimizations related to them. Something like:
  1. String userProdId = request.getParameter("prodNum");
  2. PreparedStatement pstmt =
  3. connection.prepareStatement("SELECT ProductID,PName,Stock FROM Product WHERE ProductID = ?");
  4. pstmt.setString(1, userProdId);
  5. ResultSet rs = pstmt.executeQuery();
  6. if(rs.next()) {
  7. // a record was found; read the other data
  8. } else {
  9. // no such product id exists
  10. }
Last edited by ~s.o.s~; Sep 13th, 2009 at 11:49 am.
I don't accept change; I don't deserve to live.

Sacrifice is a painful, pure and beautiful thing.

Dammit, Jones, What the Hell Are Knoll Pointers?!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: timon_zed is an unknown quantity at this point 
Solved Threads: 0
timon_zed timon_zed is offline Offline
Newbie Poster

Re: data retrieval trouble

 
0
  #3
Sep 19th, 2009
Alright thanx ama give it a whirl.
Reply With Quote Quick reply to this message  
Reply

Tags
connection, database

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1401 | Replies: 2
Thread Tools Search this Thread



Tag cloud for connection, database
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC