Guest User

Untitled

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