Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java question Objective: Write a program that manages a Movie database. Requirem

ID: 3571284 • Letter: J

Question

java question

Objective:

Write a program that manages a Movie database.

Requirements:

Each movie needs to have the follow attributes

Name

Year

Rating (1 to 5 stars)

Director

Box Office Gross

The Movie database must be able to

Add a movie

Remove a movie by title

Sort movie by Title

Sort movie by Rating

Sort movie by Box Office Gross

Show movies by a director

Print to a database file (you may define your own protocol)

Read from a database file

Write a front end that will allow a user to use any of the features noted in the database description

Explanation / Answer

First, install a RDBMS into your system like MySQL,oracle,postgreSQL etc. Then create a database and a table in that database.

Let us assume the database as test and table is named as movie.

In order to fulfill your requirements, we need to use a java framework called JSP(Java Server Pages).

For database Connection,

<%

Connection con = null; // An variable 'con' which is a Connection object initialized to null

Statement st = null; // This statement object is used to store database queries

Resultset rs = null; //This Resultset object is used to store results

Class.forName("com.mysql.jdbc.Driver"); //A mysql JDBC driver (Diff for oracle,postgreSQL,DB2)

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?"+"user=root&password=12345"); //databse connection initialization with getConection method and DriverManager class.The parameters used are name of the database, username, password.

%>

The below code is Front-end UI using HTML & CSS.

addmovie.jsp

<html>

<head>

<title>To Add Movie</title>

</head>

<body>

<form name = "addmovie" action = "insertmovie.jsp" method = "post">

<table align = "center">

<tr>
       <td>Movie Name</td>
       <td><input type="text" name="movie_name" maxlength="40" value=""></td>
   </tr>

<tr>
       <td>Year</td>
       <td><input type="text" name="year" maxlength="10" value=""></td>
   </tr>

<tr>
       <td>Rating</td>
       <td><input type="text" name="rating" maxlength="2" value="" maxvalue = "5"></td>
   </tr>

<tr>
       <td>Director</td>
       <td><input type="text" name="director" maxlength="30" value=""></td>
   </tr>

<tr>
       <td>Gross</td>
       <td><input type="text" name="gross" maxlength="15" value=""></td>
   </tr>

</table>
   <br/><br/><table align="center">
   <tr>
           <td><input type="button" value="SAVE" name="save"></td>
           <td><input type="reset" value="CLEAR"></td>  
       </tr>
       </table>
      
</form>          

</body>

</html>

The below code is used for adding the movie in a database.

insertmovie.jsp

<html>
<head>
  
<title>INSERTING</title>
  
</head>

  
<body>
<%
Connection con=null;
Statement st=null;
ResultSet rs=null;
int i=0,j=0;
  
   String movie_name = request.getParameter("movie_name");
   String year = request.getParameter("year");
int rating = Integer.ParseInt(request.getparameter("rating"));
   String director = request.getParameter("director");
double gross = Double.parseDouble(request.getParameter("gross"));
   Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?"+"user=root&password=12345");
   try
   {
       int count=0;
         
         
       if(btnName!=null)
       {
       if(btnName.equals("save"))
       {
       st = con.createStatement();
       String sql = "insert into movie (movie_name,year,rating,director,gross)"+ "values('"+movie_name+"','"+year+"','"+rating+"','"+director+"','"+gross+"')";
       i=st.executeUpdate(sql);
   response.sendRedirect("addmovie.jsp");
       }
       response.sendRedirect("addmovie.jsp");
   }
   }
   catch(Exception e)
   {
   e.printStackTrace();
   }
  
%>
  
  
</body>
</html>

The below files are used to remove the movie from database

remmovieform.jsp

<html>

<head>

</head>

<body>

<form name = "remmovie" action = "remmovie.jsp" method = "post">

Movie Name:<input type="text" name="movie_name" maxlength="40" value="">

<table align="center">
   <tr>
           <td><input type="button" value="REMOVE" name="remove"></td>
           <td><input type="reset" value="CLEAR"></td>  
       </tr>
       </table>
      
</form>

removemovie.jsp

<html>
<head>
  
<title>REMOVING</title>
  
</head>

  
<body>
<%
Connection con=null;
Statement st=null;
ResultSet rs=null;
int i=0,j=0;
  
   String movie_name = request.getParameter("movie_name");

   Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?"+"user=root&password=12345");
   try
   {
       int count=0;
         
         
       if(btnName!=null)
       {
       if(btnName.equals("remove"))
       {
       st = con.createStatement();
       String sql = "DELETE from movie (movie_name,year,rating,director,gross) where movie_name=movie_name";
       i=st.executeUpdate(sql);
   response.sendRedirect("remmovieform.jsp");
       }
       response.sendRedirect("remmoviefrom.jsp");
   }
   }
   catch(Exception e)
   {
   e.printStackTrace();
   }
  
%>
  
  
</body>
</html>

To sort movie by title, use this statement

Statement st = "select movie_name,year,rating,director,gross from movie order by movie_name";

To sort movie by rating,use this statement

Statement st = "select movie_name,year,rating,director,gross from movie order by rating";

To sort movie by gross,use this statement

Statement st = "select movie_name,year,rating,director,gross from movie order by gross";

To show movie by director,

Statement st = "select * from movie where director = director";