[code]package br.com.desenvolvimento;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GravaContato extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
PrintWriter out = response.getWriter();
Connection conexao = new Conexao().abreConexao();
String sql = "SELECT login, email FROM usuario";
try{
Statement st = conexao.createStatement();
ResultSet rs = st.executeQuery(sql);
String login = "";
String email = "";
if(rs.next()){
do{
login = rs.getString("login");
email = rs.getString("email");
if(login == "" || login == null || email == "" || login == null
|| login != request.getParameter("login") || email != request.getParameter("email")){
Contato cont = new Contato();
cont.setNome(request.getParameter("nome"));
cont.setEmail(request.getParameter("email"));
cont.setSexo(request.getParameter("sexo"));
cont.setIdade(Integer.parseInt(request.getParameter("idade")));
cont.setNews(request.getParameter("news"));
cont.setLogin(request.getParameter("login"));
ComandoDAO dao = new ComandoDAO();
dao.insereContato(cont);
}else{
out.println("NÃO FOI POSSÍVEL CADASTRAR. EMAIL OU LOGIN JÁ EXISTE");
}
}while(rs.next());
}else{
Contato cont = new Contato();
cont.setNome(request.getParameter("nome"));
cont.setEmail(request.getParameter("email"));
cont.setSexo(request.getParameter("sexo"));
cont.setIdade(Integer.parseInt(request.getParameter("idade")));
cont.setNews(request.getParameter("news"));
cont.setLogin(request.getParameter("login"));
ComandoDAO dao = new ComandoDAO();
dao.insereContato(cont);
}
}
catch(Exception e){
out.println("ERRO: " + e.getMessage());
}
}
}
[/code]