Guest User

Untitled

a guest
Apr 7th, 2019
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18.  
  19. /**
  20. *
  21. * @author Abhishek
  22. */
  23. public class searchContractor extends HttpServlet {
  24.  
  25. /**
  26. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  27. * methods.
  28. *
  29. * @param request servlet request
  30. * @param response servlet response
  31. * @throws ServletException if a servlet-specific error occurs
  32. * @throws IOException if an I/O error occurs
  33. */
  34. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  35. throws ServletException, IOException {
  36. response.setContentType("text/html;charset=UTF-8");
  37. try (PrintWriter out = response.getWriter()) {
  38. /* TODO output your page here. You may use following sample code. */
  39. out.println("<!DOCTYPE html>");
  40. out.println("<html>");
  41. out.println("<head>");
  42. out.println("<title>Servlet searchContractor</title>");
  43. out.println("</head>");
  44. out.println("<body>");
  45. out.println("<h1>Servlet searchContractor at " + request.getContextPath() + "</h1>");
  46. out.println("</body>");
  47. out.println("</html>");
  48. }
  49. }
  50.  
  51. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  52. /**
  53. * Handles the HTTP <code>GET</code> method.
  54. *
  55. * @param request servlet request
  56. * @param response servlet response
  57. * @throws ServletException if a servlet-specific error occurs
  58. * @throws IOException if an I/O error occurs
  59. */
  60. private static String dbURL = "jdbc:derby://localhost:1527/assignment;create=true;user=abhishek;password=abhishek";
  61. private static String tableName = "contractor_details";
  62. // jdbc Connection
  63. private static Connection conn = null;
  64. private static Statement stmt = null;
  65.  
  66. private static void createConnection()
  67. {
  68. try
  69. {
  70. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  71. //Get a connection
  72. conn = DriverManager.getConnection(dbURL);
  73. }
  74. catch (Exception except)
  75. {
  76. except.printStackTrace();
  77. }
  78. }
  79. @Override
  80. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  81. throws ServletException, IOException {
  82. PrintWriter out = response.getWriter();
  83. String contractor_name = (request.getParameter("contractor_name"));
  84.  
  85. createConnection();
  86. try
  87. {
  88. stmt = conn.createStatement();
  89. ResultSet rs = stmt.executeQuery("select * from " + tableName + " where contractor_name LIKE '"+contractor_name+"%'");
  90. while(rs.next()){
  91. //Retrieve by column name
  92. int id = rs.getInt("contractor_id");
  93. String name = rs.getString("contractor_name");
  94. int bid_cost = rs.getInt("bid_cost");
  95. String materials_used = rs.getString("materials_used");
  96. //String estimated_completion_date = rs.getString("estimated_completion_date");
  97. //Display values
  98. out.print("ID: " + id);
  99. out.print(", CONTRACTOR_NAME: " + name);
  100. out.print(", BID_COST: " + bid_cost);
  101. out.print(", MATERIALS_USED: " + materials_used);
  102. //System.out.print(", ESTIMATED_COMPLETION_DATE: " + estimated_completion_date);
  103. out.println("");
  104. }
  105. rs.close();
  106. stmt.close();
  107. }
  108. catch (SQLException sqlExcept)
  109. {
  110. sqlExcept.printStackTrace();
  111. }
  112.  
  113. try
  114. {
  115. if (stmt != null)
  116. {
  117. stmt.close();
  118. }
  119. if (conn != null)
  120. {
  121. DriverManager.getConnection(dbURL + ";shutdown=true");
  122. conn.close();
  123. }
  124. }
  125. catch (SQLException sqlExcept)
  126. {
  127.  
  128. }
  129.  
  130. }
  131.  
  132. /**
  133. * Handles the HTTP <code>POST</code> method.
  134. *
  135. * @param request servlet request
  136. * @param response servlet response
  137. * @throws ServletException if a servlet-specific error occurs
  138. * @throws IOException if an I/O error occurs
  139. */
  140. @Override
  141. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  142. throws ServletException, IOException {
  143. processRequest(request, response);
  144. }
  145.  
  146. /**
  147. * Returns a short description of the servlet.
  148. *
  149. * @return a String containing servlet description
  150. */
  151. @Override
  152. public String getServletInfo() {
  153. return "Short description";
  154. }// </editor-fold>
  155.  
  156. }
Add Comment
Please, Sign In to add comment