Guest User

Untitled

a guest
Jan 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.59 KB | None | 0 0
  1. //package searchbook;
  2.  
  3. import java.io.*;
  4.  
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. import java.sql.*;
  9. import java.util.*;
  10.  
  11. public class SearchBook extends HttpServlet {
  12. public void doPost(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. response.setContentType("text/html");
  15. HttpSession session = request.getSession(true);
  16. List booklist = new ArrayList();
  17. Connection con = null;
  18.  
  19. String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" + "C:\users\ppreeti\executive_db.accdb";
  20.  
  21. String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
  22. String user = "";
  23. String pass = "";
  24. String category = "";
  25. category = request.getParameter("input");
  26. String sqlquery = "select Index1.link_id "
  27. + "FROM Index1 "
  28. + " WHERE Index1.index_name LIKE '%" + category + "%' ";
  29. String sqlResult = null;
  30. try {
  31. Class.forName(driver);
  32. con = DriverManager.getConnection(url, user, pass);
  33. try {
  34. Statement st = con.createStatement();
  35. System.out.println("Connection created 1");
  36. ResultSet rs = st.executeQuery(sqlquery);
  37. while (rs.next()) {
  38. sqlResult = rs.getString(1);
  39.  
  40. }
  41. System.out.println("Result retreived 1");
  42. //System.out.println('"sqlquery"');
  43. } catch (SQLException s) {
  44. System.out.println("SQL statement is not executed! " + s);
  45. }
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. System.out.println("************");
  50. String sqlq = "";
  51.  
  52. int flag = 0;
  53.  
  54.  
  55. /*if(sqlResult.equals("1"))
  56. {
  57. flag=1;
  58. System.out.println("entered if block for section!");
  59. sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM Report "
  60. + " where Report.Report_Name LIKE '%"+category+"%' ";
  61. sqlq="select Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name "
  62. + "FROM Section , Report , Contact"
  63. + " WHERE Report.Section_ID=Section.Section_ID and Contact.Contact_ID=Report.Report_ID "
  64. + "and Section.Section_Name = '"+category+"' ";
  65. } */
  66. if (sqlResult.equals("1")) {
  67. flag = 1;
  68. System.out.println("entered if block for section!");
  69. /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM Report "
  70. + " where Report.Report_Name LIKE '%"+category+"%' ";*/
  71. sqlq = "select distinct Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name "
  72. + "FROM Section , Report , Contact"
  73. + " WHERE Report.Section_ID=Section.Section_ID and Contact.Contact_ID=Report.Contact_ID and Section.Section_Name LIKE '%" + category + "%' ";
  74. // + "and Section.Section_Name = '"+category+"' ";
  75. }
  76.  
  77. if (sqlResult.equals("2")) {
  78. flag = 1;
  79. System.out.println("entered if block for report!");
  80. /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM Report "
  81. + " where Report.Report_Name LIKE '%"+category+"%' ";*/
  82. sqlq = "select distinct Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name "
  83. + "FROM Section , Report , Contact"
  84. + " WHERE Report.Section_ID=Section.Section_ID and Contact.Contact_ID=Report.Contact_ID and Report.Report_Name LIKE '%" + category + "%' ";
  85. // + "and Report.Report_Name = '"+category+"' ";
  86. }
  87. if (sqlResult.equals("3")) {
  88. flag = 1;
  89. System.out.println("entered if block for metrics !");
  90. /*sqlq="select Report.Report_Name,Report.Report_ID,Report.Section_ID,Report.Contact_ID,Report.link_id FROM Report "
  91. + " where Report.Report_Name LIKE '%"+category+"%' "*/
  92. ;
  93.  
  94. sqlq = "select distinct Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name "
  95. + "FROM Section , Report , Contact,Metrics"
  96. + " WHERE Report.Section_ID=Section.Section_ID and Contact.Contact_ID=Report.Contact_ID and Metrics.Report_ID=Report.Report_ID "
  97. + "and Metrics.Metric_Name LIKE '%" + category + "%' ";
  98. }
  99. if (sqlResult.equals("4")) {
  100. flag = 1;
  101. System.out.println("entered if block for contact name!");
  102. /*sqlq="select Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name, Metrics.Metric_Name "
  103. + "FROM Section , Report , Contact, Metrics"
  104. + " WHERE Report.Section_ID=Section.Section_ID and Metrics.Report_ID=Report.Report_ID "
  105. + "and Report.Report_ID IN (SELECT Report.Report_ID FROM Report WHERE "
  106. + "Contact.Contact_ID=Report.Contact_ID and Contact.Contact_Name LIKE '%"+category+"%' and Metrics.Metric_Segment = 'M') ORDER BY Report_Name ";*/
  107.  
  108.  
  109. sqlq = "select distinct Section.Section_Name , Report.Report_Name , Report.Link, Contact.Contact_Name "
  110. + "FROM Section , Report , Contact"
  111. + " WHERE Report.Section_ID=Section.Section_ID and Contact.Contact_ID=Report.Contact_ID "
  112. + "and Contact.Contact_Name LIKE '%" + category + "%' ";
  113. }
  114.  
  115.  
  116. if (flag == 1) {
  117. try {
  118. Class.forName(driver);
  119. con = DriverManager.getConnection(url, user, pass);
  120. try {
  121. Statement st = con.createStatement();
  122. System.out.println("Connection created");
  123. ResultSet rs = st.executeQuery(sqlq);
  124. System.out.println("Result retreived for 2nd query ");
  125. while (rs.next()) {
  126. List<String> book = new ArrayList<String>();
  127.  
  128. String Name = rs.getString("Section_Name");
  129. String reportName = rs.getString("Report_Name");
  130. String link = rs.getString("Link");
  131. String contactName = rs.getString("Contact_Name");
  132. /* String metricName=rs.getString("Metric_Name");*/
  133. //String reportId=rs.getString("Report_ID");
  134.  
  135. book.add(Name);
  136. book.add(reportName);
  137. book.add(link);
  138. book.add(contactName);
  139. /* book.add(metricName);*/
  140. //book.add(reportId);
  141.  
  142. /* book.add(ind_id);
  143. book.add(ind_name);*/
  144.  
  145. booklist.add(book);
  146. }
  147. } catch (SQLException s) {
  148. s.printStackTrace();
  149. System.out.println("SQL statement is not executed in 2nd query! " + s);
  150. }
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. System.out.println("And it came here lastly !");
  156. request.setAttribute("booklist", booklist);
  157. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/searchbook.jsp");
  158. dispatcher.forward(request, response);
  159. System.out.println("***************************************************************************************");
  160. }
  161. }
Add Comment
Please, Sign In to add comment