Data management of owned books (portfolio)

I will post for the first time. I am a first year new graduate working for a small business. I am creating a portfolio for studying with advice from senior employees. If you misunderstand the meaning or function of the code you use, please give us your guidance. The development software used is "Eclipse". Bold is the code, // is the code below.

Portfolio contents ・ Simple management system for books you own (so-called My Library)

What you want to do with the code ・ Search for books you own

Files to use -Own.java (java file first entered from jsp)

Code Own.java **protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {**

Specify the types of the three data received by //ownBook.jsp. Use this method of String type for each sent data (data name after request.getParameter and data stored in it). Replace with data name. The title, author, and publisher of the book you want to search are sent. // I thought I was writing. When request.getParameter is satisfied if HttpServletRequest needs to be defined in the parent interface? Is the above HttpServletRequest OK? ↓

String bookTitleSerch = request.getParameter(“bookTitleSearch”); String bookWriterSerch= request.getParameter(“bookWriterSearch”); String bookCompanySerch= request.getParameter(“bookCompanySearch”);

// Create a book type ownBookList variable Create a data type called book because the data type you want to retrieve is not limited to String type only and int type only (If the type name is easy to understand because it is defined as void) Anything is fine) ArryList handles a collection of data like an array. To explain in detail by applying it, I created a method called ownBooksList of book type. ArrayList ownBooksList = new ArrayList();

// Exception handling Specify the place where the error will occur Why was it here? try { // Get the list = Information to be stored in the method Outsource the processing to Select of OwnDBAccess.java, and pass the above ownBookList variable as an argument at that time.

ownBooksList = OwnDBAccess.Select(bookTitleSerch, bookWriterSerch , bookCompanySerch); // Exception handling: Nothing is displayed on the front side when an error occurs } catch (SQLException e) { e.printStackTrace(); } // Set to request Save the instance using the request scope. This allows you to carry until you receive the three words you want to search for. request.setAttribute(“ownBookList”, ownBooksList);

// Specify the jsp file for which you want to display the requested setAttribute. Omitted due to a cliché. ServletContext context = getServletContext(); RequestDispatcher dis = context.getRequestDispatcher(“/OwnReslut.jsp”); dis.forward(request, response); }

・DBAccess.java public static ArrayList Select(String bookTitleSerch , String bookWriterSerch , String bookCompanySerch) throws SQLException { // Do not put anything in the dao variable of the Dao file Tomorrow, we will output about Dao. Define a Dao type variable (?). Dao is a file mainly for inputting commands related to database operations. ~~ The reason for null here is that you want to process it and receive what is sent in a broad sense. ~~ Because variables that suddenly appear cannot be processed unless variables are defined in the method.

Dao dao = null;

//上記に同意味。resultという名前の変数を作成、受け取りたいがためにnull。また、検索結果によって返ってくるデータが変わるため。 ArrayList result = null;

//例外処理。

try {

//daoインスタンスを作成。ここで作る意味は検査例外が発生する可能性がある処理を含むため。データベースとの接続を行い、SQlのパスワード等の不一致により例外が発生しやすい。 dao = new Dao();

//DaoファイルのOwnメソッドに引数として()内の変数を渡す。並びに受け取ったデータを上記result変数に代入

result = dao.Own(bookTitleSerch , bookWriterSerch , bookCompanySerch); }finally {

//リソースの開放。必要な情報のみを取り出す為。

if(dao != null) dao.close(); }

//一つ上のreslutに渡す。

return result; }

・Dao.java //以下のメソッドではデータベースの操作と取り出したいデータの指定を行っております。あと、例外処理がしたいのでthrowsを書いてもらいました。()内は引数としてDBAccessから送られてきた三つの変数です(受け取ったら戻り値になるんでしたっけ?)。

public ArrayList Own(String title , String writer , String company) throws SQLException{ // Fill in the processing to be performed in SQL This time, I want to retrieve all the data that matches the conditions of the books table, so the code is as follows. sql = “select * from books where title like ? and writer like ? and company like ?”;

//以下はSQLを実行するためのコードだったと思います(うろ覚え)。これ書かなきゃいけない理由は分かりません。調べてもこう動く、こう書くとしか出てきませんでした。ご教示よろしくお願いします。

PreparedStatement ps = null; //これが確か、dtoにつなげるためのコードだったと思います。結果が検索内容によって変わるのでnull。 ResultSet rs = null;

// Create List method Is the above sql method used? Enter the column name to be entered in ArrayList list = new ArrayList(); //例外処理ですね。起こりそうな例外を書きます。 try { // Set a placeholder I want to run SQL because there is still a prepareStatement ps = con.prepareStatement(sql);

//上記sql=の中の?に入る変数。昨日のファイルにもあるtitleという引数を入れます。 ps.setString(1, “%”+title+”%”);

//上記にほぼ同様の処理です。 ps.setString(2, “%”+writer+”%”);

//上記にほぼ同じです。 ps.setString(3, “%”+company+”%”);

// SQL execution rs = ps.executeQuery(); //検索したデータがすべて表示されるまで繰り返し取り出す while(rs.next()) { // Create item method Create an item instance of the dto file here. Jump to the dto file. book item = new book(); // Get the title of the book and store it in item item.setTitle(rs.getString(“title”)); //本の著者を取得、itemへ item.setWriter(rs.getString(“writer”)); //本の出版社を取得、itemへ item.setCompany(rs.getString(“company”)); //itemメソッドに格納 list.add(item); }

//これもリソースの開放 rs.close(); }finally {

//これもリソース(ry ps.close(); } //呼び出し元の上記listメソッドに返す return list; }

Dto //このファイルは今まで登場した三つの変数を一つの箱として結果に渡すダンボール箱の役割を持っています。変数を受け取ってもどこに格納するかを指定します(自信あり)。

//前言撤回です。こんな感じだろうなとかいたので細かいところは説明できません。

public class book implements Serializable{ private static final long serialVersionUID = 1L;

// Title public String title; // Famous public String writer; // the publisher public String company;

// Getter / Setter public String getTitle() { return title; }

public void setTitle(String title) { this.title = title; }

public String getWriter() { return writer; }

public void setWriter(String writer) { this.writer = writer; }

public String getCompany() { return company; }

public void setCompany(String company) { this.company =company ; }

}

For book search, here. Seniors, please point out your opinions and mistakes.

Recommended Posts

Data management of owned books (portfolio)
Create dummy data of portfolio with Faker [Note]
[Portfolio commentary] "Share-read" -Share and own the knowledge of books-
[Java] Simplify the implementation of data history management with Reladomo