Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------FORM.JSP
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Cadastro</title>
- </head>
- <body>
- <form name="form" action="noticia.do" method="post">
- <h1>Cadastrar noticia</h1>
- <textarea name="noticia" rows="4" cols="30"></textarea></br>
- <input type="submit" value="Enviar" >
- <input type="reset" value="Limpar">
- </form>
- </body>
- </html>
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------LSITA.JSP
- <%@page import="br.com.DAO.ListarNoticiasDAO"%>
- <%@page import="br.com.DAO.CadastrarNoticiaDAO"%>
- <%@page import="java.util.ArrayList"%>
- <%@page import="br.com.bean.Noticia"%>
- <%@page contentType="text/html" pageEncoding="UTF-8"%>
- <!DOCTYPE html>
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>JSP Page</title>
- </head>
- <body>
- <center>
- <%
- String codigo = "";
- String noticia = "";
- String vcontador ="";
- String ac = (String) request.getAttribute("relatorio");
- ListarNoticiasDAO cmd = new ListarNoticiasDAO();
- Noticia n = new Noticia();
- ArrayList<Noticia> ms = cmd.listarNoticias() ;
- for (int i = 0; i < ms.size(); i++) {
- n = ms.get(i);
- codigo = String.valueOf(n.getCodigo());
- noticia = String.valueOf(n.getNoticia());
- vcontador = String.valueOf(n.getMostrarData());
- %><table border=0.3 cellpadding=40>
- <tr>
- <td width='100px'>Noticia:</br> <%=noticia%></td>
- <td>data: <%=vcontador%></td>
- </tr>
- <%}
- %>
- </table>
- </center>
- </body>
- </html>
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------CADASTRARNOTICIADAO.JAVA
- package br.com.DAO;
- import br.com.bean.Noticia;
- import java.sql.PreparedStatement;
- import java.util.Calendar;
- public class CadastrarNoticiaDAO extends DAO{
- public void inserir(Noticia n) throws Exception {
- try {
- abrirBanco();
- String query = "INSERT INTO noticia (codigo,noticia,datanoticia) values(null,?,?)";
- pst=(PreparedStatement) con.prepareStatement(query);
- pst.setString(1,n.getNoticia());
- pst.setDate(2, new java.sql.Date
- (Calendar.getInstance().getTimeInMillis()));
- pst.execute();
- fecharBanco();
- } catch (Exception e) {
- System.out.println("Erro " + e.getMessage());
- }
- }
- }
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------LISTARNOTICIADAO.JAVA
- package br.com.DAO;
- import br.com.bean.Noticia;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- public class ListarNoticiasDAO extends DAO {
- public ArrayList<Noticia> listarNoticias () throws Exception {
- ArrayList<Noticia> listar = new ArrayList<>();
- try{
- abrirBanco();
- String query = "select * FROM noticia order by(codigo)desc limit 0,10";
- pst = con.prepareStatement(query);
- ResultSet rs = pst.executeQuery();
- Noticia ntbean ;
- while (rs.next()){
- ntbean = new Noticia();
- ntbean.setCodigo(rs.getInt("codigo"));
- ntbean.setNoticia(rs.getString("noticia"));
- ntbean.setMostrarData(rs.getString("datanoticia"));
- listar.add(ntbean);
- }
- fecharBanco();
- }catch (Exception e){
- System.out.println("Erro " + e.getMessage());
- }
- return listar;
- }
- }
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------DAO.JAVA
- package br.com.DAO;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class DAO {
- Connection con;
- PreparedStatement pst;
- ResultSet rs;
- public void abrirBanco() throws SQLException {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://localhost/aula6";
- String user ="root";
- String senha ="";
- con=(Connection) DriverManager.getConnection(url,user,senha);
- System.out.println("Conectado ao banco de dados ");
- } catch (ClassNotFoundException ex) {//tratamento de erro de drive
- System.out.println("Classe não encontrada, adicione o driver nas bibliotecas.");
- Logger.getLogger(DAO.class.getName()).log(Level.SEVERE, null, ex);
- } catch(SQLException e) {
- System.out.println(e);
- throw new RuntimeException(e);
- }
- }
- public void fecharBanco() throws Exception{
- if (pst!= null) {
- pst.close();
- System.out.println("Execuçao da Query fechada\n");
- }
- }
- }
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------NOTICIA.JAVA
- package br.com.bean;
- import java.util.Calendar;
- public class Noticia {
- private int codigo;
- private String noticia;
- private Calendar datanoticia;
- private String mostrarData;
- public String getMostrarData(){
- return mostrarData;
- }
- public void setMostrarData(String mostrarData){
- this.mostrarData = mostrarData;
- }
- public int getCodigo() {
- return codigo;
- }
- public void setCodigo(int codigo) {
- this.codigo = codigo;
- }
- public String getNoticia() {
- return noticia;
- }
- public void setNoticia(String noticia) {
- this.noticia = noticia;
- }
- public Calendar getDatanoticia() {
- return datanoticia;
- }
- public void setDatanoticia(Calendar datanoticia) {
- this.datanoticia = datanoticia;
- }
- }
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////Q
- //--------------SERVLETNOTICIA.JAVA
- package br.com.controle;
- import br.com.DAO.CadastrarNoticiaDAO;
- import br.com.bean.Noticia;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.RequestDispatcher;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- public class NoticiaServlet extends HttpServlet {
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- String nnoticia = request.getParameter("noticia");
- Noticia n = new Noticia();
- n.setNoticia(nnoticia);
- CadastrarNoticiaDAO dao = new CadastrarNoticiaDAO();
- try {
- dao.inserir(n);
- request.setAttribute("NOTICIA", n.getNoticia());
- RequestDispatcher in =
- request.getRequestDispatcher("lista.jsp");
- in.forward(request, response);}
- catch(Exception e){
- }
- }
- }
- }
- //--------------
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //--------------ENDOFPROGRAM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement