Home>
code
// specify package
package movie;
import java.util.*;
import java.sql.*;
public class Model {
    private String q;
    private ArrayList<TitleListResult>results;
        private Connection conn = null;
        private PreparedStatement stmt = null;
        private ResultSet resultSet = null;

        public void execute(){
    System.out.println("Hello");
                   try {
                conn = ConnectUtilMy.connectDatabase();
            TitleListResult result = new TitleListResult();
            // Find address
            PreparedStatement pstmt = conn.prepareStatement(
                "SELECT title_name,years_publication,performance_income,production_cost,public_evaluation,screening_time,genre_name,original_name,cast_name,directed_name "+
                     "FROM movie inner join original_list on movie.original_number=original_list.original_number "+
"inner join genre_list on movie.genre_number=genre_list.genre_number "+
"inner join directed_list on movie.directed_number=directed_list.directed_number "+
"inner join cast_list on movie.cast_number=cast_list.cast_number where title_name like? or directed_name like ?");
            pstmt.setString(1, q + "%");
            pstmt.setMaxRows(50);
            ResultSet rs = pstmt.executeQuery();
            // Get search results
            results = new ArrayList<String[]>();
            while (rs.next()) {
                String[] result = new String[10];
                result[0] = rs.getString("movie_number");
                result[1] = rs.getString("years_publication");
                result[2] = rs.getString("performance_income");
                result[3] = rs.getString("production_cost");
                                result[4] = rs.getString("public_evaluation");
                                result[5] = rs.getString("screening_time");
                                result[6] = rs.getString("genre_number");
                                result[7] = rs.getString("directed_number");
                                result[8] = rs.getString("original_number");
                                result[9] = rs.getString("cast_number");results.add(result);
            }
            // disconnect
            rs.close();
            pstmt.close();
            conn.close();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    public ArrayList<TitleListResult>getResults() {
        return results;
    }
    public void setQ(String q) {
        this.q = q;
    }
     // This is the place I added it.
Public void setC(String c) {
        this.c = c;
    }
}
// specify package
package movie;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
// Specify the servlet name (here, the class name Controller)
@WebServlet(name = "Controller", urlPatterns = {"/Controller"})
public class Controller extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // Get the value of the search key keyNum entered in view.jsp
        String q = request.getParameter("keyNum");
                q = new String(q.getBytes("8859_1"), "Windows-31J");
        if (q != null) {
            // Create an instance of Model class
            Model model = new Model();
            // set the search keymodel.setQ(q);
            // perform a search
            model.execute();
            // get search results
            ArrayList<String[]>results = model.getResults();
            // pass the search results to view.jsp
            request.setAttribute("results", results);
        }
        // forward the process to view.jsp
        getServletContext().getRequestDispatcher("/view.jsp").forward(request, response);
    }
}
code

I have created a system that creates a database with sql and searches it on the web using java.

Controller.java
DataAccess.java
Model.java
TitleListResult.java
ConnectUtilMy.java

Five of
It uses a file called view.jsp.

In an actual system, the database will be searched using the information entered on the screen. First, search in the Controller program.
Keyword and title name, director name, starring name or whichever
As a piece of information to identify, you can set a fixed value and let the Model class search for it.

"The system under development specifies the search keyword,
Is a title name, director name, starring name, or any of them.
It is like.
In order to realize this specification, it is executed in the current Model.java
Change the SELECT statement. ]

Said

[In order to execute the SELECT statement corresponding to this specification, Model.java is
Naturally, the entered search keyword and the title name or director name or starring
You need to get the information that identifies the name or which one.
The method to acquire is basically the method used in the current Model class.
Is good.
In other words, make a method like setQ, and make a class of Controller
Please call from. ]

So I will create another method and call it from the Controller class.
What should I do?

I tried adding the part that was already written, but if I write anything else
Is it possible to call a method because it has been created?

  • Answer # 1

    Thinking or doing

    // This is the place I added it.
    public void setC(String c) {
        this.c = c;
    }


    So I don't think there is any problem

    The call is also as already written

              // Create an instance of Model class
                Model model = new Model();
                // set the search key
                model.setQ(q);
                // Set the search type (title name, director name, lead name, any)
                model.setC(c);
                // perform a search
                model.execute();

    It should be close to.
    Create a method (setC) in Model class and call it from another class (Controller).
    I think it has been realized. (Although we may consider the ease of use or the method name)