Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Conexão
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class Conexao {
- private static final String URL="jdbc:mysql://localhost:3307/unp";
- private static final String DRIVER="com.mysql.jdbc.Driver";
- private static final String USUARIO="root";
- private static final String SENHA="usbw";
- public static Connection getConexao() throws SQLException{
- try {
- Class.forName(DRIVER);
- System.out.println("Conectado ao banco");
- return DriverManager.getConnection(URL, USUARIO, SENHA);
- }
- catch (ClassNotFoundException e){
- throw new SQLException(e.getMessage());
- }
- }
- }
- Dao
- import java.sql.SQLException;
- public interface Dao {
- public void inserir(Object objeto) throws SQLException;
- public Object consultar(String codigo) throws SQLException;
- public void atualizar(Object objeto) throws SQLException;
- public void excluir(String codigo) throws SQLException;
- }
- ClienteDao
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import unp.Conexao;
- import unp.model.Cliente;
- public class ClienteDao implements Dao {
- @Override
- public void inserir(Object objeto) throws SQLException {
- Cliente cliente = (Cliente) objeto;
- Connection conexao = Conexao.getConexao();
- String query = "INSERT INTO clientes (codigo, nome, email, telefone) VALUES (?,?,?,?)";
- PreparedStatement ps = conexao.prepareStatement(query);
- ps.setString(1, cliente.getCodigo());
- ps.setString(2, cliente.getNome());
- ps.setString(3, cliente.getEmail());
- ps.setString(4, cliente.getTelefone());
- ps.execute();
- ps.close();
- }
- @Override
- public Cliente consultar(String codigo) throws SQLException {
- // TODO Auto-generated method stub
- Connection conexao = Conexao.getConexao();
- PreparedStatement ps = conexao.prepareStatement("SELECT cli.* FROM clientes AS cli WHERE cli.codigo=?");
- ps.setString(1, codigo);
- ResultSet rs = ps.executeQuery();
- rs.next();
- Cliente cliente = new Cliente();
- cliente.setCodigo(rs.getString("codigo"));
- cliente.setNome(rs.getString("nome"));
- cliente.setEmail(rs.getString("email"));
- cliente.setTelefone(rs.getString("telefone"));
- return cliente;
- }
- @Override
- public void atualizar(Object objeto) throws SQLException {
- // TODO Auto-generated method stub
- }
- @Override
- public void excluir(String codigo) throws SQLException {
- // TODO Auto-generated method stub
- }
- }
- Cliente
- public class Cliente {
- private String codigo;
- private String nome;
- private String email;
- private String telefone;
- public void setCodigo(String codigo)
- {
- this.codigo = codigo;
- }
- public String getCodigo()
- {
- return this.codigo;
- }
- public void setNome(String nome)
- {
- this.nome = nome;
- }
- public String getNome()
- {
- return this.nome;
- }
- public void setEmail(String email)
- {
- this.email = email;
- }
- public String getEmail()
- {
- return this.email;
- }
- public void setTelefone(String telefone)
- {
- this.telefone = telefone;
- }
- public String getTelefone()
- {
- return this.telefone;
- }
- }
- Banco de Dados
- CREATE TABLE `clientes` (
- `codigo` varchar(10) NOT NULL,
- `nome` varchar(100) NOT NULL,
- `email` varchar(100) NOT NULL,
- `telefone` varchar(15) DEFAULT NULL,
- PRIMARY KEY (`codigo`),
- UNIQUE KEY `codigo_UNIQUE` (`codigo`)
- ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
- Cadastro
- <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
- pageEncoding="ISO-8859-1"%>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Novo Cliente</title>
- <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <h1>Novo Cliente</h1>
- <form method="POST" action="novo-cliente.jsp">
- <div class="form-group">
- <label for="codigo">Código</label>
- <input type="text" name="codigo" id="codigo" class="form-control">
- </div>
- <div class="form-group">
- <label for="nome">Nome</label>
- <input type="text" name="nome" id="nome" class="form-control">
- </div>
- <div class="form-group">
- <label class="email">E-mal:</label>
- <input type="text" name="email" id="email" class="form-control">
- </div>
- <div class="form-group">
- <label class="telefone">Telefone:</label>
- <input type="text" name="telefone" id="telefone" class="form-control">
- </div>
- <button type="submit" class="btn btn-primary">Cadastrar</button>
- </form>
- </div><!-- .container -->
- </body>
- </html>
- Visualização
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
- <title>Cliente</title>
- <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
- </head>
- <body>
- <div class="container">
- <table class="table">
- <tbody>
- <tr>
- <th>Código:</th>
- <td><%= cliente.getCodigo() %></td>
- </tr>
- <tr>
- <th>Nome:</th>
- <td><%= cliente.getNome() %></td>
- </tr>
- <tr>
- <th>Email:</th>
- <td><%= cliente.getEmail() %></td>
- </tr>
- <tr>
- <th>Telefone:</th>
- <td><%= cliente.getTelefone() %></td>
- </tr>
- </tbody>
- </table>
- <a href="<%= request.getContextPath() + "/novo-cliente.jsp" %>" class="btn btn-primary">Voltar</a>
- </div><!-- .container -->
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement