Advertisement
Guest User

Untitled

a guest
Aug 18th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. package com.company.app.logic;
  2.  
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5.  
  6. public class Item {
  7.  
  8. private int itemId;
  9. private String itemName;
  10.  
  11. public Item(ResultSet rs) {
  12. try {
  13. this.itemId = rs.getInt(1);
  14. this.itemName = rs.getString(2);
  15. } catch (SQLException e) {
  16. e.printStackTrace();
  17. }
  18. }
  19.  
  20. public String getItemName() {
  21. return itemName;
  22. }
  23.  
  24. public int getItemId() {
  25. return itemId;
  26. }
  27.  
  28. public String toString() {
  29. return itemName;
  30. }
  31. }
  32.  
  33.  
  34. package com.company.app.logic;
  35. import java.sql.*;
  36. import java.util.*;
  37. public class Main {
  38. private static Main instance = new Main();
  39. private static Connection connection;
  40. private Main() {
  41. try {
  42. Class.forName("com.mysql.jdbc.Driver");
  43.  
  44. String url = "jdbc:mysql://localhost:3306/maindb";
  45. connection = DriverManager.getConnection(url, "root", "root");
  46. } catch (Exception e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. public static Main getInstance() {
  51. return instance;
  52. }
  53. public List<Item> getItems() {
  54. List<Item> items = new ArrayList<>();
  55. Statement statement = null;
  56. ResultSet rs = null;
  57. try {
  58. statement = connection.createStatement();
  59. rs = statement.executeQuery("SELECT * FROM items");
  60. while (rs.next()) {
  61. Item g = new Item(rs);
  62. items.add(g);
  63. }
  64. } catch (SQLException e) {
  65. e.printStackTrace();
  66. } finally {
  67. try {
  68. if (rs != null) {
  69. rs.close();
  70. }
  71. if (statement != null) {
  72. statement.close();
  73. }
  74. } catch (SQLException e) {
  75. e.printStackTrace();
  76. }
  77. }
  78. return items;
  79. }
  80. }
  81.  
  82.  
  83. package com.company.app.web;
  84.  
  85. import com.company.app.logic.Item;
  86. import com.company.app.logic.Main;
  87.  
  88. import javax.servlet.ServletException;
  89. import javax.servlet.annotation.WebServlet;
  90. import javax.servlet.http.HttpServlet;
  91. import javax.servlet.http.HttpServletRequest;
  92. import javax.servlet.http.HttpServletResponse;
  93. import java.io.IOException;
  94. import java.io.PrintWriter;
  95. import java.util.List;
  96.  
  97. @WebServlet("/items")
  98. public class MainServlet extends HttpServlet {
  99. @Override
  100. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  101. resp.setContentType("text/html;charset=utf-8");
  102.  
  103. PrintWriter pw = resp.getWriter();
  104. pw.println("<table border=1>");
  105. try {
  106. List<Item> l = Main.getInstance().getItems();
  107. for (Item item : l) {
  108. pw.println("<tr>");
  109. pw.println("<td>" + item.getItemId() + "</td>");
  110. pw.println("<td>" +item.getItemName() + "</td>");
  111. pw.println("</tr>");
  112. }
  113. } catch (Exception e) {
  114. throw new ServletException(e);
  115. }
  116. pw.println("</table>");
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement