Guest User

Untitled

a guest
Apr 2nd, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <%@ page import="java.sql.*" %>
  3. <%ResultSet resultset =null;%>
  4. <% String month[]={"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"}; %>
  5. <% int i=month.length-1; %>
  6. <% int j=0; %>
  7.  
  8.  
  9. <HTML>
  10. <HEAD>
  11. <TITLE>REPORT MODULE</TITLE>
  12. <link rel="stylesheet" href="css/jquery-ui.css">
  13. <link rel="stylesheet" href="css/style.css">
  14. <script src="js/jquery-1.11.1.js"></script>
  15. <script src="js/jquery-ui.js"></script>
  16.  
  17. <script>
  18. $(function() {
  19. $( "#tabs" ).tabs();
  20. });
  21. </script>
  22.  
  23.  
  24. </HEAD>
  25.  
  26. <BODY>
  27. <div class="wrapper">
  28. <div class="container">
  29. <div id="tabs">
  30. <ul>
  31. <li><a href="#Report">Report Module</a></li>
  32.  
  33. </ul>
  34. <div id="Report">
  35.  
  36. <%
  37. try{
  38. Class.forName("org.postgresql.Driver").newInstance();
  39. Connection connection =
  40. DriverManager.getConnection
  41. ("jdbc:postgresql://localhost:2280/CASTCOE3?user=operator&password=CastAIP");
  42.  
  43. Statement statement = connection.createStatement() ;
  44.  
  45. resultset =statement.executeQuery("select acc_name, project_name from Account") ;
  46.  
  47. %>
  48.  
  49.  
  50.  
  51.  
  52. <form method="post" action="ExportController">
  53. <label for="acc_Name">Account Name/Project Name:</label>&nbsp;&nbsp;&nbsp;&nbsp;
  54. <select name="accountName">
  55. <% while(resultset.next()){ %>
  56. <option><%= resultset.getString(1)+"/"+resultset.getString(2)%></option>
  57. <% } %>
  58. </select> <br />
  59. <label for="month">Month:</label>&nbsp;&nbsp;&nbsp;&nbsp;
  60. <select name="month">
  61. <% while(j<=i){ %>
  62. <option><%= month[j] %></option>
  63. <% j++; %>
  64. <% } %>
  65. </select> <br /> <br />
  66.  
  67.  
  68. <input type="submit" name="Export to Excel" value="Export to Excel"></a>
  69.  
  70.  
  71. </form>
  72.  
  73. </div>
  74.  
  75.  
  76. <%
  77. //**Should I input the codes here?**
  78. }
  79. catch(Exception e)
  80. {
  81. out.println("wrong entry"+e);
  82. }
  83. %>
  84.  
  85.  
  86.  
  87. </BODY>
  88. </HTML>
  89.  
  90. package com.coetool.servlet.controller;
  91.  
  92.  
  93.  
  94. import java.sql.*;
  95. import java.io.*;
  96.  
  97. import javax.servlet.ServletException;
  98.  
  99.  
  100. import javax.servlet.http.HttpServlet;
  101. import javax.servlet.http.HttpServletRequest;
  102. import javax.servlet.http.HttpServletResponse;
  103.  
  104.  
  105.  
  106.  
  107. import org.apache.poi.hssf.usermodel.HSSFRow;
  108. import org.apache.poi.hssf.usermodel.HSSFSheet;
  109. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  110. import org.apache.poi.ss.usermodel.*;
  111.  
  112.  
  113.  
  114. public class ExportController extends HttpServlet {
  115.  
  116.  
  117.  
  118. @Override
  119. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  120.  
  121. String[] b=new String[5];
  122. String c=null;
  123. int e = 0;
  124.  
  125.  
  126. try{
  127. Class.forName("org.postgresql.Driver");
  128. Connection connection =
  129. DriverManager.getConnection
  130. ("jdbc:postgresql://localhost:2280/CASTCOE3?user=operator&password=CastAIP");
  131.  
  132. Statement statement = connection.createStatement() ;
  133.  
  134.  
  135. if(request.getParameter("Export to Excel")!=null){
  136.  
  137. String AccountName = request.getParameter("accountName");
  138.  
  139.  
  140. b=AccountName.split("/");
  141. c=b[0];
  142. String mon=request.getParameter("month");
  143. String a[]={"Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"};
  144. int d[]={1,2,3,4,5,6,7,8,9,10,11,12};
  145. for(int j=0;j<=11;j++){
  146. if(a[j].equals(mon)){
  147. e=d[j];
  148. break;}
  149. else {
  150. e=d[j];}}
  151.  
  152.  
  153.  
  154.  
  155. String s="SELECT DISTINCT a.project_id, a.acc_name, a.project_name, a.iot,a.ilc_code, a.active, a.license_no, b.app_fte, b.contact_person, c.cost_call_date from Account a INNER JOIN Application b USING (acc_id) INNER JOIN account_version c USING (acc_id) where a.acc_name="+"'"+c+"'"+"AND EXTRACT (MONTH FROM c.cost_call_date)="+e;
  156.  
  157. ResultSet rs = statement.executeQuery(s);
  158. HSSFWorkbook workbook = new HSSFWorkbook();
  159. HSSFSheet sheet = workbook.createSheet("Account_Version");
  160. HSSFRow rowhead = sheet.createRow((short) 0);
  161. rowhead.createCell((short) 0).setCellValue("Project ID");
  162. rowhead.createCell((short) 1).setCellValue("Account Name");
  163. rowhead.createCell((short) 2).setCellValue("Project Name");
  164. rowhead.createCell((short) 3).setCellValue("IOT");
  165. rowhead.createCell((short) 4).setCellValue("Account Code");
  166. rowhead.createCell((short) 5).setCellValue("FTE");
  167. rowhead.createCell((short) 6).setCellValue("License Key");
  168. rowhead.createCell((short) 7).setCellValue("Contact Person");
  169.  
  170.  
  171. rowhead.createCell((short) 8).setCellValue("Month");
  172. rowhead.createCell((short) 9).setCellValue("IsActive");
  173. rowhead.createCell((short) 10).setCellValue("Comments");
  174.  
  175. int i = 1;
  176. if(request.getParameter("Export to Excel")!=null && rs==null){
  177. alert("No Data for the selected fields");
  178. }
  179. if(request.getParameter("Export to Excel")!=null && rs!=null) {
  180. alert("Report generated successfully");
  181.  
  182. while (rs.next()){
  183. HSSFRow row = sheet.createRow((short) i);
  184. row.createCell((short) 0).setCellValue(rs.getString("project_id"));
  185. row.createCell((short) 1).setCellValue(rs.getString("acc_name"));
  186. row.createCell((short) 2).setCellValue(rs.getString("project_name"));
  187. row.createCell((short) 3).setCellValue(rs.getString("iot"));
  188. row.createCell((short) 4).setCellValue(rs.getString("ilc_code"));
  189. row.createCell((short) 5).setCellValue(rs.getInt("app_fte"));
  190. row.createCell((short) 6).setCellValue(rs.getInt("license_no"));
  191. row.createCell((short) 7).setCellValue(rs.getString("contact_person"));
  192. row.createCell((short) 8).setCellValue(mon);
  193. row.createCell((short) 9).setCellValue(rs.getString("active"));
  194.  
  195. i++;
  196. }
  197.  
  198.  
  199.  
  200. String yemi = "C:/Users/IBM_ADMIN/Desktop/test.xls";
  201. FileOutputStream fileOut = new FileOutputStream(yemi);
  202. workbook.write(fileOut);
  203. fileOut.close();}
  204. }}
  205.  
  206.  
  207. catch (ClassNotFoundException e1) {
  208. e1.printStackTrace();
  209. } catch (SQLException e1) {
  210. e1.printStackTrace();
  211. } catch (FileNotFoundException e1) {
  212. e1.printStackTrace();
  213. } catch (IOException e1) {
  214. e1.printStackTrace();
  215.  
  216. }
  217.  
  218.  
  219. request.getRequestDispatcher("index5.jsp").forward(request, response);
  220.  
  221. }
  222.  
  223. private void alert(String string) {
  224. // TODO Auto-generated method stub
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. }
Add Comment
Please, Sign In to add comment