Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="UTF-8"?>
- <Context>
- <Resource name="jdbc/library"
- auth="Container"
- type="javax.sql.DataSource"
- initialSize="10"
- maxTotal="100"
- maxIdle="30"
- maxWaitMillis="100"
- username="root"
- password="admin"
- driveClassName="com.myqsl.jdbc.Driver"
- url="jdbc:mysql://localhost:3306/library?useSSl=false&serverTimizone=UTC"/>
- </Context>
- package pl.javastart.util;
- import java.sql.Connection;
- import java.sql.SQLException;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.sql.DataSource;
- public class ConnectionProvider {
- private static DataSource dataSource;
- public static Connection getConnection() throws SQLException {
- return getDSInstance().getConnection();
- }
- private static DataSource getDSInstance() {
- if (dataSource == null) {
- try {
- Context initContext = new InitialContext();
- Context envContext = (Context) initContext.lookup("java:comp/env");
- dataSource = (DataSource) envContext.lookup("jdbc/library");
- } catch (NamingException e) {
- e.printStackTrace();
- }
- }
- return dataSource;
- }
- }
- package pl.javastart.model;
- public class Book {
- private String isbn;
- private String title;
- private String description;
- public String getIsbn() {
- return isbn;
- }
- public void setIsbn(String isbn) {
- this.isbn = isbn;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getDescription() {
- return description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
- public Book() {
- }
- public Book(String isbn, String title, String desc) {
- this.isbn = isbn;
- this.title = title;
- description = desc;
- }
- }
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Library Viewer</title>
- </head>
- <body>
- <h1>Biblioteka viewer</h1>
- <form action="BookServlet" method="post">
- <input placeholder="ISBN" type="text" name="isbn">
- <br>
- <input placeholder="Tytuł" type="text" name="title">
- <br>
- <input placeholder="Opis" type="text" name="description">
- <br>
- Szukaj: <input type="radio" name="option" value="search"><br>
- Dodaj: <input type="radio" name="option" value="add"><br>
- Modyfikuj: <input type="radio" name="option" value="update"><br>
- Usuń: <input type="radio" name="option" value="delete"><br>
- <br>
- <input type="submit" value="Wyślij">
- </form>
- </body>
- </html>
- <%@page import="pl.javastart.model.Book"%>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <% Book book = (Book)request.getAttribute("book"); %>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Wynik operacji</title>
- </head>
- <body>
- <h1>Wynik zapytania <%= request.getAttribute("option") %></h1>
- <p>W wyniku Twojego zapytania otrzymano następujacy wynik:</p>
- <p>Tytuł: <%= book.getTitle() %><br>
- ISBN: <%= book.getIsbn() %><br>
- Opis: <%= book.getDescription() %></p>
- </body>
- </html>
- package pl.javastart.dao;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import pl.javastart.model.Book;
- import pl.javastart.util.ConnectionProvider;
- public class BookDao {
- private final static String CREATE = "INSERT INTO book(isbn, title, description) VALUES(?, ?, ?);";
- private final static String READ = "SELECT isbn, title, description FROM book WHERE isbn = ?;";
- private final static String UPDATE = "UPDATE book SET isbn=?, title=?, description=? WHERE isbn = ?;";
- private final static String DELETE = "DELETE FROM book WHERE isbn=?;";
- public void create(Book book) throws SQLException {
- try (Connection conn = ConnectionProvider.getConnection();
- PreparedStatement prepStmt = conn.prepareStatement(CREATE)) {
- prepStmt.setString(1, book.getIsbn());
- prepStmt.setString(2, book.getTitle());
- prepStmt.setString(3, book.getDescription());
- prepStmt.executeUpdate();
- }
- }
- public Book read(String isbn) throws SQLException {
- Book resultBook = null;
- try (Connection conn = ConnectionProvider.getConnection();
- PreparedStatement prepStmt = conn.prepareStatement(READ);) {
- prepStmt.setString(1, isbn);
- ResultSet resultSet = prepStmt.executeQuery();
- if (resultSet.next()) {
- resultBook = new Book();
- resultBook.setIsbn(resultSet.getString("isbn"));
- resultBook.setTitle(resultSet.getString("title"));
- resultBook.setDescription(resultSet.getString("description"));
- }
- }
- return resultBook;
- }
- public void update(Book book) throws SQLException {
- try (Connection conn = ConnectionProvider.getConnection();
- PreparedStatement prepStmt = conn.prepareStatement(UPDATE);) {
- prepStmt.setString(1, book.getIsbn());
- prepStmt.setString(2, book.getTitle());
- prepStmt.setString(3, book.getDescription());
- prepStmt.setString(4, book.getIsbn());
- prepStmt.executeUpdate();
- }
- }
- public void delete(Book book) throws SQLException {
- try (Connection conn = ConnectionProvider.getConnection();
- PreparedStatement prepStmt = conn.prepareStatement(DELETE);) {
- prepStmt.setString(1, book.getIsbn());
- prepStmt.executeUpdate();
- }
- }
- }
- package pl.javastart.controller;
- import java.io.IOException;
- import java.sql.SQLException;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import pl.javastart.dao.BookDao;
- import pl.javastart.model.Book;
- @WebServlet("/BookServlet")
- public class BookServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- request.setCharacterEncoding("UTF-8");
- String isbn = request.getParameter("isbn");
- String title = request.getParameter("title");
- String description = request.getParameter("description");
- String option = request.getParameter("option");
- try {
- BookDao dao = new BookDao();
- Book book = null;
- String operation = null;
- if ("search".equals(option)) {
- book = dao.read(isbn);
- operation = "search";
- } else if ("add".equals(option)) {
- book = new Book(isbn, title, description);
- dao.create(book);
- operation = "add";
- } else if ("update".equals(option)) {
- book = new Book(isbn, title, description);
- dao.update(book);
- operation = "update";
- } else if ("delete".equals(option)) {
- book = new Book(isbn, title, description);
- dao.delete(book);
- operation = "delete";
- }
- request.setAttribute("option", operation);
- request.setAttribute("book", book);
- request.getRequestDispatcher("result.jsp").forward(request, response);
- } catch (SQLException e) {
- e.printStackTrace();
- request.getRequestDispatcher("error.jsp").forward(request, response);
- }
- }
- }
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>Upss</title>
- </head>
- <body>
- <h1>Coś poszło nie tak</h1>
- <a href="index.jsp">Spróbuj ponownie</a>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement