Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DataBase:
- ***********************************************************************************************************************************
- CREATE TABLE IF NOT EXISTS `pelicula` (
- `id_pelicula` int(11) NOT NULL AUTO_INCREMENT,
- `title` varchar(50) DEFAULT NULL,
- `year` int(11) DEFAULT NULL,
- `url_imbd` varchar(50) DEFAULT NULL,
- PRIMARY KEY (`id_pelicula`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
- ***********************************************************************************************************************************
- Index.html
- <html>
- <head>
- <meta http-equiv="Refresh" content="0; url=http://localhost:8084/appPelicula/ServletIndex">
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body>
- </body>
- </html>
- ***********************************************************************************************************************************
- NuevaPelicula.html
- <html>
- <head>
- <title>Peliculas</title>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- </head>
- <body style="background:#0A0B0A">
- <center><h1 style="color:#1BE91B">Peliculas</h1></center>
- <form method="post" action="ServletNuevo">
- <table align="center">
- <tr><td style="color:#FFFFFF">Title:</td><td><input type="text" id="txttitle" name="txttitle"></td>
- <tr><td style="color:#FFFFFF">year:</td><td><input type="text" id="txtyear" name="txtyear"></td>
- <tr><td style="color:#FFFFFF">url_imdb:</td><td><input type="text" id="txturl_imbd" name="txturl_imbd"></td>
- <tr><td colspan="3" align="right"><input type="submit" value="Agregar Pelicula"></td></tr>
- </table>
- </form>
- </body>
- </html>
- ***********************************************************************************************************************************
- BaseDato.java
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- /**
- *
- * @author SnakeChaos
- */
- public class BaseDato {
- private boolean conectado;
- private Connection conex;
- public BaseDato () throws InstantiationException, IllegalAccessException {
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(BaseDato.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- conex = DriverManager.getConnection("jdbc:mysql://localhost:3306/peliculas", "root", "");
- conectado = true;
- }
- catch (SQLException ex) {
- conectado = false;
- }
- }
- public boolean isConectado() {
- return conectado;
- }
- public void insertar(TipoDato t) {
- Statement st=null;
- if (isConectado()) {
- try {
- st = conex.createStatement();
- String title = ((Pelicula) t).getTitle();
- int year = ((Pelicula) t).getYear();
- String url_imbd = ((Pelicula) t).getUrl_imbd();
- st.executeUpdate(((Pelicula) t).getData());
- }
- catch (SQLException ex) {
- System.err.println(ex.getMessage());
- }
- }
- }
- public ArrayList<TipoDato> getRegistros() {
- ArrayList<TipoDato> lista = new ArrayList<>();
- Statement st=null;
- ResultSet rs=null;
- if (isConectado()) {
- try {
- st = conex.createStatement();
- rs = st.executeQuery("SELECT * FROM pelicula");
- while (rs.next()) {
- lista.add(new Pelicula(rs.getInt("id_pelicula"),
- rs.getString("title"),
- rs.getInt("year"),
- rs.getString("url_imbd")
- ));
- }
- }
- catch (SQLException ex) {
- System.out.println(ex.getMessage());
- }
- }
- return lista;
- }
- public TipoDato getRegistro(int id_pelicula){
- TipoDato td = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance ();
- conex = DriverManager.getConnection("jdbc:mysql://localhost:3306/peliculas","root","");
- Statement st = conex.createStatement();
- ResultSet rs = st.executeQuery("SELECT * FROM pelicula WHERE id_pelicula='"+id_pelicula+"'");
- if (rs.first()) {
- td = new Pelicula(rs.getInt("id_pelicula"),
- rs.getString("title"),
- rs.getInt("year"),
- rs.getString("url_imbd"));
- }
- else{
- td = new Pelicula("",'s',"");
- }
- st.close();
- conex.close();
- }
- catch(Exception ex) {
- System.err.println(ex.getMessage());
- }
- return td;
- }
- public void Modificar(Pelicula p) {
- try {
- Statement st = conex.createStatement();
- st.executeUpdate(String.format(
- "UPDATE pelicula SET title='%s', year=%d ,url_imbd='%s' WHERE id_pelicula=%d",
- p.getTitle(), p.getYear(), p.getUrl_imbd(), p.getId_pelicula()));
- st.close();
- }
- catch (SQLException ex) {
- }
- catch (Exception ex) {
- }
- }
- }
- *********************************************************************************************************************************
- Pelicula.java
- import java.util.*;
- /**
- *
- * @author SnakeChaos
- */
- public class Pelicula implements TipoDato{
- private int id_pelicula;
- private String title;
- private int year;
- private String url_imbd;
- public Pelicula(String title, int year, String url_imbd) {
- this(0, title, year,url_imbd);
- }
- public Pelicula(int id_pelicula, String title, int year, String url_imbd) {
- this.id_pelicula = id_pelicula;
- this.title = title;
- this.year = year;
- this.url_imbd = url_imbd;
- }
- public int getId_pelicula() {
- return id_pelicula;
- }
- public void setId_pelicula(int id_pelicula) {
- this.id_pelicula = id_pelicula;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- public String getUrl_imbd() {
- return url_imbd;
- }
- public void setUrl_imbd(String url_imbd) {
- this.url_imbd = url_imbd;
- }
- @Override
- public String getData() {
- return String.format("insert into pelicula (title, year, url_imbd) values('%s', '%s' ,'%s')", title, year, url_imbd);
- }
- @Override
- public String toString() {
- return "Pelicula{" + "title=" + title + ", year=" + year + ", url_imbd=" + url_imbd + '}';
- }
- }
- *********************************************************************************************************************************
- ServletIndex.java
- package paquete;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author SnakeChaos
- */
- @WebServlet(name = "ServletIndex", urlPatterns = {"/ServletIndex"})
- public class ServletIndex extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet ServletIndex</title>");
- out.println("</head>");
- out.println("<body style=\"background:#0A0B0A\">");
- BaseDato bd = new BaseDato();
- out.println("<center><h1 style=\"color:#1BE91B\">Listado de Peliculas</h1></center>");
- out.println("<table bgcolor=\"#FFFFFF\" border=1 align='center'>");
- out.println("<tr><th colspan=3>Peliculas</th></tr>");
- out.println("<tr><th colspan=1>Title</th><th colspan=1>Year</th><th colspan=1>Url_imbd</th></tr>");
- for (TipoDato t: bd.getRegistros()) {
- Pelicula p = (Pelicula) t;
- out.println("<tr><td><a href=\"ServletModificar?id_pelicula=" +
- p.getId_pelicula() + "\">" +
- p.getTitle() + "</a></td>");
- out.println("<td>" + p.getYear() + "</td>");
- out.println("<td><a target=\"_self\" href='"+
- p.getUrl_imbd() + "'>"
- + p.getUrl_imbd() + "</a></td></tr>");
- }
- out.println("</table>");
- out.println("</br></br><center><a href=\"http://localhost:8084/appPelicula/NuevaPelicula.html\">Agregar Nueva Pelicula</a></center>");
- out.println("</body>");
- out.println("</html>");
- } catch (InstantiationException ex) {
- Logger.getLogger(ServletIndex.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(ServletIndex.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- *********************************************************************************************************************************
- ServletModificar.java
- package paquete;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author SnakeChaos
- */
- @WebServlet(name = "ServletModificar", urlPatterns = {"/ServletModificar"})
- public class ServletModificar extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet ServletModificar</title>");
- out.println("</head>");
- out.println("<body style=\"background:#0A0B0A\">");
- int id_pelicula = Integer.valueOf(request.getParameter("id_pelicula"));
- BaseDato bd = new BaseDato();
- Pelicula p = (Pelicula) bd.getRegistro(id_pelicula);
- out.println("<center><h1 style=\"color:#1BE91B\">Pelicula</h1></center>");
- out.println("<form action=\"/appPelicula/ServletModificarPelicula\" method=\"post\">");
- out.println("<table border='1' align='center'>");
- out.println("<input type=\"hidden\" name=\"id_pelicula\" value=\""
- + p.getId_pelicula() + "\">");
- out.println("<tr><td style=\"color:#FFFFFF\">Title</td><td align='right'><input type=\"text\" name=\"title\" value=\""
- + p.getTitle() + "\"></td></tr>");
- out.println("<tr><td style=\"color:#FFFFFF\">Year</td><td align='right'><input type=\"text\" name=\"year\" value=\""
- + p.getYear() + "\"></td></tr>");
- out.println("<tr><td style=\"color:#FFFFFF\">Url_imbd<td align='right'><input align='right' type=\"text\" name=\"url_imbd\" value=\""
- + p.getUrl_imbd() + "\"></td></tr>");
- out.println("</table>");
- out.println("<br><center><input type=\"submit\" value=\"ModificarDatos\"></center>");
- out.println("</form>");
- out.println("</body>");
- out.println("</html>");
- } catch (InstantiationException ex) {
- Logger.getLogger(ServletModificar.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(ServletModificar.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- *********************************************************************************************************************************
- ServletModificarPelicula.java
- package paquete;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- 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 java.sql.*;
- /**
- *
- * @author SnakeChaos
- */
- @WebServlet(name = "ServletModificarPelicula", urlPatterns = {"/ServletModificarPelicula"})
- public class ServletModificarPelicula extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- Pelicula p;
- int id_pelicula = Integer.valueOf(request.getParameter("id_pelicula"));
- int year = Integer.valueOf(request.getParameter("year"));
- p = new Pelicula(id_pelicula, request.getParameter("title"),year, request.getParameter("url_imbd"));
- BaseDato bd = new BaseDato();
- bd.Modificar(p);
- String site = new String("http://localhost:8084/appPelicula/ServletIndex");
- response.setStatus(response.SC_MOVED_TEMPORARILY);
- response.setHeader("Location", site);
- } catch (InstantiationException ex) {
- Logger.getLogger(ServletModificarPelicula.class.getName()).log(Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- Logger.getLogger(ServletModificarPelicula.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }
- }
- *********************************************************************************************************************************
- ServletNuevo.java
- package paquete;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author SnakeChaos
- */
- @WebServlet(name = "ServletNuevo", urlPatterns = {"/ServletNuevo"})
- public class ServletNuevo extends HttpServlet {
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Agregar Pelicula</title>");
- out.println("</head>");
- out.println("<body>");
- String title = request.getParameter("txttitle");
- int year = Integer.parseInt(request.getParameter("txtyear"));
- String url_imbd = request.getParameter("txturl_imbd");
- try {
- BaseDato bd = new BaseDato();
- Pelicula p = new Pelicula (title,year,url_imbd);
- System.out.println(p.getData());
- bd.insertar(p);
- // New location to be redirected
- String site = new String("http://localhost:8084/appPelicula/ServletIndex");
- response.setStatus(response.SC_MOVED_TEMPORARILY);
- response.setHeader("Location", site);
- }
- catch (InstantiationException ex) {
- out.println(ex.getMessage());
- } catch (IllegalAccessException ex) {
- out.println(ex.getMessage());
- }
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
- *********************************************************************************************************************************
- TipoDato.java
- package paquete;
- /**
- *
- * @author SnakeChaos
- */
- public interface TipoDato {
- public String getData();
- }
Advertisement
Add Comment
Please, Sign In to add comment