Advertisement
Guest User

Untitled

a guest
Oct 7th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Context>
  3. <Resource name="jdbc/library"
  4. auth="Container"
  5. type="javax.sql.DataSource"
  6. initialSize="10"
  7. maxTotal="100"
  8. maxIdle="30"
  9. maxWaitMillis="100"
  10. username="root"
  11. password="admin"
  12. driveClassName="com.myqsl.jdbc.Driver"
  13. url="jdbc:mysql://localhost:3306/library?useSSl=false&amp;serverTimizone=UTC"/>
  14. </Context>
  15.  
  16. package pl.javastart.util;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19.  
  20. import javax.naming.Context;
  21. import javax.naming.InitialContext;
  22. import javax.naming.NamingException;
  23. import javax.sql.DataSource;
  24.  
  25. public class ConnectionProvider {
  26. private static DataSource dataSource;
  27.  
  28. public static Connection getConnection() throws SQLException {
  29. return getDSInstance().getConnection();
  30. }
  31.  
  32. private static DataSource getDSInstance() {
  33. if (dataSource == null) {
  34. try {
  35. Context initContext = new InitialContext();
  36. Context envContext = (Context) initContext.lookup("java:comp/env");
  37. dataSource = (DataSource) envContext.lookup("jdbc/library");
  38. } catch (NamingException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. return dataSource;
  43. }
  44. }
  45.  
  46. package pl.javastart.model;
  47.  
  48. public class Book {
  49. private String isbn;
  50. private String title;
  51. private String description;
  52.  
  53. public String getIsbn() {
  54. return isbn;
  55. }
  56. public void setIsbn(String isbn) {
  57. this.isbn = isbn;
  58. }
  59. public String getTitle() {
  60. return title;
  61. }
  62. public void setTitle(String title) {
  63. this.title = title;
  64. }
  65. public String getDescription() {
  66. return description;
  67. }
  68. public void setDescription(String description) {
  69. this.description = description;
  70. }
  71. public Book() {
  72. }
  73. public Book(String isbn, String title, String desc) {
  74. this.isbn = isbn;
  75. this.title = title;
  76. description = desc;
  77. }
  78. }
  79.  
  80. <%@ page language="java" contentType="text/html; charset=UTF-8"
  81. pageEncoding="UTF-8"%>
  82. <!DOCTYPE html>
  83. <html>
  84. <head>
  85. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  86. <title>Library Viewer</title>
  87. </head>
  88. <body>
  89. <h1>Biblioteka viewer</h1>
  90. <form action="BookServlet" method="post">
  91. <input placeholder="ISBN" type="text" name="isbn">
  92. <br>
  93. <input placeholder="Tytuł" type="text" name="title">
  94. <br>
  95. <input placeholder="Opis" type="text" name="description">
  96. <br>
  97. Szukaj: <input type="radio" name="option" value="search"><br>
  98. Dodaj: <input type="radio" name="option" value="add"><br>
  99. Modyfikuj: <input type="radio" name="option" value="update"><br>
  100. Usuń: <input type="radio" name="option" value="delete"><br>
  101. <br>
  102. <input type="submit" value="Wyślij">
  103. </form>
  104.  
  105. </body>
  106. </html>
  107.  
  108. <%@page import="pl.javastart.model.Book"%>
  109. <%@ page language="java" contentType="text/html; charset=UTF-8"
  110. pageEncoding="UTF-8"%>
  111. <% Book book = (Book)request.getAttribute("book"); %>
  112. <!DOCTYPE html>
  113. <html>
  114. <head>
  115. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  116. <title>Wynik operacji</title>
  117. </head>
  118. <body>
  119. <h1>Wynik zapytania <%= request.getAttribute("option") %></h1>
  120. <p>W wyniku Twojego zapytania otrzymano następujacy wynik:</p>
  121. <p>Tytuł: <%= book.getTitle() %><br>
  122. ISBN: <%= book.getIsbn() %><br>
  123. Opis: <%= book.getDescription() %></p>
  124. </body>
  125. </html>
  126.  
  127. package pl.javastart.dao;
  128.  
  129. import java.sql.Connection;
  130. import java.sql.PreparedStatement;
  131. import java.sql.ResultSet;
  132. import java.sql.SQLException;
  133.  
  134. import pl.javastart.model.Book;
  135. import pl.javastart.util.ConnectionProvider;
  136.  
  137. public class BookDao {
  138.  
  139. private final static String CREATE = "INSERT INTO book(isbn, title, description) VALUES(?, ?, ?);";
  140. private final static String READ = "SELECT isbn, title, description FROM book WHERE isbn = ?;";
  141. private final static String UPDATE = "UPDATE book SET isbn=?, title=?, description=? WHERE isbn = ?;";
  142. private final static String DELETE = "DELETE FROM book WHERE isbn=?;";
  143.  
  144. public void create(Book book) throws SQLException {
  145. try (Connection conn = ConnectionProvider.getConnection();
  146. PreparedStatement prepStmt = conn.prepareStatement(CREATE)) {
  147. prepStmt.setString(1, book.getIsbn());
  148. prepStmt.setString(2, book.getTitle());
  149. prepStmt.setString(3, book.getDescription());
  150. prepStmt.executeUpdate();
  151. }
  152. }
  153.  
  154. public Book read(String isbn) throws SQLException {
  155. Book resultBook = null;
  156. try (Connection conn = ConnectionProvider.getConnection();
  157. PreparedStatement prepStmt = conn.prepareStatement(READ);) {
  158. prepStmt.setString(1, isbn);
  159. ResultSet resultSet = prepStmt.executeQuery();
  160. if (resultSet.next()) {
  161. resultBook = new Book();
  162. resultBook.setIsbn(resultSet.getString("isbn"));
  163. resultBook.setTitle(resultSet.getString("title"));
  164. resultBook.setDescription(resultSet.getString("description"));
  165. }
  166. }
  167. return resultBook;
  168. }
  169.  
  170. public void update(Book book) throws SQLException {
  171. try (Connection conn = ConnectionProvider.getConnection();
  172. PreparedStatement prepStmt = conn.prepareStatement(UPDATE);) {
  173. prepStmt.setString(1, book.getIsbn());
  174. prepStmt.setString(2, book.getTitle());
  175. prepStmt.setString(3, book.getDescription());
  176. prepStmt.setString(4, book.getIsbn());
  177. prepStmt.executeUpdate();
  178. }
  179. }
  180.  
  181. public void delete(Book book) throws SQLException {
  182. try (Connection conn = ConnectionProvider.getConnection();
  183. PreparedStatement prepStmt = conn.prepareStatement(DELETE);) {
  184. prepStmt.setString(1, book.getIsbn());
  185. prepStmt.executeUpdate();
  186. }
  187. }
  188.  
  189. }
  190.  
  191. package pl.javastart.controller;
  192.  
  193. import java.io.IOException;
  194. import java.sql.SQLException;
  195.  
  196. import javax.servlet.ServletException;
  197. import javax.servlet.annotation.WebServlet;
  198. import javax.servlet.http.HttpServlet;
  199. import javax.servlet.http.HttpServletRequest;
  200. import javax.servlet.http.HttpServletResponse;
  201.  
  202. import pl.javastart.dao.BookDao;
  203. import pl.javastart.model.Book;
  204.  
  205. @WebServlet("/BookServlet")
  206. public class BookServlet extends HttpServlet {
  207. private static final long serialVersionUID = 1L;
  208.  
  209. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  210. throws ServletException, IOException {
  211. request.setCharacterEncoding("UTF-8");
  212. String isbn = request.getParameter("isbn");
  213. String title = request.getParameter("title");
  214. String description = request.getParameter("description");
  215. String option = request.getParameter("option");
  216. try {
  217. BookDao dao = new BookDao();
  218. Book book = null;
  219. String operation = null;
  220. if ("search".equals(option)) {
  221. book = dao.read(isbn);
  222. operation = "search";
  223. } else if ("add".equals(option)) {
  224. book = new Book(isbn, title, description);
  225. dao.create(book);
  226. operation = "add";
  227. } else if ("update".equals(option)) {
  228. book = new Book(isbn, title, description);
  229. dao.update(book);
  230. operation = "update";
  231. } else if ("delete".equals(option)) {
  232. book = new Book(isbn, title, description);
  233. dao.delete(book);
  234. operation = "delete";
  235. }
  236. request.setAttribute("option", operation);
  237. request.setAttribute("book", book);
  238. request.getRequestDispatcher("result.jsp").forward(request, response);
  239. } catch (SQLException e) {
  240. e.printStackTrace();
  241. request.getRequestDispatcher("error.jsp").forward(request, response);
  242. }
  243. }
  244.  
  245. }
  246.  
  247. <%@ page language="java" contentType="text/html; charset=UTF-8"
  248. pageEncoding="UTF-8"%>
  249. <!DOCTYPE html>
  250. <html>
  251. <head>
  252. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  253. <title>Upss</title>
  254. </head>
  255. <body>
  256. <h1>Coś poszło nie tak</h1>
  257. <a href="index.jsp">Spróbuj ponownie</a>
  258. </body>
  259. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement