Advertisement
Guest User

aula06

a guest
Mar 23rd, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.14 KB | None | 0 0
  1. //--------------FORM.JSP
  2. <html>
  3.     <head>
  4.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5.         <title>JSP Cadastro</title>
  6.    
  7.     </head>
  8.     <body>
  9.          
  10.        
  11.       <form name="form" action="noticia.do" method="post">
  12.           <h1>Cadastrar noticia</h1>
  13.             <textarea name="noticia" rows="4" cols="30"></textarea></br>
  14.                 <input type="submit" value="Enviar" >
  15.                 <input type="reset" value="Limpar">
  16.       </form>
  17.     </body>
  18. </html>
  19.  
  20. //--------------
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  22. //--------------LSITA.JSP
  23.  
  24. <%@page import="br.com.DAO.ListarNoticiasDAO"%>
  25. <%@page import="br.com.DAO.CadastrarNoticiaDAO"%>
  26. <%@page import="java.util.ArrayList"%>
  27. <%@page import="br.com.bean.Noticia"%>
  28. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  29. <!DOCTYPE html>
  30. <html>
  31.     <head>
  32.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  33.         <title>JSP Page</title>
  34.     </head>
  35.     <body>
  36.     <center>
  37. <%
  38.           String codigo = "";
  39.           String noticia = "";
  40.           String vcontador ="";
  41.          
  42.           String ac = (String) request.getAttribute("relatorio");
  43.            
  44.              ListarNoticiasDAO cmd = new ListarNoticiasDAO();
  45.              Noticia n =  new Noticia();
  46.            
  47.              ArrayList<Noticia> ms = cmd.listarNoticias() ;  
  48.              
  49.             for (int i = 0; i < ms.size(); i++) {                
  50.               n = ms.get(i);
  51.               codigo = String.valueOf(n.getCodigo());
  52.               noticia = String.valueOf(n.getNoticia());
  53.               vcontador = String.valueOf(n.getMostrarData());
  54.               %><table border=0.3 cellpadding=40>
  55.               <tr>
  56.              
  57.                   <td width='100px'>Noticia:</br> <%=noticia%></td>
  58.                   <td>data: <%=vcontador%></td>
  59.               </tr>
  60.             <%}
  61.         %>
  62.          
  63. </table>
  64.     </center>
  65.     </body>
  66. </html>
  67.  
  68. //--------------
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  70. //--------------CADASTRARNOTICIADAO.JAVA
  71.  
  72. package br.com.DAO;
  73.  
  74. import br.com.bean.Noticia;
  75. import java.sql.PreparedStatement;
  76. import java.util.Calendar;
  77.  
  78. public class CadastrarNoticiaDAO extends DAO{
  79.   public void inserir(Noticia n) throws Exception {
  80.     try {
  81.     abrirBanco();
  82.     String query = "INSERT INTO noticia (codigo,noticia,datanoticia) values(null,?,?)";
  83.     pst=(PreparedStatement) con.prepareStatement(query);
  84.     pst.setString(1,n.getNoticia());
  85.     pst.setDate(2, new java.sql.Date
  86.     (Calendar.getInstance().getTimeInMillis()));
  87.     pst.execute();
  88.     fecharBanco();
  89.     } catch (Exception e) {
  90.         System.out.println("Erro " + e.getMessage());
  91.     }
  92.     }    
  93. }
  94.  
  95. //--------------
  96. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  97. //--------------LISTARNOTICIADAO.JAVA
  98.  
  99. package br.com.DAO;
  100. import br.com.bean.Noticia;
  101. import java.sql.ResultSet;
  102. import java.util.ArrayList;
  103.  
  104. public class ListarNoticiasDAO extends DAO {
  105.     public ArrayList<Noticia> listarNoticias () throws Exception {
  106.        ArrayList<Noticia> listar = new ArrayList<>();
  107.          try{
  108.          abrirBanco();
  109.          String query = "select * FROM noticia order by(codigo)desc limit 0,10";
  110.          pst = con.prepareStatement(query);
  111.          ResultSet rs = pst.executeQuery();
  112.          Noticia ntbean ;
  113.          while (rs.next()){
  114.              ntbean = new Noticia();
  115.              ntbean.setCodigo(rs.getInt("codigo"));
  116.              ntbean.setNoticia(rs.getString("noticia"));
  117.              ntbean.setMostrarData(rs.getString("datanoticia"));
  118.              listar.add(ntbean);
  119.              }
  120.          fecharBanco();
  121.          }catch (Exception e){
  122.            System.out.println("Erro " + e.getMessage());
  123.      }
  124.        return listar;
  125.      }
  126. }  
  127.  
  128. //--------------
  129. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  130. //--------------DAO.JAVA
  131.  
  132. package br.com.DAO;
  133.  
  134. import java.sql.Connection;
  135. import java.sql.DriverManager;
  136. import java.sql.PreparedStatement;
  137. import java.sql.ResultSet;
  138. import java.sql.SQLException;
  139. import java.util.logging.Level;
  140. import java.util.logging.Logger;
  141.  
  142. public class DAO {
  143.   Connection con;
  144.     PreparedStatement pst;
  145.     ResultSet rs;
  146.     public void abrirBanco() throws SQLException {
  147.       try {
  148.         Class.forName("com.mysql.jdbc.Driver");
  149.         String url = "jdbc:mysql://localhost/aula6";
  150.         String user ="root";
  151.         String senha ="";  
  152.         con=(Connection) DriverManager.getConnection(url,user,senha);
  153.          System.out.println("Conectado ao banco de dados ");
  154.       } catch (ClassNotFoundException ex) {//tratamento de erro de drive
  155.           System.out.println("Classe não encontrada, adicione o driver nas bibliotecas.");
  156.         Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
  157.         } catch(SQLException e) {
  158.           System.out.println(e);
  159.           throw new RuntimeException(e);
  160.        }
  161.      }    
  162.    
  163.     public void fecharBanco() throws Exception{
  164.        if (pst!= null) {  
  165.             pst.close();
  166.             System.out.println("Execuçao da Query fechada\n");
  167.         }  
  168.     }  
  169. }
  170.  
  171. //--------------
  172. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  173. //--------------NOTICIA.JAVA
  174.  
  175. package br.com.bean;
  176.  
  177. import java.util.Calendar;
  178.  
  179. public class Noticia {
  180.     private int codigo;
  181.     private String noticia;
  182.     private Calendar datanoticia;
  183.     private String mostrarData;
  184.    
  185.     public String getMostrarData(){
  186.         return mostrarData;
  187.     }
  188.    
  189.     public void setMostrarData(String mostrarData){
  190.         this.mostrarData = mostrarData;
  191.     }
  192.    
  193.     public int getCodigo() {
  194.         return codigo;
  195.     }
  196.  
  197.     public void setCodigo(int codigo) {
  198.         this.codigo = codigo;
  199.     }
  200.  
  201.     public String getNoticia() {
  202.         return noticia;
  203.     }
  204.  
  205.     public void setNoticia(String noticia) {
  206.         this.noticia = noticia;
  207.     }
  208.    
  209.     public Calendar getDatanoticia() {
  210.         return datanoticia;
  211.     }
  212.    
  213.     public void setDatanoticia(Calendar datanoticia) {
  214.         this.datanoticia = datanoticia;
  215.     }  
  216. }
  217.  
  218. //--------------
  219. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
  220. //--------------SERVLETNOTICIA.JAVA
  221.  
  222. package br.com.controle;
  223.  
  224. import br.com.DAO.CadastrarNoticiaDAO;
  225. import br.com.bean.Noticia;
  226. import java.io.IOException;
  227. import java.io.PrintWriter;
  228. import javax.servlet.RequestDispatcher;
  229. import javax.servlet.ServletException;
  230. import javax.servlet.http.HttpServlet;
  231. import javax.servlet.http.HttpServletRequest;
  232. import javax.servlet.http.HttpServletResponse;
  233.  
  234.  
  235. public class NoticiaServlet extends HttpServlet {
  236.    
  237.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  238.             throws ServletException, IOException {
  239.         response.setContentType("text/html;charset=UTF-8");
  240.         try (PrintWriter out = response.getWriter()) {
  241.          
  242.            String nnoticia = request.getParameter("noticia");
  243.             Noticia  n = new Noticia();
  244.             n.setNoticia(nnoticia);
  245.            
  246.                 CadastrarNoticiaDAO dao = new CadastrarNoticiaDAO();
  247.                 try {
  248.                 dao.inserir(n);
  249.                 request.setAttribute("NOTICIA", n.getNoticia());
  250.  
  251.                 RequestDispatcher in =
  252.                 request.getRequestDispatcher("lista.jsp");
  253.                 in.forward(request, response);}
  254.                 catch(Exception e){
  255.                 }
  256.         }
  257.     }
  258. }
  259.  
  260. //--------------
  261. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  262. //--------------ENDOFPROGRAM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement