package br.edu.metodista.acesso.menu;
public class Menu {
private Long id;
private String nome;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getNome() { return nome; }
public void setNome(String nome) { this.nome = nome; }
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Menu other = (Menu) obj;
if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + (this.id != null ? this.id.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return nome;
}
}
MenuDAO.java
package br.edu.metodista.acesso.menu;
import java.sql.*;
import java.util.*;
import br.edu.metodista.ipa.bd.*;
public class MenuDAO {
public int salvar(Menu menu) throws Exception {
if (existe(menu))
return alterar(menu);
else
return incluir(menu);
}
public int incluir(Menu menu) throws Exception {
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(INCLUIR_SQL);
try {
stmt.setLong(1, BD.getProximoId(TABELA));
stmt.setString(2, menu.getNome());
return stmt.executeUpdate();
} finally {
stmt.close();
conn.close();
}
}
public int alterar(Menu menu) throws Exception {
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(ALTERAR_SQL);
try {
stmt.setLong(1, menu.getId());
stmt.setString(2, menu.getNome());
stmt.setLong(3, menu.getId());
return stmt.executeUpdate();
} finally {
stmt.close();
conn.close();
}
}
public int excluir(Menu menu) throws Exception {
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(EXCLUIR_SQL);
try {
stmt.setLong(1, menu.getId());
return stmt.executeUpdate();
} finally {
stmt.close();
conn.close();
}
}
public boolean existe(Menu menu) throws Exception {
if (menu.getId() == null)
return false;
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(EXISTE_SQL);
try {
stmt.setLong(1, menu.getId());
RecordSet rs = new RecordSet(stmt.executeQuery());
if (rs.next())
return true;
return false;
} finally {
stmt.close();
conn.close();
}
}
public boolean carregar(Menu menu) throws Exception {
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(CARREGAR_SQL);
try {
stmt.setLong(1, menu.getId());
RecordSet rs = new RecordSet(stmt.executeQuery());
if (rs.next()) {
menu.setNome(rs.getString("nome"));
return true;
}
return false;
} finally {
stmt.close();
conn.close();
}
}
public ArrayList listar() throws Exception {
ArrayList lista = new ArrayList();
Connection conn = BD.getConnection();
PreparedStatement stmt = conn.prepareStatement(LISTAR_SQL);
try {
RecordSet rs = new RecordSet(stmt.executeQuery());
while (rs.next()) {
Menu menu = new Menu();
menu.setId(rs.getLong("id"));
menu.setNome(rs.getString("nome"));
}
return lista;
} finally {
stmt.close();
conn.close();
}
}
private static final String TABELA = "tbl_menu";
private static final String INCLUIR_SQL = "INSERT INTO " + TABELA + " (id, nome) VALUES (?,?)";
private static final String ALTERAR_SQL = "UPDATE " + TABELA + " SET id=?, nome=? WHERE id=?";
private static final String EXCLUIR_SQL = "DELETE FROM " + TABELA + " WHERE id=?";
private static final String CARREGAR_SQL = "SELECT * FROM " + TABELA + " WHERE id=?";
private static final String EXISTE_SQL = "SELECT id FROM " + TABELA + " WHERE id=?";
private static final String LISTAR_SQL = "SELECT * FROM " + TABELA + " ";
public static void main(String[] args) throws Exception {
Menu s = null;
s = new Menu();
s.setNome("Países");
new MenuDAO().salvar(s);
s = new Menu();
s.setNome("Estados");
new MenuDAO().salvar(s);
s = new Menu();
s.setNome("Cidades");
new MenuDAO().salvar(s);
}
}
Nenhum comentário:
Postar um comentário