Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. Conexão
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.SQLException;
  5.  
  6. public class Conexao {
  7. private static final String URL="jdbc:mysql://localhost:3307/unp";
  8. private static final String DRIVER="com.mysql.jdbc.Driver";
  9. private static final String USUARIO="root";
  10. private static final String SENHA="usbw";
  11.  
  12. public static Connection getConexao() throws SQLException{
  13. try {
  14. Class.forName(DRIVER);
  15. System.out.println("Conectado ao banco");
  16. return DriverManager.getConnection(URL, USUARIO, SENHA);
  17. }
  18. catch (ClassNotFoundException e){
  19. throw new SQLException(e.getMessage());
  20. }
  21. }
  22. }
  23.  
  24.  
  25. Dao
  26. import java.sql.SQLException;
  27.  
  28. public interface Dao {
  29.  
  30. public void inserir(Object objeto) throws SQLException;
  31.  
  32. public Object consultar(String codigo) throws SQLException;
  33.  
  34. public void atualizar(Object objeto) throws SQLException;
  35.  
  36. public void excluir(String codigo) throws SQLException;
  37.  
  38. }
  39.  
  40. ClienteDao
  41. import java.sql.Connection;
  42. import java.sql.PreparedStatement;
  43. import java.sql.ResultSet;
  44. import java.sql.SQLException;
  45.  
  46. import unp.Conexao;
  47. import unp.model.Cliente;
  48.  
  49. public class ClienteDao implements Dao {
  50.  
  51. @Override
  52. public void inserir(Object objeto) throws SQLException {
  53. Cliente cliente = (Cliente) objeto;
  54.  
  55. Connection conexao = Conexao.getConexao();
  56.  
  57. String query = "INSERT INTO clientes (codigo, nome, email, telefone) VALUES (?,?,?,?)";
  58.  
  59. PreparedStatement ps = conexao.prepareStatement(query);
  60. ps.setString(1, cliente.getCodigo());
  61. ps.setString(2, cliente.getNome());
  62. ps.setString(3, cliente.getEmail());
  63. ps.setString(4, cliente.getTelefone());
  64. ps.execute();
  65. ps.close();
  66. }
  67.  
  68. @Override
  69. public Cliente consultar(String codigo) throws SQLException {
  70. // TODO Auto-generated method stub
  71.  
  72. Connection conexao = Conexao.getConexao();
  73.  
  74. PreparedStatement ps = conexao.prepareStatement("SELECT cli.* FROM clientes AS cli WHERE cli.codigo=?");
  75.  
  76. ps.setString(1, codigo);
  77. ResultSet rs = ps.executeQuery();
  78. rs.next();
  79.  
  80. Cliente cliente = new Cliente();
  81. cliente.setCodigo(rs.getString("codigo"));
  82. cliente.setNome(rs.getString("nome"));
  83. cliente.setEmail(rs.getString("email"));
  84. cliente.setTelefone(rs.getString("telefone"));
  85. return cliente;
  86. }
  87.  
  88. @Override
  89. public void atualizar(Object objeto) throws SQLException {
  90. // TODO Auto-generated method stub
  91.  
  92. }
  93.  
  94. @Override
  95. public void excluir(String codigo) throws SQLException {
  96. // TODO Auto-generated method stub
  97.  
  98. }
  99.  
  100. }
  101.  
  102. Cliente
  103. public class Cliente {
  104.  
  105. private String codigo;
  106. private String nome;
  107. private String email;
  108. private String telefone;
  109.  
  110. public void setCodigo(String codigo)
  111. {
  112. this.codigo = codigo;
  113. }
  114.  
  115. public String getCodigo()
  116. {
  117. return this.codigo;
  118. }
  119.  
  120. public void setNome(String nome)
  121. {
  122. this.nome = nome;
  123. }
  124.  
  125. public String getNome()
  126. {
  127. return this.nome;
  128. }
  129.  
  130. public void setEmail(String email)
  131. {
  132. this.email = email;
  133. }
  134.  
  135. public String getEmail()
  136. {
  137. return this.email;
  138. }
  139.  
  140. public void setTelefone(String telefone)
  141. {
  142. this.telefone = telefone;
  143. }
  144.  
  145. public String getTelefone()
  146. {
  147. return this.telefone;
  148. }
  149. }
  150.  
  151. Banco de Dados
  152. CREATE TABLE `clientes` (
  153. `codigo` varchar(10) NOT NULL,
  154. `nome` varchar(100) NOT NULL,
  155. `email` varchar(100) NOT NULL,
  156. `telefone` varchar(15) DEFAULT NULL,
  157. PRIMARY KEY (`codigo`),
  158. UNIQUE KEY `codigo_UNIQUE` (`codigo`)
  159. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  160.  
  161.  
  162. Cadastro
  163.  
  164. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  165. pageEncoding="ISO-8859-1"%>
  166. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  167. <html>
  168. <head>
  169. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  170. <title>Novo Cliente</title>
  171. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  172. </head>
  173. <body>
  174. <div class="container">
  175. <h1>Novo Cliente</h1>
  176.  
  177. <form method="POST" action="novo-cliente.jsp">
  178. <div class="form-group">
  179. <label for="codigo">Código</label>
  180. <input type="text" name="codigo" id="codigo" class="form-control">
  181. </div>
  182.  
  183. <div class="form-group">
  184. <label for="nome">Nome</label>
  185. <input type="text" name="nome" id="nome" class="form-control">
  186. </div>
  187.  
  188. <div class="form-group">
  189. <label class="email">E-mal:</label>
  190. <input type="text" name="email" id="email" class="form-control">
  191. </div>
  192.  
  193. <div class="form-group">
  194. <label class="telefone">Telefone:</label>
  195. <input type="text" name="telefone" id="telefone" class="form-control">
  196. </div>
  197.  
  198. <button type="submit" class="btn btn-primary">Cadastrar</button>
  199. </form>
  200. </div><!-- .container -->
  201. </body>
  202. </html>
  203.  
  204.  
  205. Visualização
  206. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  207. <html>
  208. <head>
  209. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  210. <title>Cliente</title>
  211. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  212. </head>
  213. <body>
  214. <div class="container">
  215.  
  216. <table class="table">
  217. <tbody>
  218. <tr>
  219. <th>Código:</th>
  220. <td><%= cliente.getCodigo() %></td>
  221. </tr>
  222. <tr>
  223. <th>Nome:</th>
  224. <td><%= cliente.getNome() %></td>
  225. </tr>
  226. <tr>
  227. <th>Email:</th>
  228. <td><%= cliente.getEmail() %></td>
  229. </tr>
  230. <tr>
  231. <th>Telefone:</th>
  232. <td><%= cliente.getTelefone() %></td>
  233. </tr>
  234. </tbody>
  235. </table>
  236.  
  237. <a href="<%= request.getContextPath() + "/novo-cliente.jsp" %>" class="btn btn-primary">Voltar</a>
  238.  
  239. </div><!-- .container -->
  240.  
  241. </body>
  242. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement