Opa!!!
Estou tentando começar a fazer as coisas certas, hehehe, separar as camadas certinho.
O que eu quero ter no final, é um cadastro de Contatos, bem simples, uma tela com uma lista com todos os Contatos cadastrados, opção para adicionar, atualizar e excluir.
Comecei pelo Bean. Queria que vocês dessem uma olhada se está certo até agora. Estou desconfiado de que aquele método processaRequest deveria estar em um servlet, porém se eu colocar ele em um servlet não sei como ele vai pegar as informações do JSP diretamente pelo submit. Eu teria que fazer um servlet extends Contato?
import java.io.Serializable;
import java.util.Properties;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
public class Contato implements Serializable{
private int id;
private String nome;
private String sobrenome;
private String endereco;
private String telefone;
private String celular;
private String btAction;
private Properties prop;
public Contato(){
prop = new Properties();
prop.put("user", "root");
prop.put("password", "");
}
public Contato(String nome,
String sobrenome,
String endereco,
String telefone,
String celular){
this.nome = nome;
this.sobrenome = sobrenome;
this.endereco = endereco;
this.telefone = telefone;
this.celular = celular;
}
/* ID */
public int getId(){
return id;
}
public void setId(int id){
this.id = id;
}
/* Nome */
public String getNome(){
return nome;
}
public void setNome(String nome){
this.nome = nome;
}
/* Sobrenome */
public String getSobrenome(){
return sobrenome;
}
public void setSobrenome(String sobrenome){
this.sobrenome = sobrenome;
}
/* Celular */
public String getCelular() {
return celular;
}
public void setCelular(String string) {
celular = string;
}
/* Endereco */
public String getEndereco() {
return endereco;
}
public void setEndereco(String string) {
endereco = string;
}
/* Telfone */
public String getTelefone() {
return telefone;
}
public void setTelefone(String string) {
telefone = string;
}
/* Action */
public String getBtAction(){
return btAction;
}
public void setBtAction(String string){
this.btAction = string;
}
/* Adiciona Contato */
private boolean addContato(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch(ClassNotFoundException e) {
System.err.println(e);
return false;
}
try{
String stringSQL;
stringSQL = "INSERT INTO CONTATO (nome, sobrenome, telefone, celular, endereco) VALUES (?,?,?,?,?)";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/agenda", prop);
PreparedStatement pstmt = con.prepareStatement(stringSQL);
pstmt.setString(1, nome);
pstmt.setString(2, sobrenome);
pstmt.setString(3, telefone);
pstmt.setString(4, celular);
pstmt.setString(5, endereco);
pstmt.executeUpdate();
con.close();
return true;
}catch (SQLException ex) {
System.err.println(ex);
return false;
}
}
/* Atualiza Contato */
private boolean updContato(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch(ClassNotFoundException e) {
System.err.println(e);
return false;
}
try{
String stringSQL;
stringSQL = "UDATE CONTATO";
stringSQL = stringSQL + " SET nome = ?";
stringSQL = stringSQL + ", sobrenome = ?";
stringSQL = stringSQL + ", telefone = ?";
stringSQL = stringSQL + ", celular = ?";
stringSQL = stringSQL + ", endereco = ?";
stringSQL = stringSQL + " WHERE id = ?";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/agenda", prop);
PreparedStatement pstmt = con.prepareStatement(stringSQL);
pstmt.setString(1, nome);
pstmt.setString(2, sobrenome);
pstmt.setString(3, telefone);
pstmt.setString(4, celular);
pstmt.setString(5, endereco);
pstmt.setInt(6, id);
pstmt.executeUpdate();
con.close();
return true;
}catch (SQLException ex) {
System.err.println(ex);
return false;
}
}
/* Elimina Contato */
private boolean delContato(){
try {
Class.forName("org.gjt.mm.mysql.Driver");
} catch(ClassNotFoundException e) {
System.err.println(e);
return false;
}
try{
String stringSQL;
stringSQL = "DELETE FROM CONTATO WHERE id = ?";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/agenda", prop);
PreparedStatement pstmt = con.prepareStatement(stringSQL);
pstmt.setInt(1, id);
pstmt.executeUpdate();
con.close();
return true;
}catch (SQLException ex) {
System.err.println(ex);
return false;
}
}
/* Processa Request */
public boolean processaRequest(HttpServletRequest request){
boolean success = false;
if (btAction.equals("add")) {
success = addContato();
}else if (btAction.equals("upd")){
success = updContato();
}else if (btAction.equals("del")){
success = delContato();
}
return success;
}
}
Outra coisa, sei como montar uma Collection e retornar para o JSP, mas não sei como mostrar os dados dela utilizando as tags dos beans por exemplo <jsp:getProperty name=“contato” property=“nome” />.
Aos poucos eu aprendo, hehehehe.
Valeu!
