Advertisement
Guest User

Ativ 3

a guest
Oct 1st, 2017
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.97 KB | None | 0 0
  1. DUMP SQL
  2.  
  3. CREATE TABLE `clientes` (  `codigo` int(11) NOT NULL AUTO_INCREMENT,  `nome` varchar(255) DEFAULT NULL,  `telefone` varchar(11) DEFAULT '',  `email` varchar(50) DEFAULT NULL,  PRIMARY KEY (`codigo`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  4.  
  5. INSERT INTO `clientes` (`codigo`, `nome`, `telefone`, `email`)VALUES  (123124,'GUilherme','1999234','aowdiaj@awd.com'), (123125,'Teste','123213','dadawda@aw.com');
  6.  
  7. ---
  8. clientes.DataSource.java:
  9.  
  10. package clientes;
  11.  
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.SQLException;
  15.  
  16. public class DataSource {
  17.   private static Connection con = null;
  18.   private static final String DRIVER = "com.mysql.jdbc.Driver";
  19.   private static final String URL = "jdbc:mysql://127.0.0.1:3306/sistema";
  20.   private static final String USER = "root";
  21.   private static final String PASS = "123456";
  22.  
  23.     public static Connection getConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  24.         if (con == null){
  25.           Class.forName(DRIVER).newInstance();
  26.           try {
  27.         con = DriverManager.getConnection(URL, USER, PASS);
  28.       } catch (SQLException e) {
  29.         e.printStackTrace();
  30.       }      
  31.         }
  32.        
  33.         return con;
  34.  
  35.     }
  36.  
  37. }
  38.  
  39. -------
  40.  
  41. clientes.Clientes.java
  42.  
  43. package clientes;
  44.  
  45. import java.io.IOException;
  46. import java.sql.SQLException;
  47.  
  48. import javax.servlet.RequestDispatcher;
  49. import javax.servlet.ServletException;
  50. import javax.servlet.annotation.WebServlet;
  51. import javax.servlet.http.HttpServlet;
  52. import javax.servlet.http.HttpServletRequest;
  53. import javax.servlet.http.HttpServletResponse;
  54.  
  55.  
  56. @WebServlet("/")
  57. public class Clientes extends HttpServlet {
  58.   private static final long serialVersionUID = 1L;
  59.        
  60.  
  61.     public Clientes() {
  62.     }
  63.    
  64.   public static final String INSERIR = "/inserir.jsp";
  65.   public static final String LISTAGEM = "/listagem.jsp";
  66.    
  67.   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  68.     String forward = "";
  69.     String action = request.getParameter("action");
  70.    
  71.       if(action != null && action.equalsIgnoreCase("inserir")) {
  72.       forward = INSERIR;
  73.     } else {
  74.       try {
  75.         forward = LISTAGEM;
  76.         request.setAttribute("clientes", ClienteDao.todoClientes());
  77.       } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  78.         e.printStackTrace();
  79.       }
  80.     }
  81.      
  82.       RequestDispatcher view = request.getRequestDispatcher(forward);
  83.     view.forward(request, response);
  84.   }
  85.  
  86.  
  87.   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  88.     Cliente cliente = new Cliente();
  89.     cliente.setNome(request.getParameter("nome"));
  90.     cliente.setEmail(request.getParameter("email"));
  91.     cliente.setTelefone(request.getParameter("telefone"));
  92.  
  93.       try {
  94.         ClienteDao.inserirCliente(cliente);
  95.       } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  96.         e.printStackTrace();
  97.       }
  98.  
  99.     RequestDispatcher view = request.getRequestDispatcher(LISTAGEM);
  100.    
  101.     try {
  102.       request.setAttribute("clientes", ClienteDao.todoClientes());
  103.     } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
  104.       // TODO Auto-generated catch block
  105.       e.printStackTrace();
  106.     }
  107.     view.forward(request, response);
  108.   }
  109.  
  110. }
  111.  
  112.  
  113. ----
  114.  
  115. clientes.Cliente.java
  116.  
  117. package clientes;
  118.  
  119. public class Cliente {
  120.   private String nome;
  121.   private String email;
  122.   private int codigo;
  123.   private String telefone;
  124.  
  125.   public void setCodigo(int codigo) {
  126.     this.codigo = codigo;
  127.   }
  128.  
  129.   public void setNome(String nome) {
  130.     this.nome = nome;
  131.   }
  132.  
  133.   public void setEmail(String email) {
  134.     this.email = email;
  135.   }
  136.  
  137.   public void setTelefone(String telefone) {
  138.     this.telefone = telefone;
  139.   }
  140.  
  141.   public String getNome() {
  142.     return nome;
  143.   }
  144.  
  145.   public String getEmail() {
  146.     return email;
  147.   }
  148.  
  149.   public int getCodigo() {
  150.     return codigo;
  151.   }
  152.  
  153.   public String getTelefone() {
  154.     return telefone;
  155.   }
  156. }
  157.  
  158. ---
  159. clientes.ClienteDao.java
  160.  
  161. package clientes;
  162.  
  163. import java.sql.ResultSet;
  164. import java.sql.SQLException;
  165. import java.util.ArrayList;
  166. import java.util.List;
  167.  
  168. import com.mysql.jdbc.PreparedStatement;
  169.  
  170. public class ClienteDao {
  171.  
  172.   public ClienteDao() {
  173.   }
  174.  
  175.   public static List<Cliente> todoClientes() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  176.         List<Cliente> clientes = new ArrayList<Cliente>();
  177.         try {
  178.        
  179.           PreparedStatement stm = (PreparedStatement) DataSource.getConnection().prepareStatement("select * from clientes");
  180.  
  181.           ResultSet rs;
  182.          
  183.           rs = stm.executeQuery();
  184.          
  185.           while(rs.next()) {
  186.             Cliente cliente = new Cliente();
  187.             cliente.setCodigo(rs.getInt("codigo"));
  188.             cliente.setNome(rs.getString("nome"));
  189.             cliente.setEmail(rs.getString("email"));
  190.             cliente.setTelefone(rs.getString("telefone"));
  191.             clientes.add(cliente);
  192.           }
  193.  
  194.         } catch (SQLException e) {
  195.             e.printStackTrace();
  196.         }
  197.  
  198.         return clientes;
  199.     }
  200.  
  201.   public static void inserirCliente(Cliente cliente) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
  202.     try {
  203.       String query = "insert into clientes (codigo, nome, email, telefone) values (?,?,?,?)";
  204.       PreparedStatement preparedStatement = (PreparedStatement) DataSource.getConnection().prepareStatement( query );
  205.       preparedStatement.setInt( 1, cliente.getCodigo() );
  206.       preparedStatement.setString( 2, cliente.getNome() );
  207.       preparedStatement.setString( 3, cliente.getEmail() );
  208.       preparedStatement.setString( 4, cliente.getTelefone() );
  209.       preparedStatement.executeUpdate();
  210.       preparedStatement.close();
  211.     } catch (SQLException e) {
  212.       e.printStackTrace();
  213.     }
  214.   }
  215.  
  216. }
  217.  
  218.  
  219. ---
  220.  
  221. inserir.jsp
  222.  
  223. <%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.List"
  224.     import="clientes.Cliente"
  225.     pageEncoding="UTF-8"%>
  226. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  227. <html>
  228. <head>
  229. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  230. <title>Insert title here</title>
  231. </head>
  232. <body>
  233.  <h1>Inserir de cliente</h1>
  234.  
  235.  <form action="Clientes" method="post">
  236.   <fieldset>
  237.     <div>
  238.     <label for="nome">Nome</label>
  239.     <input type="text" name="nome" />
  240.     </div>
  241.     <div>
  242.     <label for="telefone">Telefone</label>
  243.     <input type="text" name="telefone" />
  244.     </div>
  245.     <div>
  246.     <label for="email">Email</label>
  247.     <input type="text" name="email" />
  248.     </div>
  249.   </fieldset>
  250.   <button type="submit">Cadastrar</button>
  251.  </form>
  252.  
  253.  
  254. </body>
  255. </html>
  256.  
  257. ----
  258. listagem.jsp
  259.  
  260. <%@ page language="java" contentType="text/html; charset=UTF-8"
  261. import="clientes.ClienteDao" import="java.util.List"
  262.     import="clientes.Cliente"
  263.     pageEncoding="UTF-8"%>
  264.     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
  265. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  266. <html>
  267. <head>
  268. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  269. <title>Insert title here</title>
  270. </head>
  271. <body>
  272.  <h1>Lista de clientes</h1>
  273.  <a href="/Clientes?action=inserir">Add Student</a>
  274.  <table>
  275.  <thead>
  276.  <th>Codigo</th>
  277.  <th>Nome</th>
  278.  <th>Telefone</th>
  279.  <th>Email</th>
  280.  </thead>
  281.   <tbody>
  282.   <c:forEach items="${clientes}" var="cliente">
  283.         <tr>
  284.           <td><c:out value="${cliente.codigo}" /></td>
  285.           <td><c:out value="${cliente.nome}" /></td>
  286.           <td><c:out value="${cliente.telefone}" /></td>
  287.           <td><c:out value="${cliente.email}" /></td>
  288.         </tr>
  289. </c:forEach>
  290.         </tbody>
  291.         </table>
  292. </body>
  293. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement